Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
bpo-33720: Improve test for the stack overflow in marshal.loads().
  • Loading branch information
serhiy-storchaka committed Jun 2, 2018
commit 2a939bacd99658b396c55c2203c5e7004b7f4826
23 changes: 17 additions & 6 deletions Lib/test/test_marshal.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,13 +210,24 @@ def test_fuzz(self):
except Exception:
pass

def test_loads_2x_code(self):
s = b'c' + (b'X' * 4*4) + b'{' * 2**20
self.assertRaises(ValueError, marshal.loads, s)

def test_loads_recursion(self):
s = b'c' + (b'X' * 4*5) + b'{' * 2**20
self.assertRaises(ValueError, marshal.loads, s)
def run_tests(N, check):
# (((...None...),),)
check(b')\x01' * N + b'N')
check(b'(\x01\x00\x00\x00' * N + b'N')
# [[[...None...]]]
check(b'[\x01\x00\x00\x00' * N + b'N')
# {None: {None: {None: ...None...}}}
check(b'{N' * N + b'N' + b'0' * N)
# frozenset([frozenset([frozenset([...None...])])])
check(b'>\x01\x00\x00\x00' * N + b'N')
# Check that the generated marshal data is valid and marshal.loads()
# works for moderately deep nesting
run_tests(100, marshal.loads)
# Very deeply nested structure shouldn't blow the stack
def check(s):
self.assertRaises(ValueError, marshal.loads, s)
run_tests(2**20, check)

def test_recursion_limit(self):
# Create a deeply nested structure.
Expand Down