From: Ezio Melotti Date: Tue, 10 May 2011 22:02:56 +0000 (+0300) Subject: #12051: Fix segfault in json.dumps() while encoding highly-nested objects using the... X-Git-Tag: v3.2.1rc1~28^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=136726537ffd92b0f3e8dabb23023ed514852e2e;p=thirdparty%2FPython%2Fcpython.git #12051: Fix segfault in json.dumps() while encoding highly-nested objects using the C accelerations. --- diff --git a/Lib/json/tests/test_recursion.py b/Lib/json/tests/test_recursion.py index 6d5db5050eeb..ab5f213711fb 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 with self.assertRaises(RuntimeError): @@ -77,3 +82,17 @@ class TestRecursion(TestCase): with self.assertRaises(RuntimeError): json.loads('[' * 100000 + '1' + ']' * 100000) + def test_highly_nested_objects_encoding(self): + # See #12051 + l, d = [], {} + for x in range(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 9f4d9a8b7ae0..cdb63bed6fc1 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -326,6 +326,9 @@ Library Extension Modules ----------------- +- 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 7a995a530a0a..5ced5c970457 100644 --- a/Modules/_json.c +++ b/Modules/_json.c @@ -1301,10 +1301,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; @@ -1330,7 +1338,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);