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

Skip to content

Commit f188bc5

Browse files
committed
#12051: merge with 3.1.
2 parents ccc87b5 + 1367265 commit f188bc5

3 files changed

Lines changed: 38 additions & 3 deletions

File tree

Lib/test/json_tests/test_recursion.py

Lines changed: 20 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):
@@ -77,3 +82,17 @@ def test_highly_nested_objects(self):
7782
with self.assertRaises(RuntimeError):
7883
json.loads('[' * 100000 + '1' + ']' * 100000)
7984

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
@@ -342,6 +342,9 @@ Tools/Demos
342342
Extension Modules
343343
-----------------
344344

345+
- Issue #12051: Fix segfault in json.dumps() while encoding highly-nested
346+
objects using the C accelerations.
347+
345348
- Issue #12017: Fix segfault in json.loads() while decoding highly-nested
346349
objects using the C accelerations.
347350

Modules/_json.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1338,10 +1338,18 @@ encoder_listencode_obj(PyEncoderObject *s, PyObject *rval, PyObject *obj, Py_ssi
13381338
return _steal_list_append(rval, encoded);
13391339
}
13401340
else if (PyList_Check(obj) || PyTuple_Check(obj)) {
1341-
return encoder_listencode_list(s, rval, obj, indent_level);
1341+
if (Py_EnterRecursiveCall(" while encoding a JSON object"))
1342+
return -1;
1343+
rv = encoder_listencode_list(s, rval, obj, indent_level);
1344+
Py_LeaveRecursiveCall();
1345+
return rv;
13421346
}
13431347
else if (PyDict_Check(obj)) {
1344-
return encoder_listencode_dict(s, rval, obj, indent_level);
1348+
if (Py_EnterRecursiveCall(" while encoding a JSON object"))
1349+
return -1;
1350+
rv = encoder_listencode_dict(s, rval, obj, indent_level);
1351+
Py_LeaveRecursiveCall();
1352+
return rv;
13451353
}
13461354
else {
13471355
PyObject *ident = NULL;
@@ -1367,7 +1375,12 @@ encoder_listencode_obj(PyEncoderObject *s, PyObject *rval, PyObject *obj, Py_ssi
13671375
Py_XDECREF(ident);
13681376
return -1;
13691377
}
1378+
1379+
if (Py_EnterRecursiveCall(" while encoding a JSON object"))
1380+
return -1;
13701381
rv = encoder_listencode_obj(s, rval, newobj, indent_level);
1382+
Py_LeaveRecursiveCall();
1383+
13711384
Py_DECREF(newobj);
13721385
if (rv) {
13731386
Py_XDECREF(ident);

0 commit comments

Comments
 (0)