]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[2.7] bpo-32137: The repr of deeply nested dict now raises a RuntimeError (GH-4570...
authorSerhiy Storchaka <storchaka@gmail.com>
Fri, 2 Feb 2018 14:29:02 +0000 (16:29 +0200)
committerGitHub <noreply@github.com>
Fri, 2 Feb 2018 14:29:02 +0000 (16:29 +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 ed606db0d11dbcc39e8578a8799f6d84f0c21d2f..54cfb97ccb8ec9b3454c12a0d19746e1effc41ac 100644 (file)
@@ -45,10 +45,11 @@ class CommonTest(seq_tests.CommonTest):
         self.assertEqual(str(a2), "[0, 1, 2, [...], 3]")
         self.assertEqual(repr(a2), "[0, 1, 2, [...], 3]")
 
-        l0 = []
-        for i in xrange(sys.getrecursionlimit() + 100):
-            l0 = [l0]
-        self.assertRaises(RuntimeError, repr, l0)
+    def test_repr_deep(self):
+        a = self.type2test([])
+        for i in range(sys.getrecursionlimit() + 100):
+            a = self.type2test([a])
+        self.assertRaises(RuntimeError, repr, a)
 
     def test_print(self):
         d = self.type2test(xrange(200))
index f43750bb83f62c2962b157241a54fc4232a0c150..248dee2fed18b123094d818b4ac6a81910c8dfe7 100644 (file)
@@ -2,6 +2,7 @@
 import unittest
 import UserDict
 import test_support
+import sys
 
 
 class BasicTestMappingProtocol(unittest.TestCase):
@@ -645,6 +646,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(RuntimeError, repr, d)
+
     def test_le(self):
         self.assertTrue(not (self._empty_mapping() < self._empty_mapping()))
         self.assertTrue(not (self._full_mapping({1: 2}) < self._full_mapping({1L: 2L})))
index 6b2db34ef3319f50619e3a7459ad27f09f368fef..aacd4739ef3b6f77e40e7d343805830bcf49101a 100644 (file)
@@ -3,6 +3,7 @@ from test import test_support
 
 import UserDict, random, string
 import gc, weakref
+import sys
 
 
 class DictTest(unittest.TestCase):
@@ -422,6 +423,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(RuntimeError, repr, d)
+
     def test_le(self):
         self.assertFalse({} < {})
         self.assertFalse({1: 2} < {1L: 2L})
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 bb65e98714eea16e6094b71b2d94eb724d34fc1d..24eff769c64e700ef261c076dbf9524db92610b0 100644 (file)
@@ -383,10 +383,7 @@ list_repr(PyListObject *v)
        so must refetch the list size on each iteration. */
     for (i = 0; i < Py_SIZE(v); ++i) {
         int status;
-        if (Py_EnterRecursiveCall(" while getting the repr of a list"))
-            goto Done;
         s = PyObject_Repr(v->ob_item[i]);
-        Py_LeaveRecursiveCall();
         if (s == NULL)
             goto Done;
         status = PyList_Append(pieces, s);
index 7a2821897999d756e795e3ae27f39204937b57db..65366b0b351b4c049690f507a353711f44cdecf5 100644 (file)
@@ -378,7 +378,12 @@ PyObject_Repr(PyObject *v)
                                    Py_TYPE(v)->tp_name, v);
     else {
         PyObject *res;
+        /* 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 = (*Py_TYPE(v)->tp_repr)(v);
+        Py_LeaveRecursiveCall();
         if (res == NULL)
             return NULL;
 #ifdef Py_USING_UNICODE
index a61c8aa013b4c80d569e6807c85006e77c110859..6f4b18cc5c65a848c689c450da506d801682cae2 100644 (file)
@@ -288,10 +288,7 @@ tuplerepr(PyTupleObject *v)
 
     /* Do repr() on each element. */
     for (i = 0; i < n; ++i) {
-        if (Py_EnterRecursiveCall(" while getting the repr of a tuple"))
-            goto Done;
         s = PyObject_Repr(v->ob_item[i]);
-        Py_LeaveRecursiveCall();
         if (s == NULL)
             goto Done;
         PyTuple_SET_ITEM(pieces, i, s);