Skip to content

Commit ef5ca7c

Browse files
committed
Turn the last few stores/loads of list iterator/reversed iterator into
relaxed atomics. (These are not performance-sensitive parts of the code, and this lets us detect races elsewhere involving the iterators.)
1 parent 4028dfe commit ef5ca7c

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

Objects/listobject.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4000,7 +4000,7 @@ listiter_setstate(PyObject *self, PyObject *state)
40004000
index = -1;
40014001
else if (index > PyList_GET_SIZE(it->it_seq))
40024002
index = PyList_GET_SIZE(it->it_seq); /* iterator exhausted */
4003-
it->it_index = index;
4003+
FT_ATOMIC_STORE_SSIZE_RELAXED(it->it_index, index);
40044004
}
40054005
Py_RETURN_NONE;
40064006
}
@@ -4152,7 +4152,7 @@ listreviter_setstate(PyObject *self, PyObject *state)
41524152
index = -1;
41534153
else if (index > PyList_GET_SIZE(it->it_seq) - 1)
41544154
index = PyList_GET_SIZE(it->it_seq) - 1;
4155-
it->it_index = index;
4155+
FT_ATOMIC_STORE_SSIZE_RELAXED(it->it_index, index);
41564156
}
41574157
Py_RETURN_NONE;
41584158
}
@@ -4169,18 +4169,19 @@ listiter_reduce_general(void *_it, int forward)
41694169
* call must be before access of iterator pointers.
41704170
* see issue #101765 */
41714171

4172-
/* the objects are not the same, index is of different types! */
41734172
if (forward) {
41744173
iter = _PyEval_GetBuiltin(&_Py_ID(iter));
41754174
_PyListIterObject *it = (_PyListIterObject *)_it;
4176-
if (it->it_index >= 0) {
4177-
return Py_BuildValue("N(O)n", iter, it->it_seq, it->it_index);
4175+
Py_ssize_t idx = FT_ATOMIC_LOAD_SSIZE_RELAXED(it->it_index);
4176+
if (idx >= 0) {
4177+
return Py_BuildValue("N(O)n", iter, it->it_seq, idx);
41784178
}
41794179
} else {
41804180
iter = _PyEval_GetBuiltin(&_Py_ID(reversed));
41814181
listreviterobject *it = (listreviterobject *)_it;
4182-
if (it->it_index >= 0) {
4183-
return Py_BuildValue("N(O)n", iter, it->it_seq, it->it_index);
4182+
Py_ssize_t idx = FT_ATOMIC_LOAD_SSIZE_RELAXED(it->it_index);
4183+
if (idx >= 0) {
4184+
return Py_BuildValue("N(O)n", iter, it->it_seq, idx);
41844185
}
41854186
}
41864187
/* empty iterator, create an empty list */

0 commit comments

Comments
 (0)