summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEzio Melotti <ezio.melotti@gmail.com>2011-05-11 00:55:35 +0300
committerEzio Melotti <ezio.melotti@gmail.com>2011-05-11 00:55:35 +0300
commite6fa5704a39e757702245eda7a6266c80955b2b2 (patch)
treeeeafe3abe2c99ef18661dd6cb0f659a34986bab3
parent32bb603da8b576d1fdfdd3ccebe91cf7dc5d3963 (diff)
downloadcpython-e6fa5704a39e757702245eda7a6266c80955b2b2.tar.gz
#12051: Fix segfault in json.dumps() while encoding highly-nested objects using the C accelerations.
-rw-r--r--Lib/json/tests/test_recursion.py22
-rw-r--r--Misc/NEWS3
-rw-r--r--Modules/_json.c17
3 files changed, 39 insertions, 3 deletions
diff --git a/Lib/json/tests/test_recursion.py b/Lib/json/tests/test_recursion.py
index 548bb89ed5..1f2d7b3b5c 100644
--- a/Lib/json/tests/test_recursion.py
+++ b/Lib/json/tests/test_recursion.py
@@ -16,6 +16,11 @@ class RecursiveJSONEncoder(json.JSONEncoder):
return 'JSONTestObject'
return json.JSONEncoder.default(o)
+class EndlessJSONEncoder(json.JSONEncoder):
+ def default(self, o):
+ """If check_circular is False, this will keep adding another list."""
+ return [o]
+
class TestRecursion(TestCase):
def test_listrecursion(self):
@@ -67,7 +72,7 @@ class TestRecursion(TestCase):
self.fail("didn't raise ValueError on default recursion")
- def test_highly_nested_objects(self):
+ def test_highly_nested_objects_decoding(self):
# test that loading highly-nested objects doesn't segfault when C
# accelerations are used. See #12017
# str
@@ -84,3 +89,18 @@ class TestRecursion(TestCase):
json.loads(u'{"a":' * 100000 + u'[1]' + u'}' * 100000)
with self.assertRaises(RuntimeError):
json.loads(u'[' * 100000 + u'1' + u']' * 100000)
+
+ def test_highly_nested_objects_encoding(self):
+ # See #12051
+ l, d = [], {}
+ for x in xrange(100000):
+ l, d = [l], {'k':d}
+ with self.assertRaises(RuntimeError):
+ json.dumps(l)
+ with self.assertRaises(RuntimeError):
+ json.dumps(d)
+
+ def test_endless_recursion(self):
+ # See #12051
+ with self.assertRaises(RuntimeError):
+ EndlessJSONEncoder(check_circular=False).encode(5j)
diff --git a/Misc/NEWS b/Misc/NEWS
index 7517baf648..90f36a181e 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -373,6 +373,9 @@ Extension Modules
- Issue #10169: Fix argument parsing in socket.sendto() to avoid error masking.
+- Issue #12051: Fix segfault in json.dumps() while encoding highly-nested
+ objects using the C accelerations.
+
- Issue #12017: Fix segfault in json.loads() while decoding highly-nested
objects using the C accelerations.
diff --git a/Modules/_json.c b/Modules/_json.c
index 4b4ef89282..6b321e57f5 100644
--- a/Modules/_json.c
+++ b/Modules/_json.c
@@ -1999,10 +1999,18 @@ encoder_listencode_obj(PyEncoderObject *s, PyObject *rval, PyObject *obj, Py_ssi
return _steal_list_append(rval, encoded);
}
else if (PyList_Check(obj) || PyTuple_Check(obj)) {
- return encoder_listencode_list(s, rval, obj, indent_level);
+ if (Py_EnterRecursiveCall(" while encoding a JSON object"))
+ return -1;
+ rv = encoder_listencode_list(s, rval, obj, indent_level);
+ Py_LeaveRecursiveCall();
+ return rv;
}
else if (PyDict_Check(obj)) {
- return encoder_listencode_dict(s, rval, obj, indent_level);
+ if (Py_EnterRecursiveCall(" while encoding a JSON object"))
+ return -1;
+ rv = encoder_listencode_dict(s, rval, obj, indent_level);
+ Py_LeaveRecursiveCall();
+ return rv;
}
else {
PyObject *ident = NULL;
@@ -2028,7 +2036,12 @@ encoder_listencode_obj(PyEncoderObject *s, PyObject *rval, PyObject *obj, Py_ssi
Py_XDECREF(ident);
return -1;
}
+
+ if (Py_EnterRecursiveCall(" while encoding a JSON object"))
+ return -1;
rv = encoder_listencode_obj(s, rval, newobj, indent_level);
+ Py_LeaveRecursiveCall();
+
Py_DECREF(newobj);
if (rv) {
Py_XDECREF(ident);