Skip to content

Commit ba6cf2d

Browse files
committed
gh-149816: Check for changed list size in Modules/_pickle.c
1 parent 671f082 commit ba6cf2d

1 file changed

Lines changed: 11 additions & 3 deletions

File tree

Modules/_pickle.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3179,7 +3179,7 @@ static int
31793179
batch_list_exact(PickleState *state, PicklerObject *self, PyObject *obj)
31803180
{
31813181
PyObject *item = NULL;
3182-
Py_ssize_t this_batch, total;
3182+
Py_ssize_t this_batch, total, list_size;
31833183

31843184
const char append_op = APPEND;
31853185
const char appends_op = APPENDS;
@@ -3189,10 +3189,12 @@ batch_list_exact(PickleState *state, PicklerObject *self, PyObject *obj)
31893189
assert(self->proto > 0);
31903190
assert(PyList_CheckExact(obj));
31913191

3192+
list_size = PyList_GET_SIZE(obj);
3193+
31923194
/* Write in batches of BATCHSIZE. */
31933195
total = 0;
31943196
do {
3195-
if (PyList_GET_SIZE(obj) - total == 1) {
3197+
if (list_size - total == 1) {
31963198
item = PyList_GetItemRef(obj, total);
31973199
if (item == NULL) {
31983200
_PyErr_FormatNote("when serializing %T item %zd", obj, total);
@@ -3229,8 +3231,14 @@ batch_list_exact(PickleState *state, PicklerObject *self, PyObject *obj)
32293231
}
32303232
if (_Pickler_Write(self, &appends_op, 1) < 0)
32313233
return -1;
3234+
if (PyList_GET_SIZE(obj) != list_size) {
3235+
PyErr_Format(
3236+
PyExc_RuntimeError,
3237+
"list changed size during iteration");
3238+
return -1;
3239+
}
32323240

3233-
} while (total < PyList_GET_SIZE(obj));
3241+
} while (total < list_size);
32343242

32353243
return 0;
32363244
}

0 commit comments

Comments
 (0)