]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
#12017: Fix segfault in json.loads() while decoding highly-nested objects using the...
authorEzio Melotti <ezio.melotti@gmail.com>
Sat, 7 May 2011 14:40:23 +0000 (17:40 +0300)
committerEzio Melotti <ezio.melotti@gmail.com>
Sat, 7 May 2011 14:40:23 +0000 (17:40 +0300)
Lib/json/tests/test_recursion.py
Misc/NEWS
Modules/_json.c

index 1e9b8ab757f3529a96872fc8976e8f821a834735..548bb89ed5935cf6ec6dfa683b546004f1028494 100644 (file)
@@ -65,3 +65,22 @@ class TestRecursion(TestCase):
             pass
         else:
             self.fail("didn't raise ValueError on default recursion")
+
+
+    def test_highly_nested_objects(self):
+        # test that loading highly-nested objects doesn't segfault when C
+        # accelerations are used. See #12017
+        # str
+        with self.assertRaises(RuntimeError):
+            json.loads('{"a":' * 100000 + '1' + '}' * 100000)
+        with self.assertRaises(RuntimeError):
+            json.loads('{"a":' * 100000 + '[1]' + '}' * 100000)
+        with self.assertRaises(RuntimeError):
+            json.loads('[' * 100000 + '1' + ']' * 100000)
+        # unicode
+        with self.assertRaises(RuntimeError):
+            json.loads(u'{"a":' * 100000 + u'1' + u'}' * 100000)
+        with self.assertRaises(RuntimeError):
+            json.loads(u'{"a":' * 100000 + u'[1]' + u'}' * 100000)
+        with self.assertRaises(RuntimeError):
+            json.loads(u'[' * 100000 + u'1' + u']' * 100000)
index 7207f4b919d46c5eafa3bc806b7b1892802e49da..8c760576a6837d7f515ecddf47bb2cd8ba50a1dc 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -358,6 +358,9 @@ Library
 Extension Modules
 -----------------
 
+- Issue #12017: Fix segfault in json.loads() while decoding highly-nested
+  objects using the C accelerations.
+
 - Issue #1838: Prevent segfault in ctypes, when _as_parameter_ on a class is set
   to an instance of the class.
 
index 7a1fabdba151769536a035d8a44a55485606ac80..4b4ef892825c0b915f9b63c5238811c7dfd3ae2f 100644 (file)
@@ -1488,6 +1488,7 @@ scan_once_str(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_t *n
 
     Returns a new PyObject representation of the term.
     */
+    PyObject *res;
     char *str = PyString_AS_STRING(pystr);
     Py_ssize_t length = PyString_GET_SIZE(pystr);
     if (idx >= length) {
@@ -1503,10 +1504,20 @@ scan_once_str(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_t *n
                 next_idx_ptr);
         case '{':
             /* object */
-            return _parse_object_str(s, pystr, idx + 1, next_idx_ptr);
+            if (Py_EnterRecursiveCall(" while decoding a JSON object "
+                                      "from a byte string"))
+                return NULL;
+            res = _parse_object_str(s, pystr, idx + 1, next_idx_ptr);
+            Py_LeaveRecursiveCall();
+            return res;
         case '[':
             /* array */
-            return _parse_array_str(s, pystr, idx + 1, next_idx_ptr);
+            if (Py_EnterRecursiveCall(" while decoding a JSON array "
+                                      "from a byte string"))
+                return NULL;
+            res = _parse_array_str(s, pystr, idx + 1, next_idx_ptr);
+            Py_LeaveRecursiveCall();
+            return res;
         case 'n':
             /* null */
             if ((idx + 3 < length) && str[idx + 1] == 'u' && str[idx + 2] == 'l' && str[idx + 3] == 'l') {
@@ -1564,6 +1575,7 @@ scan_once_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_
 
     Returns a new PyObject representation of the term.
     */
+    PyObject *res;
     Py_UNICODE *str = PyUnicode_AS_UNICODE(pystr);
     Py_ssize_t length = PyUnicode_GET_SIZE(pystr);
     if (idx >= length) {
@@ -1578,10 +1590,20 @@ scan_once_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_
                 next_idx_ptr);
         case '{':
             /* object */
-            return _parse_object_unicode(s, pystr, idx + 1, next_idx_ptr);
+            if (Py_EnterRecursiveCall(" while decoding a JSON object "
+                                      "from a unicode string"))
+                return NULL;
+            res = _parse_object_unicode(s, pystr, idx + 1, next_idx_ptr);
+            Py_LeaveRecursiveCall();
+            return res;
         case '[':
             /* array */
-            return _parse_array_unicode(s, pystr, idx + 1, next_idx_ptr);
+            if (Py_EnterRecursiveCall(" while decoding a JSON array "
+                                      "from a unicode string"))
+                return NULL;
+            res = _parse_array_unicode(s, pystr, idx + 1, next_idx_ptr);
+            Py_LeaveRecursiveCall();
+            return res;
         case 'n':
             /* null */
             if ((idx + 3 < length) && str[idx + 1] == 'u' && str[idx + 2] == 'l' && str[idx + 3] == 'l') {