Skip to content

Commit 46afba7

Browse files
authored
gh-149816: Fix a race condition in _PyBytes_FromList with free-threading (#149909)
1 parent e56ae81 commit 46afba7

2 files changed

Lines changed: 6 additions & 2 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a race condition in ``_PyBytes_FromList`` in free-threading mode.

Objects/bytesobject.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "pycore_global_objects.h"// _Py_GET_GLOBAL_OBJECT()
1212
#include "pycore_initconfig.h" // _PyStatus_OK()
1313
#include "pycore_long.h" // _PyLong_DigitValue
14+
#include "pycore_list.h" // _PyList_GetItemRef
1415
#include "pycore_object.h" // _PyObject_GC_TRACK
1516
#include "pycore_pymem.h" // PYMEM_CLEANBYTE
1617
#include "pycore_strhex.h" // _Py_strhex_with_sep()
@@ -2991,8 +2992,10 @@ _PyBytes_FromList(PyObject *x)
29912992
size = _PyBytesWriter_GetAllocated(writer);
29922993

29932994
for (Py_ssize_t i = 0; i < PyList_GET_SIZE(x); i++) {
2994-
PyObject *item = PyList_GET_ITEM(x, i);
2995-
Py_INCREF(item);
2995+
PyObject *item = _PyList_GetItemRef((PyListObject *)x, i);
2996+
if (item == NULL) {
2997+
goto error;
2998+
}
29962999
Py_ssize_t value = PyNumber_AsSsize_t(item, NULL);
29973000
Py_DECREF(item);
29983001
if (value == -1 && PyErr_Occurred())

0 commit comments

Comments
 (0)