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

Skip to content

Commit 1367265

Browse files
committed
#12051: Fix segfault in json.dumps() while encoding highly-nested objects using the C accelerations.
1 parent ee18b6f commit 1367265

3 files changed

Lines changed: 38 additions & 3 deletions

File tree

Lib/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
@@ -326,6 +326,9 @@ Library
326326
Extension Modules
327327
-----------------
328328

329+
- Issue #12051: Fix segfault in json.dumps() while encoding highly-nested
330+
objects using the C accelerations.
331+
329332
- Issue #12017: Fix segfault in json.loads() while decoding highly-nested
330333
objects using the C accelerations.
331334

Modules/_json.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,10 +1301,18 @@ encoder_listencode_obj(PyEncoderObject *s, PyObject *rval, PyObject *obj, Py_ssi
13011301
return _steal_list_append(rval, encoded);
13021302
}
13031303
else if (PyList_Check(obj) || PyTuple_Check(obj)) {
1304-
return encoder_listencode_list(s, rval, obj, indent_level);
1304+
if (Py_EnterRecursiveCall(" while encoding a JSON object"))
1305+
return -1;
1306+
rv = encoder_listencode_list(s, rval, obj, indent_level);
1307+
Py_LeaveRecursiveCall();
1308+
return rv;
13051309
}
13061310
else if (PyDict_Check(obj)) {
1307-
return encoder_listencode_dict(s, rval, obj, indent_level);
1311+
if (Py_EnterRecursiveCall(" while encoding a JSON object"))
1312+
return -1;
1313+
rv = encoder_listencode_dict(s, rval, obj, indent_level);
1314+
Py_LeaveRecursiveCall();
1315+
return rv;
13081316
}
13091317
else {
13101318
PyObject *ident = NULL;
@@ -1330,7 +1338,12 @@ encoder_listencode_obj(PyEncoderObject *s, PyObject *rval, PyObject *obj, Py_ssi
13301338
Py_XDECREF(ident);
13311339
return -1;
13321340
}
1341+
1342+
if (Py_EnterRecursiveCall(" while encoding a JSON object"))
1343+
return -1;
13331344
rv = encoder_listencode_obj(s, rval, newobj, indent_level);
1345+
Py_LeaveRecursiveCall();
1346+
13341347
Py_DECREF(newobj);
13351348
if (rv) {
13361349
Py_XDECREF(ident);

0 commit comments

Comments
 (0)