[Python-checkins] cpython (3.1): #12017: Fix segfault in json.loads() while decoding highly-nested objects using

ezio.melotti python-checkins at python.org
Sat May 7 17:29:27 CEST 2011


http://hg.python.org/cpython/rev/61164d09337e
changeset: 69910:61164d09337e
branch: 3.1
parent: 69899:c32b3aceeb84
user: Ezio Melotti <ezio.melotti at gmail.com>
date: Sat May 07 17:58:09 2011 +0300
summary:
 #12017: Fix segfault in json.loads() while decoding highly-nested objects using the C accelerations.
files:
 Lib/json/tests/test_recursion.py | 12 ++++++++++++
 Misc/NEWS | 3 +++
 Modules/_json.c | 15 +++++++++++++--
 3 files changed, 28 insertions(+), 2 deletions(-)
diff --git a/Lib/json/tests/test_recursion.py b/Lib/json/tests/test_recursion.py
--- a/Lib/json/tests/test_recursion.py
+++ b/Lib/json/tests/test_recursion.py
@@ -65,3 +65,15 @@
 pass
 else:
 self.fail("didn't raise ValueError on default recursion")
+
+
+ def test_highly_nested_objects(self):
+ # test that loading highly-nested objects doesn't segfault when C
+ # accelerations are used. See #12017
+ with self.assertRaises(RuntimeError):
+ json.loads('{"a":' * 100000 + '1' + '}' * 100000)
+ with self.assertRaises(RuntimeError):
+ json.loads('{"a":' * 100000 + '[1]' + '}' * 100000)
+ with self.assertRaises(RuntimeError):
+ json.loads('[' * 100000 + '1' + ']' * 100000)
+
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -314,6 +314,9 @@
 Extensions
 ----------
 
+- Issue #12017: Fix segfault in json.loads() while decoding highly-nested
+ objects using the C accelerations.
+
 - Issue #1838: Prevent segfault in ctypes, when _as_parameter_ on a class is set
 to an instance of the class.
 
diff --git a/Modules/_json.c b/Modules/_json.c
--- a/Modules/_json.c
+++ b/Modules/_json.c
@@ -899,6 +899,7 @@
 
 Returns a new PyObject representation of the term.
 */
+ PyObject *res;
 Py_UNICODE *str = PyUnicode_AS_UNICODE(pystr);
 Py_ssize_t length = PyUnicode_GET_SIZE(pystr);
 if (idx >= length) {
@@ -913,10 +914,20 @@
 next_idx_ptr);
 case '{':
 /* object */
- return _parse_object_unicode(s, pystr, idx + 1, next_idx_ptr);
+ if (Py_EnterRecursiveCall(" while decoding a JSON object "
+ "from a unicode string"))
+ return NULL;
+ res = _parse_object_unicode(s, pystr, idx + 1, next_idx_ptr);
+ Py_LeaveRecursiveCall();
+ return res;
 case '[':
 /* array */
- return _parse_array_unicode(s, pystr, idx + 1, next_idx_ptr);
+ if (Py_EnterRecursiveCall(" while decoding a JSON array "
+ "from a unicode string"))
+ return NULL;
+ res = _parse_array_unicode(s, pystr, idx + 1, next_idx_ptr);
+ Py_LeaveRecursiveCall();
+ return res;
 case 'n':
 /* null */
 if ((idx + 3 < length) && str[idx + 1] == 'u' && str[idx + 2] == 'l' && str[idx + 3] == 'l') {
-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /