From a1771bb2f2604260c51f87b45ad958d915ba39a1 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Sun, 3 Dec 2017 22:12:11 +0200 Subject: [PATCH] [3.6] bpo-32137: The repr of deeply nested dict now raises a RecursionError (GH-4570) instead of crashing due to a stack overflow. This perhaps will fix similar problems in other extension types. (cherry picked from commit 1fb72d2) --- Lib/test/list_tests.py | 7 ++++--- Lib/test/mapping_tests.py | 9 +++++++++ Lib/test/test_dict.py | 6 ++++++ .../2017-11-26-14-36-30.bpo-32137.Stj5nL.rst | 2 ++ Objects/dictobject.c | 6 ++++++ 5 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2017-11-26-14-36-30.bpo-32137.Stj5nL.rst diff --git a/Lib/test/list_tests.py b/Lib/test/list_tests.py index 26e93687f41162..05e771f944b9c6 100644 --- a/Lib/test/list_tests.py +++ b/Lib/test/list_tests.py @@ -53,10 +53,11 @@ def test_repr(self): self.assertEqual(str(a2), "[0, 1, 2, [...], 3]") self.assertEqual(repr(a2), "[0, 1, 2, [...], 3]") - l0 = [] + def test_repr_deep(self): + a = self.type2test([]) for i in range(sys.getrecursionlimit() + 100): - l0 = [l0] - self.assertRaises(RecursionError, repr, l0) + a = self.type2test([a]) + self.assertRaises(RecursionError, repr, a) def test_print(self): d = self.type2test(range(200)) diff --git a/Lib/test/mapping_tests.py b/Lib/test/mapping_tests.py index ff82f4eb7d8988..53f29f605386ba 100644 --- a/Lib/test/mapping_tests.py +++ b/Lib/test/mapping_tests.py @@ -1,6 +1,7 @@ # tests common to dict and UserDict import unittest import collections +import sys class BasicTestMappingProtocol(unittest.TestCase): @@ -619,6 +620,14 @@ def __repr__(self): d = self._full_mapping({1: BadRepr()}) self.assertRaises(Exc, repr, d) + def test_repr_deep(self): + d = self._empty_mapping() + for i in range(sys.getrecursionlimit() + 100): + d0 = d + d = self._empty_mapping() + d[1] = d0 + self.assertRaises(RecursionError, repr, d) + def test_eq(self): self.assertEqual(self._empty_mapping(), self._empty_mapping()) self.assertEqual(self._full_mapping({1: 2}), diff --git a/Lib/test/test_dict.py b/Lib/test/test_dict.py index 8013f37c88da0a..4386eda3ae48fd 100644 --- a/Lib/test/test_dict.py +++ b/Lib/test/test_dict.py @@ -468,6 +468,12 @@ def __repr__(self): d = {1: BadRepr()} self.assertRaises(Exc, repr, d) + def test_repr_deep(self): + d = {} + for i in range(sys.getrecursionlimit() + 100): + d = {1: d} + self.assertRaises(RecursionError, repr, d) + def test_eq(self): self.assertEqual({}, {}) self.assertEqual({1: 2}, {1: 2}) diff --git a/Misc/NEWS.d/next/Core and Builtins/2017-11-26-14-36-30.bpo-32137.Stj5nL.rst b/Misc/NEWS.d/next/Core and Builtins/2017-11-26-14-36-30.bpo-32137.Stj5nL.rst new file mode 100644 index 00000000000000..f8f4ab93c9e2e8 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2017-11-26-14-36-30.bpo-32137.Stj5nL.rst @@ -0,0 +1,2 @@ +The repr of deeply nested dict now raises a RecursionError instead of +crashing due to a stack overflow. diff --git a/Objects/dictobject.c b/Objects/dictobject.c index 690ef3bd2b3ae0..b467288690ebc5 100644 --- a/Objects/dictobject.c +++ b/Objects/dictobject.c @@ -2075,7 +2075,10 @@ dict_repr(PyDictObject *mp) } first = 0; + if (Py_EnterRecursiveCall(" while getting the repr of a dict")) + goto error; s = PyObject_Repr(key); + Py_LeaveRecursiveCall(); if (s == NULL) goto error; res = _PyUnicodeWriter_WriteStr(&writer, s); @@ -2086,7 +2089,10 @@ dict_repr(PyDictObject *mp) if (_PyUnicodeWriter_WriteASCIIString(&writer, ": ", 2) < 0) goto error; + if (Py_EnterRecursiveCall(" while getting the repr of a dict")) + goto error; s = PyObject_Repr(value); + Py_LeaveRecursiveCall(); if (s == NULL) goto error; res = _PyUnicodeWriter_WriteStr(&writer, s);