]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-32137: The repr of deeply nested dict now raises a RecursionError (GH-4570) ...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Thu, 1 Feb 2018 11:57:28 +0000 (03:57 -0800)
committerSerhiy Storchaka <storchaka@gmail.com>
Thu, 1 Feb 2018 11:57:28 +0000 (13:57 +0200)
instead of crashing due to a stack overflow.

This perhaps will fix similar problems in other extension types.
(cherry picked from commit 1fb72d2ad243c965d4432b4e93884064001a2607)

Lib/test/list_tests.py
Lib/test/mapping_tests.py
Lib/test/test_dict.py
Misc/NEWS.d/next/Core and Builtins/2017-11-26-14-36-30.bpo-32137.Stj5nL.rst [new file with mode: 0644]
Objects/listobject.c
Objects/object.c
Objects/tupleobject.c

index 26e93687f41162dce6bd8b40df3b9410274636f4..05e771f944b9c67aab5e609efa77b8afc1ec77f7 100644 (file)
@@ -53,10 +53,11 @@ class CommonTest(seq_tests.CommonTest):
         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))
index ff82f4eb7d8988a3f5448ec76c1c0b6e8bba2370..53f29f605386ba575cf96d6925ad77e5a46e9372 100644 (file)
@@ -1,6 +1,7 @@
 # tests common to dict and UserDict
 import unittest
 import collections
+import sys
 
 
 class BasicTestMappingProtocol(unittest.TestCase):
@@ -619,6 +620,14 @@ class TestHashMappingProtocol(TestMappingProtocol):
         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}),
index 8013f37c88da0ac57de0e343df132c2f82d2c27b..4386eda3ae48fdd4a65d81e545d22a66b1e0384f 100644 (file)
@@ -468,6 +468,12 @@ class DictTest(unittest.TestCase):
         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 (file)
index 0000000..f8f4ab9
--- /dev/null
@@ -0,0 +1,2 @@
+The repr of deeply nested dict now raises a RecursionError instead of
+crashing due to a stack overflow.
index 547bdf0b95f1ee841aebd45719bdcc088bdce858..ea8310f153aed49e11eb8ce6342fef66f7291740 100644 (file)
@@ -366,10 +366,7 @@ list_repr(PyListObject *v)
                 goto error;
         }
 
-        if (Py_EnterRecursiveCall(" while getting the repr of a list"))
-            goto error;
         s = PyObject_Repr(v->ob_item[i]);
-        Py_LeaveRecursiveCall();
         if (s == NULL)
             goto error;
 
index d88ae3b94f3e7d49928a4dfe5d3ba4adfa3c902b..defff5579699e11c79849bca431d8c446ae29c0f 100644 (file)
@@ -482,7 +482,12 @@ PyObject_Repr(PyObject *v)
     assert(!PyErr_Occurred());
 #endif
 
+    /* It is possible for a type to have a tp_repr representation that loops
+       infinitely. */
+    if (Py_EnterRecursiveCall(" while getting the repr of an object"))
+        return NULL;
     res = (*v->ob_type->tp_repr)(v);
+    Py_LeaveRecursiveCall();
     if (res == NULL)
         return NULL;
     if (!PyUnicode_Check(res)) {
index 0dada74dc7c986241c2042d1b32d74dae1136f1d..047926f969b768099def2cce51cfc448278ee6a1 100644 (file)
@@ -300,10 +300,7 @@ tuplerepr(PyTupleObject *v)
                 goto error;
         }
 
-        if (Py_EnterRecursiveCall(" while getting the repr of a tuple"))
-            goto error;
         s = PyObject_Repr(v->ob_item[i]);
-        Py_LeaveRecursiveCall();
         if (s == NULL)
             goto error;