diff -r 051190230254 Lib/json/tests/test_recursion.py --- a/Lib/json/tests/test_recursion.py Fri May 06 11:31:33 2011 +0200 +++ b/Lib/json/tests/test_recursion.py Fri May 06 19:03:08 2011 +0300 @@ -65,3 +65,22 @@ 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 + self.assertRaises(RuntimeError, + json.loads, '{"a":' * 100000 + '1' + '}' * 100000) + self.assertRaises(RuntimeError, + json.loads, '{"a":' * 100000 + '[1]' + '}' * 100000) + self.assertRaises(RuntimeError, + json.loads, '[' * 100000 + '1' + ']' * 100000) + # unicode + self.assertRaises(RuntimeError, + json.loads, u'{"a":' * 100000 + u'1' + u'}' * 100000) + self.assertRaises(RuntimeError, + json.loads, u'{"a":' * 100000 + u'[1]' + u'}' * 100000) + self.assertRaises(RuntimeError, + json.loads, u'[' * 100000 + u'1' + u']' * 100000) diff -r 051190230254 Modules/_json.c --- a/Modules/_json.c Fri May 06 11:31:33 2011 +0200 +++ b/Modules/_json.c Fri May 06 19:03:08 2011 +0300 @@ -1488,6 +1488,7 @@ 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 @@ 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 @@ 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 @@ 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') {