Skip to content

Commit dafe3cb

Browse files
committed
[3.14] gh-149816: Fix a race condition in _PyBytes_FromList with free-threading (GH-149909)
(cherry picked from commit 46afba7) Co-authored-by: sobolevn <mail@sobolevn.me>
1 parent 9ad8a1b commit dafe3cb

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
@@ -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()
@@ -2877,8 +2878,10 @@ _PyBytes_FromList(PyObject *x)
28772878
size = writer.allocated;
28782879

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

0 commit comments

Comments
 (0)