]> granicus.if.org Git - python/commitdiff
bpo-33720: Improve tests for the stack overflow in marshal.loads(). (GH-7336)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Thu, 5 Jul 2018 08:48:47 +0000 (01:48 -0700)
committerGitHub <noreply@github.com>
Thu, 5 Jul 2018 08:48:47 +0000 (01:48 -0700)
(cherry picked from commit fc05e68d8fac70349b7ea17ec14e7e0cfa956121)

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

index 29dda987d0bb7f28c59e16ccb4769acb469ce00a..c40cdbfa5bee20b3682a4ab3740443aa7f2f46a5 100644 (file)
@@ -210,13 +210,24 @@ class BugsTestCase(unittest.TestCase):
             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.