Kaydet (Commit) 34fe1b7a authored tarafından Serhiy Storchaka's avatar Serhiy Storchaka

Issue #5308: Raise ValueError when marshalling too large object (a sequence

with size >= 2**31), instead of producing illegal marshal data.
üst 7d360038
......@@ -269,6 +269,53 @@ class BugsTestCase(unittest.TestCase):
invalid_string = 'l\x02\x00\x00\x00\x00\x00\x00\x00'
self.assertRaises(ValueError, marshal.loads, invalid_string)
LARGE_SIZE = 2**31
character_size = 4 if sys.maxunicode > 0xFFFF else 2
pointer_size = 8 if sys.maxsize > 0xFFFFFFFF else 4
@unittest.skipIf(LARGE_SIZE > sys.maxsize, "test cannot run on 32-bit systems")
class LargeValuesTestCase(unittest.TestCase):
def check_unmarshallable(self, data):
f = open(test_support.TESTFN, 'wb')
self.addCleanup(test_support.unlink, test_support.TESTFN)
with f:
self.assertRaises(ValueError, marshal.dump, data, f)
@test_support.precisionbigmemtest(size=LARGE_SIZE, memuse=1, dry_run=False)
def test_bytes(self, size):
self.check_unmarshallable(b'x' * size)
@test_support.precisionbigmemtest(size=LARGE_SIZE,
memuse=character_size, dry_run=False)
def test_str(self, size):
self.check_unmarshallable('x' * size)
@test_support.precisionbigmemtest(size=LARGE_SIZE,
memuse=pointer_size, dry_run=False)
def test_tuple(self, size):
self.check_unmarshallable((None,) * size)
@test_support.precisionbigmemtest(size=LARGE_SIZE,
memuse=pointer_size, dry_run=False)
def test_list(self, size):
self.check_unmarshallable([None] * size)
@test_support.precisionbigmemtest(size=LARGE_SIZE,
memuse=pointer_size*12 + sys.getsizeof(LARGE_SIZE-1),
dry_run=False)
def test_set(self, size):
self.check_unmarshallable(set(range(size)))
@test_support.precisionbigmemtest(size=LARGE_SIZE,
memuse=pointer_size*12 + sys.getsizeof(LARGE_SIZE-1),
dry_run=False)
def test_frozenset(self, size):
self.check_unmarshallable(frozenset(range(size)))
@test_support.precisionbigmemtest(size=LARGE_SIZE, memuse=1, dry_run=False)
def test_bytearray(self, size):
self.check_unmarshallable(bytearray(size))
def test_main():
test_support.run_unittest(IntTestCase,
......@@ -277,7 +324,9 @@ def test_main():
CodeTestCase,
ContainerTestCase,
ExceptionTestCase,
BugsTestCase)
BugsTestCase,
LargeValuesTestCase,
)
if __name__ == "__main__":
test_main()
......@@ -1062,7 +1062,7 @@ def bigmemtest(minsize, memuse, overhead=5*_1M):
return wrapper
return decorator
def precisionbigmemtest(size, memuse, overhead=5*_1M):
def precisionbigmemtest(size, memuse, overhead=5*_1M, dry_run=True):
def decorator(f):
def wrapper(self):
if not real_max_memuse:
......@@ -1070,11 +1070,12 @@ def precisionbigmemtest(size, memuse, overhead=5*_1M):
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
if ((real_max_memuse or not dry_run)
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
......
......@@ -9,6 +9,9 @@ What's New in Python 2.7.4
Core and Builtins
-----------------
- Issue #5308: Raise ValueError when marshalling too large object (a sequence
with size >= 2**31), instead of producing illegal marshal data.
- Issue #17043: The unicode-internal decoder no longer read past the end of
input buffer.
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment