Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 53d36b6

Browse files
committed
Issue #16228: Fix a crash in the json module where a list changes size while it is being encoded.
Patch by Serhiy Storchaka.
2 parents 1511a5a + 5ebe65f commit 53d36b6

3 files changed

Lines changed: 14 additions & 7 deletions

File tree

Lib/test/json_tests/test_dump.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ def test_encode_truefalse(self):
2020
{2: 3.0, 4.0: 5, False: 1, 6: True}, sort_keys=True),
2121
'{"false": 1, "2": 3.0, "4.0": 5, "6": true}')
2222

23+
# Issue 16228: Crash on encoding resized list
24+
def test_encode_mutated(self):
25+
a = [object()] * 10
26+
def crasher(obj):
27+
del a[-1]
28+
self.assertEqual(self.dumps(a, default=crasher),
29+
'[null, null, null, null, null]')
30+
2331

2432
class TestPyDump(TestDump, PyTest): pass
2533

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ Core and Builtins
8585
Library
8686
-------
8787

88+
- Issue #16228: Fix a crash in the json module where a list changes size
89+
while it is being encoded. Patch by Serhiy Storchaka.
90+
8891
- Issue #16351: New function gc.get_stats() returns per-generation collection
8992
statistics.
9093

Modules/_json.c

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1659,8 +1659,6 @@ encoder_listencode_list(PyEncoderObject *s, _PyAccu *acc,
16591659
static PyObject *empty_array = NULL;
16601660
PyObject *ident = NULL;
16611661
PyObject *s_fast = NULL;
1662-
Py_ssize_t num_items;
1663-
PyObject **seq_items;
16641662
Py_ssize_t i;
16651663

16661664
if (open_array == NULL || close_array == NULL || empty_array == NULL) {
@@ -1674,8 +1672,7 @@ encoder_listencode_list(PyEncoderObject *s, _PyAccu *acc,
16741672
s_fast = PySequence_Fast(seq, "_iterencode_list needs a sequence");
16751673
if (s_fast == NULL)
16761674
return -1;
1677-
num_items = PySequence_Fast_GET_SIZE(s_fast);
1678-
if (num_items == 0) {
1675+
if (PySequence_Fast_GET_SIZE(s_fast) == 0) {
16791676
Py_DECREF(s_fast);
16801677
return _PyAccu_Accumulate(acc, empty_array);
16811678
}
@@ -1696,7 +1693,6 @@ encoder_listencode_list(PyEncoderObject *s, _PyAccu *acc,
16961693
}
16971694
}
16981695

1699-
seq_items = PySequence_Fast_ITEMS(s_fast);
17001696
if (_PyAccu_Accumulate(acc, open_array))
17011697
goto bail;
17021698
if (s->indent != Py_None) {
@@ -1708,8 +1704,8 @@ encoder_listencode_list(PyEncoderObject *s, _PyAccu *acc,
17081704
buf += newline_indent
17091705
*/
17101706
}
1711-
for (i = 0; i < num_items; i++) {
1712-
PyObject *obj = seq_items[i];
1707+
for (i = 0; i < PySequence_Fast_GET_SIZE(s_fast); i++) {
1708+
PyObject *obj = PySequence_Fast_GET_ITEM(s_fast, i);
17131709
if (i) {
17141710
if (_PyAccu_Accumulate(acc, s->item_separator))
17151711
goto bail;

0 commit comments

Comments
 (0)