summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2012-11-01 19:52:06 +0100
committerAntoine Pitrou <solipsis@pitrou.net>2012-11-01 19:52:06 +0100
commit9d64b9860c4c99c160a02aa1fb575fd9f42264ca (patch)
tree6e4882db54a12852557b142f7f5d560f62038000
parentf861ed6575d72bde017cdccc0b4523ab30dc1e28 (diff)
downloadcpython-9d64b9860c4c99c160a02aa1fb575fd9f42264ca.tar.gz
Issue #16228: Fix a crash in the json module where a list changes size while it is being encoded.
Patch by Serhiy Storchaka.
-rw-r--r--Lib/test/json_tests/test_dump.py8
-rw-r--r--Misc/NEWS3
-rw-r--r--Modules/_json.c10
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
index 083c11f08c..fee972eac4 100644
--- a/Lib/test/json_tests/test_dump.py
+++ b/Lib/test/json_tests/test_dump.py
@@ -19,6 +19,14 @@ class TestDump:
{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
class TestCDump(TestDump, CTest): pass
diff --git a/Misc/NEWS b/Misc/NEWS
index 9a18c13f6a..7ece3120b8 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -143,6 +143,9 @@ Core and Builtins
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 #14897: Enhance error messages of struct.pack and
struct.pack_into. Patch by Matti Mäki.
diff --git a/Modules/_json.c b/Modules/_json.c
index 2a71b9fda9..01436b6950 100644
--- a/Modules/_json.c
+++ b/Modules/_json.c
@@ -1585,8 +1585,6 @@ encoder_listencode_list(PyEncoderObject *s, PyObject *rval, PyObject *seq, Py_ss
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) {
@@ -1600,8 +1598,7 @@ encoder_listencode_list(PyEncoderObject *s, PyObject *rval, PyObject *seq, Py_ss
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 PyList_Append(rval, empty_array);
}
@@ -1622,7 +1619,6 @@ encoder_listencode_list(PyEncoderObject *s, PyObject *rval, PyObject *seq, Py_ss
}
}
- seq_items = PySequence_Fast_ITEMS(s_fast);
if (PyList_Append(rval, open_array))
goto bail;
if (s->indent != Py_None) {
@@ -1634,8 +1630,8 @@ encoder_listencode_list(PyEncoderObject *s, PyObject *rval, PyObject *seq, Py_ss
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 (PyList_Append(rval, s->item_separator))
goto bail;