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

Skip to content

Commit f8d6fd6

Browse files
committed
#12051: merge with 3.2.
2 parents 1947477 + f188bc5 commit f8d6fd6

3 files changed

Lines changed: 39 additions & 3 deletions

File tree

Lib/test/json_tests/test_recursion.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ def default(self, o):
1616
return 'JSONTestObject'
1717
return json.JSONEncoder.default(o)
1818

19+
class EndlessJSONEncoder(json.JSONEncoder):
20+
def default(self, o):
21+
"""If check_circular is False, this will keep adding another list."""
22+
return [o]
23+
1924

2025
class TestRecursion(TestCase):
2126
def test_listrecursion(self):
@@ -67,7 +72,7 @@ def test_defaultrecursion(self):
6772
self.fail("didn't raise ValueError on default recursion")
6873

6974

70-
def test_highly_nested_objects(self):
75+
def test_highly_nested_objects_decoding(self):
7176
# test that loading highly-nested objects doesn't segfault when C
7277
# accelerations are used. See #12017
7378
with self.assertRaises(RuntimeError):
@@ -76,3 +81,18 @@ def test_highly_nested_objects(self):
7681
json.loads('{"a":' * 100000 + '[1]' + '}' * 100000)
7782
with self.assertRaises(RuntimeError):
7883
json.loads('[' * 100000 + '1' + ']' * 100000)
84+
85+
def test_highly_nested_objects_encoding(self):
86+
# See #12051
87+
l, d = [], {}
88+
for x in range(100000):
89+
l, d = [l], {'k':d}
90+
with self.assertRaises(RuntimeError):
91+
json.dumps(l)
92+
with self.assertRaises(RuntimeError):
93+
json.dumps(d)
94+
95+
def test_endless_recursion(self):
96+
# See #12051
97+
with self.assertRaises(RuntimeError):
98+
EndlessJSONEncoder(check_circular=False).encode(5j)

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,9 @@ Tools/Demos
624624
Extension Modules
625625
-----------------
626626

627+
- Issue #12051: Fix segfault in json.dumps() while encoding highly-nested
628+
objects using the C accelerations.
629+
627630
- Issue #12017: Fix segfault in json.loads() while decoding highly-nested
628631
objects using the C accelerations.
629632

Modules/_json.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1354,10 +1354,18 @@ encoder_listencode_obj(PyEncoderObject *s, PyObject *rval, PyObject *obj, Py_ssi
13541354
return _steal_list_append(rval, encoded);
13551355
}
13561356
else if (PyList_Check(obj) || PyTuple_Check(obj)) {
1357-
return encoder_listencode_list(s, rval, obj, indent_level);
1357+
if (Py_EnterRecursiveCall(" while encoding a JSON object"))
1358+
return -1;
1359+
rv = encoder_listencode_list(s, rval, obj, indent_level);
1360+
Py_LeaveRecursiveCall();
1361+
return rv;
13581362
}
13591363
else if (PyDict_Check(obj)) {
1360-
return encoder_listencode_dict(s, rval, obj, indent_level);
1364+
if (Py_EnterRecursiveCall(" while encoding a JSON object"))
1365+
return -1;
1366+
rv = encoder_listencode_dict(s, rval, obj, indent_level);
1367+
Py_LeaveRecursiveCall();
1368+
return rv;
13611369
}
13621370
else {
13631371
PyObject *ident = NULL;
@@ -1383,7 +1391,12 @@ encoder_listencode_obj(PyEncoderObject *s, PyObject *rval, PyObject *obj, Py_ssi
13831391
Py_XDECREF(ident);
13841392
return -1;
13851393
}
1394+
1395+
if (Py_EnterRecursiveCall(" while encoding a JSON object"))
1396+
return -1;
13861397
rv = encoder_listencode_obj(s, rval, newobj, indent_level);
1398+
Py_LeaveRecursiveCall();
1399+
13871400
Py_DECREF(newobj);
13881401
if (rv) {
13891402
Py_XDECREF(ident);

0 commit comments

Comments
 (0)