Skip to content

Commit 1d4f9ab

Browse files
authored
[3.14] gh-149816: Fix a race condition in _PyBytes_FromList with free-threading (GH-149909) (#149912)
(cherry picked from commit 46afba7)
1 parent 25092e8 commit 1d4f9ab

2 files changed

Lines changed: 6 additions & 3 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 & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "pycore_global_objects.h"// _Py_GET_GLOBAL_OBJECT()
1111
#include "pycore_initconfig.h" // _PyStatus_OK()
1212
#include "pycore_long.h" // _PyLong_DigitValue
13+
#include "pycore_list.h" // _PyList_GetItemRef
1314
#include "pycore_object.h" // _PyObject_GC_TRACK
1415
#include "pycore_pymem.h" // PYMEM_CLEANBYTE
1516
#include "pycore_strhex.h" // _Py_strhex_with_sep()
@@ -2866,7 +2867,6 @@ _PyBytes_FromList(PyObject *x)
28662867
Py_ssize_t i, size = PyList_GET_SIZE(x);
28672868
Py_ssize_t value;
28682869
char *str;
2869-
PyObject *item;
28702870
_PyBytesWriter writer;
28712871

28722872
_PyBytesWriter_Init(&writer);
@@ -2877,8 +2877,10 @@ _PyBytes_FromList(PyObject *x)
28772877
size = writer.allocated;
28782878

28792879
for (i = 0; i < PyList_GET_SIZE(x); i++) {
2880-
item = PyList_GET_ITEM(x, i);
2881-
Py_INCREF(item);
2880+
PyObject *item = _PyList_GetItemRef((PyListObject *)x, i);
2881+
if (item == NULL) {
2882+
goto error;
2883+
}
28822884
value = PyNumber_AsSsize_t(item, NULL);
28832885
Py_DECREF(item);
28842886
if (value == -1 && PyErr_Occurred())

0 commit comments

Comments
 (0)