summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2018-07-05 08:17:20 (GMT)
committerGitHub <noreply@github.com>2018-07-05 08:17:20 (GMT)
commitfc05e68d8fac70349b7ea17ec14e7e0cfa956121 (patch)
tree17a855433e7ef2595859d18589d022ea881f264e /Lib
parent3a9bb5f269c8534606eb02d1faa2c882d8cb8530 (diff)
downloadcpython-fc05e68d8fac70349b7ea17ec14e7e0cfa956121.zip
cpython-fc05e68d8fac70349b7ea17ec14e7e0cfa956121.tar.gz
cpython-fc05e68d8fac70349b7ea17ec14e7e0cfa956121.tar.bz2
bpo-33720: Improve tests for the stack overflow in marshal.loads(). (GH-7336)
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_marshal.py23
1 files changed, 17 insertions, 6 deletions
diff --git a/Lib/test/test_marshal.py b/Lib/test/test_marshal.py
index a8a43d2..a20ad67 100644
--- a/Lib/test/test_marshal.py
+++ b/Lib/test/test_marshal.py
@@ -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.