]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[2.7] bpo-33720: Improve tests for the stack overflow in marshal.loads(). (GH-7336...
authorSerhiy Storchaka <storchaka@gmail.com>
Thu, 5 Jul 2018 09:20:19 +0000 (12:20 +0300)
committerGitHub <noreply@github.com>
Thu, 5 Jul 2018 09:20:19 +0000 (12:20 +0300)
(cherry picked from commit fc05e68d8fac70349b7ea17ec14e7e0cfa956121)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Lib/test/test_marshal.py

index 76d59bac392a0fa5055a3086ef78fb32618e0a3b..bbaa9442ab76b04b657bdc1ea4498bb67e76f37e 100644 (file)
@@ -227,8 +227,22 @@ class BugsTestCase(unittest.TestCase):
                 pass
 
     def test_loads_recursion(self):
-        s = 'c' + ('X' * 4*4) + '{' * 2**20
-        self.assertRaises(ValueError, marshal.loads, s)
+        def run_tests(N, check):
+            # (((...None...),),)
+            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.