diff options
author | Neal Norwitz <nnorwitz@gmail.com> | 2008-07-31 17:17:14 (GMT) |
---|---|---|
committer | Neal Norwitz <nnorwitz@gmail.com> | 2008-07-31 17:17:14 (GMT) |
commit | e7d8be80ba634fa15ece6f503c33592e0d333361 (patch) | |
tree | 4264163ebdcea24504f3842330602d98301cf659 /Lib | |
parent | e70f8e1205b5fc60a30469db69bbee4d5d532d86 (diff) | |
download | cpython-e7d8be80ba634fa15ece6f503c33592e0d333361.zip cpython-e7d8be80ba634fa15ece6f503c33592e0d333361.tar.gz cpython-e7d8be80ba634fa15ece6f503c33592e0d333361.tar.bz2 |
Security patches from Apple: prevent int overflow when allocating memory
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/seq_tests.py | 12 | ||||
-rw-r--r-- | Lib/test/test_bigmem.py | 114 | ||||
-rw-r--r-- | Lib/test/test_strop.py | 19 | ||||
-rw-r--r-- | Lib/test/test_support.py | 25 |
4 files changed, 161 insertions, 9 deletions
diff --git a/Lib/test/seq_tests.py b/Lib/test/seq_tests.py index c273a3f..14303aa 100644 --- a/Lib/test/seq_tests.py +++ b/Lib/test/seq_tests.py @@ -306,11 +306,13 @@ class CommonTest(unittest.TestCase): self.assertEqual(id(s), id(s*1)) def test_bigrepeat(self): - x = self.type2test([0]) - x *= 2**16 - self.assertRaises(MemoryError, x.__mul__, 2**16) - if hasattr(x, '__imul__'): - self.assertRaises(MemoryError, x.__imul__, 2**16) + import sys + if sys.maxint <= 2147483647: + x = self.type2test([0]) + x *= 2**16 + self.assertRaises(MemoryError, x.__mul__, 2**16) + if hasattr(x, '__imul__'): + self.assertRaises(MemoryError, x.__imul__, 2**16) def test_subscript(self): a = self.type2test([10, 11]) diff --git a/Lib/test/test_bigmem.py b/Lib/test/test_bigmem.py index d4fc6eb..360bcb2 100644 --- a/Lib/test/test_bigmem.py +++ b/Lib/test/test_bigmem.py @@ -1,5 +1,5 @@ from test import test_support -from test.test_support import bigmemtest, _1G, _2G +from test.test_support import bigmemtest, _1G, _2G, _4G, precisionbigmemtest import unittest import operator @@ -54,6 +54,22 @@ class StrTest(unittest.TestCase): self.assertEquals(s[lpadsize:-rpadsize], SUBSTR) self.assertEquals(s.strip(), SUBSTR.strip()) + @precisionbigmemtest(size=_2G - 1, memuse=1) + def test_center_unicode(self, size): + SUBSTR = u' abc def ghi' + try: + s = SUBSTR.center(size) + except OverflowError: + pass # acceptable on 32-bit + else: + self.assertEquals(len(s), size) + lpadsize = rpadsize = (len(s) - len(SUBSTR)) // 2 + if len(s) % 2: + lpadsize += 1 + self.assertEquals(s[lpadsize:-rpadsize], SUBSTR) + self.assertEquals(s.strip(), SUBSTR.strip()) + del s + @bigmemtest(minsize=_2G, memuse=2) def test_count(self, size): SUBSTR = ' abc def ghi' @@ -70,10 +86,51 @@ class StrTest(unittest.TestCase): s = '.' * size self.assertEquals(len(s.decode('utf-8')), size) + def basic_encode_test(self, size, enc, c=u'.', expectedsize=None): + if expectedsize is None: + expectedsize = size + + s = c * size + self.assertEquals(len(s.encode(enc)), expectedsize) + @bigmemtest(minsize=_2G + 2, memuse=3) def test_encode(self, size): - s = u'.' * size - self.assertEquals(len(s.encode('utf-8')), size) + return self.basic_encode_test(size, 'utf-8') + + @precisionbigmemtest(size=_4G / 6 + 2, memuse=2) + def test_encode_raw_unicode_escape(self, size): + try: + return self.basic_encode_test(size, 'raw_unicode_escape') + except MemoryError: + pass # acceptable on 32-bit + + @precisionbigmemtest(size=_4G / 5 + 70, memuse=3) + def test_encode_utf7(self, size): + try: + return self.basic_encode_test(size, 'utf7') + except MemoryError: + pass # acceptable on 32-bit + + @precisionbigmemtest(size=_4G / 4 + 5, memuse=6) + def test_encode_utf32(self, size): + try: + return self.basic_encode_test(size, 'utf32', expectedsize=4*size+4) + except MemoryError: + pass # acceptable on 32-bit + + @precisionbigmemtest(size=_2G-1, memuse=2) + def test_decodeascii(self, size): + return self.basic_encode_test(size, 'ascii', c='A') + + @precisionbigmemtest(size=_4G / 5, memuse=6+2) + def test_unicode_repr_oflw(self, size): + try: + s = u"\uAAAA"*size + r = repr(s) + except MemoryError: + pass # acceptable on 32-bit + else: + self.failUnless(s == eval(r)) @bigmemtest(minsize=_2G, memuse=2) def test_endswith(self, size): @@ -459,6 +516,11 @@ class StrTest(unittest.TestCase): self.assertEquals(s.count('\\'), size) self.assertEquals(s.count('0'), size * 2) + @bigmemtest(minsize=2**32 / 5, memuse=6+2) + def test_unicode_repr(self, size): + s = u"\uAAAA" * size + self.failUnless(len(repr(s)) > size) + # This test is meaningful even with size < 2G, as long as the # doubled string is > 2G (but it tests more if both are > 2G :) @bigmemtest(minsize=_1G + 2, memuse=3) @@ -642,6 +704,35 @@ class TupleTest(unittest.TestCase): def test_repeat_large(self, size): return self.basic_test_repeat(size) + @bigmemtest(minsize=_1G - 1, memuse=12) + def test_repeat_large_2(self, size): + return self.basic_test_repeat(size) + + @precisionbigmemtest(size=_1G - 1, memuse=9) + def test_from_2G_generator(self, size): + try: + t = tuple(xrange(size)) + except MemoryError: + pass # acceptable on 32-bit + else: + count = 0 + for item in t: + self.assertEquals(item, count) + count += 1 + self.assertEquals(count, size) + + @precisionbigmemtest(size=_1G - 25, memuse=9) + def test_from_almost_2G_generator(self, size): + try: + t = tuple(xrange(size)) + count = 0 + for item in t: + self.assertEquals(item, count) + count += 1 + self.assertEquals(count, size) + except MemoryError: + pass # acceptable, expected on 32-bit + # Like test_concat, split in two. def basic_test_repr(self, size): t = (0,) * size @@ -957,8 +1048,23 @@ class ListTest(unittest.TestCase): self.assertEquals(l[:10], [1] * 10) self.assertEquals(l[-10:], [5] * 10) +class BufferTest(unittest.TestCase): + + @precisionbigmemtest(size=_1G, memuse=4) + def test_repeat(self, size): + try: + b = buffer("AAAA")*size + except MemoryError: + pass # acceptable on 32-bit + else: + count = 0 + for c in b: + self.assertEquals(c, 'A') + count += 1 + self.assertEquals(count, size*4) + def test_main(): - test_support.run_unittest(StrTest, TupleTest, ListTest) + test_support.run_unittest(StrTest, TupleTest, ListTest, BufferTest) if __name__ == '__main__': if len(sys.argv) > 1: diff --git a/Lib/test/test_strop.py b/Lib/test/test_strop.py index 2ac7986..7cda83f 100644 --- a/Lib/test/test_strop.py +++ b/Lib/test/test_strop.py @@ -115,6 +115,25 @@ class StropFunctionTestCase(unittest.TestCase): strop.uppercase strop.whitespace + @test_support.precisionbigmemtest(size=test_support._2G - 1, memuse=5) + def test_stropjoin_huge_list(self, size): + a = "A" * size + try: + r = strop.join([a, a], a) + except OverflowError: + pass + else: + self.assertEquals(len(r), len(a) * 3) + + @test_support.precisionbigmemtest(size=test_support._2G - 1, memuse=1) + def test_stropjoin_huge_tup(self, size): + a = "A" * size + try: + r = strop.join((a, a), a) + except OverflowError: + pass # acceptable on 32-bit + else: + self.assertEquals(len(r), len(a) * 3) transtable = '\000\001\002\003\004\005\006\007\010\011\012\013\014\015\016\017\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037 !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`xyzdefghijklmnopqrstuvwxyz{|}~\177\200\201\202\203\204\205\206\207\210\211\212\213\214\215\216\217\220\221\222\223\224\225\226\227\230\231\232\233\234\235\236\237\240\241\242\243\244\245\246\247\250\251\252\253\254\255\256\257\260\261\262\263\264\265\266\267\270\271\272\273\274\275\276\277\300\301\302\303\304\305\306\307\310\311\312\313\314\315\316\317\320\321\322\323\324\325\326\327\330\331\332\333\334\335\336\337\340\341\342\343\344\345\346\347\350\351\352\353\354\355\356\357\360\361\362\363\364\365\366\367\370\371\372\373\374\375\376\377' diff --git a/Lib/test/test_support.py b/Lib/test/test_support.py index 1aa21d3..adcbdd1 100644 --- a/Lib/test/test_support.py +++ b/Lib/test/test_support.py @@ -67,6 +67,7 @@ verbose = 1 # Flag set to 0 by regrtest.py use_resources = None # Flag set to [] by regrtest.py max_memuse = 0 # Disable bigmem tests (they will still be run with # small sizes, to make sure they work.) +real_max_memuse = 0 # _original_stdout is meant to hold stdout at the time regrtest began. # This may be "the real" stdout, or IDLE's emulation of stdout, or whatever. @@ -611,12 +612,14 @@ def run_with_locale(catstr, *locales): _1M = 1024*1024 _1G = 1024 * _1M _2G = 2 * _1G +_4G = 4 * _1G MAX_Py_ssize_t = sys.maxsize def set_memlimit(limit): import re global max_memuse + global real_max_memuse sizes = { 'k': 1024, 'm': _1M, @@ -628,6 +631,7 @@ def set_memlimit(limit): if m is None: raise ValueError('Invalid memory limit %r' % (limit,)) memlimit = int(float(m.group(1)) * sizes[m.group(3).lower()]) + real_max_memuse = memlimit if memlimit > MAX_Py_ssize_t: memlimit = MAX_Py_ssize_t if memlimit < _2G - 1: @@ -673,6 +677,27 @@ def bigmemtest(minsize, memuse, overhead=5*_1M): return wrapper return decorator +def precisionbigmemtest(size, memuse, overhead=5*_1M): + def decorator(f): + def wrapper(self): + if not real_max_memuse: + maxsize = 5147 + else: + maxsize = size + + if real_max_memuse and real_max_memuse < maxsize * memuse: + if verbose: + sys.stderr.write("Skipping %s because of memory " + "constraint\n" % (f.__name__,)) + return + + return f(self, maxsize) + wrapper.size = size + wrapper.memuse = memuse + wrapper.overhead = overhead + return wrapper + return decorator + def bigaddrspacetest(f): """Decorator for tests that fill the address space.""" def wrapper(self): |