gh-133931: Introduce _PyObject_XSetRefDelayed to replace Py_XSETREF (gh-134377)

This commit is contained in:
Donghee Na
2025-06-18 08:36:02 +09:00
committed by GitHub
parent cb39410111
commit 52be7f445e
6 changed files with 104 additions and 21 deletions

View File

@@ -3967,13 +3967,9 @@ _PyObject_SetDict(PyObject *obj, PyObject *value)
return -1;
}
Py_BEGIN_CRITICAL_SECTION(obj);
PyObject *olddict = *dictptr;
FT_ATOMIC_STORE_PTR_RELEASE(*dictptr, Py_NewRef(value));
#ifdef Py_GIL_DISABLED
_PyObject_XDecRefDelayed(olddict);
#else
Py_XDECREF(olddict);
#endif
// gh-133980: To prevent use-after-free from other threads that reference
// the __dict__
_PyObject_XSetRefDelayed(dictptr, Py_NewRef(value));
Py_END_CRITICAL_SECTION();
return 0;
}