From: Kumar Aditya Date: Fri, 18 Apr 2025 15:33:42 +0000 (+0530) Subject: gh-132685: fix thread safety of `PyMember_GetOne` with `_Py_T_OBJECT` (#132690) X-Git-Tag: v3.14.0b1~411 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=7fd708b727fe19403726da6cb912b81768a96946;p=thirdparty%2FPython%2Fcpython.git gh-132685: fix thread safety of `PyMember_GetOne` with `_Py_T_OBJECT` (#132690) --- diff --git a/Python/structmember.c b/Python/structmember.c index d36e049d6b5d..574acf296157 100644 --- a/Python/structmember.c +++ b/Python/structmember.c @@ -85,10 +85,22 @@ PyMember_GetOne(const char *obj_addr, PyMemberDef *l) break; } case _Py_T_OBJECT: - v = *(PyObject **)addr; - if (v == NULL) + v = FT_ATOMIC_LOAD_PTR(*(PyObject **) addr); + if (v != NULL) { +#ifdef Py_GIL_DISABLED + if (!_Py_TryIncrefCompare((PyObject **) addr, v)) { + Py_BEGIN_CRITICAL_SECTION((PyObject *) obj_addr); + v = FT_ATOMIC_LOAD_PTR(*(PyObject **) addr); + Py_XINCREF(v); + Py_END_CRITICAL_SECTION(); + } +#else + Py_INCREF(v); +#endif + } + if (v == NULL) { v = Py_None; - Py_INCREF(v); + } break; case Py_T_OBJECT_EX: v = member_get_object(addr, obj_addr, l);