]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] gh-129983: fix data race in compile_template in sre.c (#130038)
authorKumar Aditya <kumaraditya@python.org>
Wed, 12 Feb 2025 13:33:56 +0000 (19:03 +0530)
committerGitHub <noreply@github.com>
Wed, 12 Feb 2025 13:33:56 +0000 (13:33 +0000)
gh-129983: fix data race in compile_template in sre.c (#130015)

(cherry picked from commit 3cf68cdd3e1809df4e426c61f6990de63747ec6f)

Co-authored-by: Tomasz Pytel <tompytel@gmail.com>
Misc/NEWS.d/next/Core and Builtins/2025-02-11-20-38-37.gh-issue-129983._1Fujo.rst [new file with mode: 0644]
Modules/_sre/sre.c

diff --git a/Misc/NEWS.d/next/Core and Builtins/2025-02-11-20-38-37.gh-issue-129983._1Fujo.rst b/Misc/NEWS.d/next/Core and Builtins/2025-02-11-20-38-37.gh-issue-129983._1Fujo.rst
new file mode 100644 (file)
index 0000000..9b43570
--- /dev/null
@@ -0,0 +1 @@
+Fix data race in compile_template in :file:`sre.c`.
index 7fbb59ef89abc80a527245149a0402d954f8eeff..608a0ccb11535cd1c745cf90b799798dead30b86 100644 (file)
@@ -1160,13 +1160,21 @@ compile_template(_sremodulestate *module_state,
                  PatternObject *pattern, PyObject *template)
 {
     /* delegate to Python code */
-    PyObject *func = module_state->compile_template;
+    PyObject *func = FT_ATOMIC_LOAD_PTR(module_state->compile_template);
     if (func == NULL) {
         func = _PyImport_GetModuleAttrString("re", "_compile_template");
         if (func == NULL) {
             return NULL;
         }
+#ifdef Py_GIL_DISABLED
+        PyObject *other_func = NULL;
+        if (!_Py_atomic_compare_exchange_ptr(&module_state->compile_template, &other_func, func))  {
+            Py_DECREF(func);
+            func = other_func;
+        }
+#else
         Py_XSETREF(module_state->compile_template, func);
+#endif
     }
 
     PyObject *args[] = {(PyObject *)pattern, template};