Skip to content

Commit 9720f60

Browse files
[2.7] bpo-33720: Improve tests for the stack overflow in marshal.loads(). (GH-7336). (GH-8107)
(cherry picked from commit fc05e68) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent bfee590 commit 9720f60

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

Lib/test/test_marshal.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,22 @@ def test_fuzz(self):
227227
pass
228228

229229
def test_loads_recursion(self):
230-
s = 'c' + ('X' * 4*4) + '{' * 2**20
231-
self.assertRaises(ValueError, marshal.loads, s)
230+
def run_tests(N, check):
231+
# (((...None...),),)
232+
check(b'(\x01\x00\x00\x00' * N + b'N')
233+
# [[[...None...]]]
234+
check(b'[\x01\x00\x00\x00' * N + b'N')
235+
# {None: {None: {None: ...None...}}}
236+
check(b'{N' * N + b'N' + b'0' * N)
237+
# frozenset([frozenset([frozenset([...None...])])])
238+
check(b'>\x01\x00\x00\x00' * N + b'N')
239+
# Check that the generated marshal data is valid and marshal.loads()
240+
# works for moderately deep nesting
241+
run_tests(100, marshal.loads)
242+
# Very deeply nested structure shouldn't blow the stack
243+
def check(s):
244+
self.assertRaises(ValueError, marshal.loads, s)
245+
run_tests(2**20, check)
232246

233247
def test_recursion_limit(self):
234248
# Create a deeply nested structure.

0 commit comments

Comments
 (0)