]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.14] gh-149816: Fix a race condition in `_PyBytes_FromList` with free-threading...
authorsobolevn <mail@sobolevn.me>
Thu, 21 May 2026 09:53:54 +0000 (12:53 +0300)
committerGitHub <noreply@github.com>
Thu, 21 May 2026 09:53:54 +0000 (09:53 +0000)
(cherry picked from commit 46afba7b9324bc9492c3527d0fe47dd74f1f598c)

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 eb13f16f67295c60febba6c8e0929fb28ba4fd9c..765589d679cce3bc1ec07c98535201e6e121000e 100644 (file)
@@ -10,6 +10,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()
@@ -2866,7 +2867,6 @@ _PyBytes_FromList(PyObject *x)
     Py_ssize_t i, size = PyList_GET_SIZE(x);
     Py_ssize_t value;
     char *str;
-    PyObject *item;
     _PyBytesWriter writer;
 
     _PyBytesWriter_Init(&writer);
@@ -2877,8 +2877,10 @@ _PyBytes_FromList(PyObject *x)
     size = writer.allocated;
 
     for (i = 0; i < PyList_GET_SIZE(x); i++) {
-        item = PyList_GET_ITEM(x, i);
-        Py_INCREF(item);
+        PyObject *item = _PyList_GetItemRef((PyListObject *)x, i);
+        if (item == NULL) {
+            goto error;
+        }
         value = PyNumber_AsSsize_t(item, NULL);
         Py_DECREF(item);
         if (value == -1 && PyErr_Occurred())