Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 06383ee

Browse files
committed
#12017: merge with 3.1.
2 parents b0b0579 + 362b951 commit 06383ee

3 files changed

Lines changed: 30 additions & 4 deletions

File tree

Lib/test/json_tests/test_recursion.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,15 @@ def test_defaultrecursion(self):
6565
pass
6666
else:
6767
self.fail("didn't raise ValueError on default recursion")
68+
69+
70+
def test_highly_nested_objects(self):
71+
# test that loading highly-nested objects doesn't segfault when C
72+
# accelerations are used. See #12017
73+
with self.assertRaises(RuntimeError):
74+
json.loads('{"a":' * 100000 + '1' + '}' * 100000)
75+
with self.assertRaises(RuntimeError):
76+
json.loads('{"a":' * 100000 + '[1]' + '}' * 100000)
77+
with self.assertRaises(RuntimeError):
78+
json.loads('[' * 100000 + '1' + ']' * 100000)
79+

Misc/NEWS

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,8 +324,11 @@ Tools/Demos
324324

325325
- Issue #11179: Make ccbench work under Python 3.1 and 2.7 again.
326326

327-
Extensions
328-
----------
327+
Extension Modules
328+
-----------------
329+
330+
- Issue #12017: Fix segfault in json.loads() while decoding highly-nested
331+
objects using the C accelerations.
329332

330333
- Issue #1838: Prevent segfault in ctypes, when _as_parameter_ on a class is set
331334
to an instance of the class.

Modules/_json.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -927,6 +927,7 @@ scan_once_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_
927927
928928
Returns a new PyObject representation of the term.
929929
*/
930+
PyObject *res;
930931
Py_UNICODE *str = PyUnicode_AS_UNICODE(pystr);
931932
Py_ssize_t length = PyUnicode_GET_SIZE(pystr);
932933
if (idx >= length) {
@@ -941,10 +942,20 @@ scan_once_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_
941942
next_idx_ptr);
942943
case '{':
943944
/* object */
944-
return _parse_object_unicode(s, pystr, idx + 1, next_idx_ptr);
945+
if (Py_EnterRecursiveCall(" while decoding a JSON object "
946+
"from a unicode string"))
947+
return NULL;
948+
res = _parse_object_unicode(s, pystr, idx + 1, next_idx_ptr);
949+
Py_LeaveRecursiveCall();
950+
return res;
945951
case '[':
946952
/* array */
947-
return _parse_array_unicode(s, pystr, idx + 1, next_idx_ptr);
953+
if (Py_EnterRecursiveCall(" while decoding a JSON array "
954+
"from a unicode string"))
955+
return NULL;
956+
res = _parse_array_unicode(s, pystr, idx + 1, next_idx_ptr);
957+
Py_LeaveRecursiveCall();
958+
return res;
948959
case 'n':
949960
/* null */
950961
if ((idx + 3 < length) && str[idx + 1] == 'u' && str[idx + 2] == 'l' && str[idx + 3] == 'l') {

0 commit comments

Comments
 (0)