]> granicus.if.org Git - python/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:58:09 +0000 (17:58 +0300)
committerEzio Melotti <ezio.melotti@gmail.com>
Sat, 7 May 2011 14:58:09 +0000 (17:58 +0300)
Lib/json/tests/test_recursion.py
Misc/NEWS
Modules/_json.c

index 1e9b8ab757f3529a96872fc8976e8f821a834735..6d5db5050eebe6e7956935acb09cebfe4c24a3ba 100644 (file)
@@ -65,3 +65,15 @@ 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
+        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)
+
index 2e31693975ba39f736ed69013e3508e4bb1a43d9..fddaf099c4ccb33a0079f87690dd2a5115aa7a50 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -314,6 +314,9 @@ Library
 Extensions
 ----------
 
+- 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 4da5e74dc26c3448634da4c415a0b4b0084366a7..7a995a530a0a301e3aa5ff5e6845c8592984f7a5 100644 (file)
@@ -899,6 +899,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) {
@@ -913,10 +914,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') {