]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-149816: Fix a race condition in `_PyBytes_FromList` with free-threading (#149909)
authorsobolevn <mail@sobolevn.me>
Sat, 16 May 2026 08:37:34 +0000 (11:37 +0300)
committerGitHub <noreply@github.com>
Sat, 16 May 2026 08:37:34 +0000 (08:37 +0000)
Misc/NEWS.d/next/Core_and_Builtins/2026-05-16-11-03-54.gh-issue-149816.X_gqMT.rst [new file with mode: 0644]
Objects/bytesobject.c

diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-05-16-11-03-54.gh-issue-149816.X_gqMT.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-05-16-11-03-54.gh-issue-149816.X_gqMT.rst
new file mode 100644 (file)
index 0000000..d35f085
--- /dev/null
@@ -0,0 +1 @@
+Fix a race condition in ``_PyBytes_FromList`` in free-threading mode.
index cd8417e25839161ca93b911f851723b69d39c683..6dd32b7079a765b059f7c9db0c76d483f76e8a49 100644 (file)
@@ -11,6 +11,7 @@
 #include "pycore_global_objects.h"// _Py_GET_GLOBAL_OBJECT()
 #include "pycore_initconfig.h"    // _PyStatus_OK()
 #include "pycore_long.h"          // _PyLong_DigitValue
+#include "pycore_list.h"          // _PyList_GetItemRef
 #include "pycore_object.h"        // _PyObject_GC_TRACK
 #include "pycore_pymem.h"         // PYMEM_CLEANBYTE
 #include "pycore_strhex.h"        // _Py_strhex_with_sep()
@@ -2991,8 +2992,10 @@ _PyBytes_FromList(PyObject *x)
     size = _PyBytesWriter_GetAllocated(writer);
 
     for (Py_ssize_t i = 0; i < PyList_GET_SIZE(x); i++) {
-        PyObject *item = PyList_GET_ITEM(x, i);
-        Py_INCREF(item);
+        PyObject *item = _PyList_GetItemRef((PyListObject *)x, i);
+        if (item == NULL) {
+            goto error;
+        }
         Py_ssize_t value = PyNumber_AsSsize_t(item, NULL);
         Py_DECREF(item);
         if (value == -1 && PyErr_Occurred())