[Python-checkins] cpython (merge 3.3 -> default): Issue #16228: Fix a crash in the json module where a list changes size while it

antoine.pitrou python-checkins at python.org
Thu Nov 1 20:04:10 CET 2012


http://hg.python.org/cpython/rev/ab65509b8443
changeset:   80159:ab65509b8443
parent:      80156:1267d64c14b3
parent:      80158:7528c02b8d52
user:        Antoine Pitrou <solipsis at pitrou.net>
date:        Thu Nov 01 20:03:30 2012 +0100
summary:
  Issue #16228: Fix a crash in the json module where a list changes size while it is being encoded.
Patch by Serhiy Storchaka.

files:
  Lib/test/json_tests/test_dump.py |   8 ++++++++
  Misc/NEWS                        |   3 +++
  Modules/_json.c                  |  10 +++-------
  3 files changed, 14 insertions(+), 7 deletions(-)


diff --git a/Lib/test/json_tests/test_dump.py b/Lib/test/json_tests/test_dump.py
--- a/Lib/test/json_tests/test_dump.py
+++ b/Lib/test/json_tests/test_dump.py
@@ -20,6 +20,14 @@
                 {2: 3.0, 4.0: 5, False: 1, 6: True}, sort_keys=True),
                 '{"false": 1, "2": 3.0, "4.0": 5, "6": true}')
 
+    # Issue 16228: Crash on encoding resized list
+    def test_encode_mutated(self):
+        a = [object()] * 10
+        def crasher(obj):
+            del a[-1]
+        self.assertEqual(self.dumps(a, default=crasher),
+                 '[null, null, null, null, null]')
+
 
 class TestPyDump(TestDump, PyTest): pass
 
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -85,6 +85,9 @@
 Library
 -------
 
+- Issue #16228: Fix a crash in the json module where a list changes size
+  while it is being encoded.  Patch by Serhiy Storchaka.
+
 - Issue #16351: New function gc.get_stats() returns per-generation collection
   statistics.
 
diff --git a/Modules/_json.c b/Modules/_json.c
--- a/Modules/_json.c
+++ b/Modules/_json.c
@@ -1659,8 +1659,6 @@
     static PyObject *empty_array = NULL;
     PyObject *ident = NULL;
     PyObject *s_fast = NULL;
-    Py_ssize_t num_items;
-    PyObject **seq_items;
     Py_ssize_t i;
 
     if (open_array == NULL || close_array == NULL || empty_array == NULL) {
@@ -1674,8 +1672,7 @@
     s_fast = PySequence_Fast(seq, "_iterencode_list needs a sequence");
     if (s_fast == NULL)
         return -1;
-    num_items = PySequence_Fast_GET_SIZE(s_fast);
-    if (num_items == 0) {
+    if (PySequence_Fast_GET_SIZE(s_fast) == 0) {
         Py_DECREF(s_fast);
         return _PyAccu_Accumulate(acc, empty_array);
     }
@@ -1696,7 +1693,6 @@
         }
     }
 
-    seq_items = PySequence_Fast_ITEMS(s_fast);
     if (_PyAccu_Accumulate(acc, open_array))
         goto bail;
     if (s->indent != Py_None) {
@@ -1708,8 +1704,8 @@
             buf += newline_indent
         */
     }
-    for (i = 0; i < num_items; i++) {
-        PyObject *obj = seq_items[i];
+    for (i = 0; i < PySequence_Fast_GET_SIZE(s_fast); i++) {
+        PyObject *obj = PySequence_Fast_GET_ITEM(s_fast, i);
         if (i) {
             if (_PyAccu_Accumulate(acc, s->item_separator))
                 goto bail;

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list