Skip to content

Commit 7e01911

Browse files
Issue python#5308: Raise ValueError when marshalling too large object (a sequence
with size >= 2**31), instead of producing illegal marshal data.
1 parent 76a2ed1 commit 7e01911

3 files changed

Lines changed: 116 additions & 66 deletions

File tree

Lib/test/test_marshal.py

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -262,16 +262,63 @@ def test_loads_reject_unicode_strings(self):
262262
unicode_string = 'T'
263263
self.assertRaises(TypeError, marshal.loads, unicode_string)
264264

265+
LARGE_SIZE = 2**31
266+
character_size = 4 if sys.maxunicode > 0xFFFF else 2
267+
pointer_size = 8 if sys.maxsize > 0xFFFFFFFF else 4
268+
269+
class NullWriter:
270+
def write(self, s):
271+
pass
272+
273+
@unittest.skipIf(LARGE_SIZE > sys.maxsize, "test cannot run on 32-bit systems")
274+
class LargeValuesTestCase(unittest.TestCase):
275+
def check_unmarshallable(self, data):
276+
self.assertRaises(ValueError, marshal.dump, data, NullWriter())
277+
278+
@support.bigmemtest(size=LARGE_SIZE, memuse=1, dry_run=False)
279+
def test_bytes(self, size):
280+
self.check_unmarshallable(b'x' * size)
281+
282+
@support.bigmemtest(size=LARGE_SIZE, memuse=character_size, dry_run=False)
283+
def test_str(self, size):
284+
self.check_unmarshallable('x' * size)
285+
286+
@support.bigmemtest(size=LARGE_SIZE, memuse=pointer_size, dry_run=False)
287+
def test_tuple(self, size):
288+
self.check_unmarshallable((None,) * size)
289+
290+
@support.bigmemtest(size=LARGE_SIZE, memuse=pointer_size, dry_run=False)
291+
def test_list(self, size):
292+
self.check_unmarshallable([None] * size)
293+
294+
@support.bigmemtest(size=LARGE_SIZE,
295+
memuse=pointer_size*12 + sys.getsizeof(LARGE_SIZE-1),
296+
dry_run=False)
297+
def test_set(self, size):
298+
self.check_unmarshallable(set(range(size)))
299+
300+
@support.bigmemtest(size=LARGE_SIZE,
301+
memuse=pointer_size*12 + sys.getsizeof(LARGE_SIZE-1),
302+
dry_run=False)
303+
def test_frozenset(self, size):
304+
self.check_unmarshallable(frozenset(range(size)))
305+
306+
@support.bigmemtest(size=LARGE_SIZE, memuse=1, dry_run=False)
307+
def test_bytearray(self, size):
308+
self.check_unmarshallable(bytearray(size))
309+
265310

266311
def test_main():
267312
support.run_unittest(IntTestCase,
268-
FloatTestCase,
269-
StringTestCase,
270-
CodeTestCase,
271-
ContainerTestCase,
272-
ExceptionTestCase,
273-
BufferTestCase,
274-
BugsTestCase)
313+
FloatTestCase,
314+
StringTestCase,
315+
CodeTestCase,
316+
ContainerTestCase,
317+
ExceptionTestCase,
318+
BufferTestCase,
319+
BugsTestCase,
320+
LargeValuesTestCase,
321+
)
275322

276323
if __name__ == "__main__":
277324
test_main()

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ What's New in Python 3.2.4
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #5308: Raise ValueError when marshalling too large object (a sequence
14+
with size >= 2**31), instead of producing illegal marshal data.
15+
1316
- Issue #12983: Bytes literals with invalid \x escape now raise a SyntaxError
1417
and a full traceback including line number.
1518

0 commit comments

Comments
 (0)