mirror of
https://github.com/python/cpython.git
synced 2025-07-24 11:44:31 +00:00
gh-130019: Fix data race in _PyType_AllocNoTrack (gh-130058)
The reference count fields, such as `ob_tid` and `ob_ref_shared`, may be accessed concurrently in the free threading build by a `_Py_TryXGetRef` or similar operation. The PyObject header fields will be initialized by `_PyObject_Init`, so only call `memset()` to zero-initialize the remainder of the allocation.
This commit is contained in:
parent
c357d69003
commit
0559339ccd
3 changed files with 9 additions and 5 deletions
|
@ -2310,11 +2310,12 @@ PyObject *
|
|||
PyUnstable_Object_GC_NewWithExtraData(PyTypeObject *tp, size_t extra_size)
|
||||
{
|
||||
size_t presize = _PyType_PreHeaderSize(tp);
|
||||
PyObject *op = gc_alloc(tp, _PyObject_SIZE(tp) + extra_size, presize);
|
||||
size_t size = _PyObject_SIZE(tp) + extra_size;
|
||||
PyObject *op = gc_alloc(tp, size, presize);
|
||||
if (op == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
memset(op, 0, _PyObject_SIZE(tp) + extra_size);
|
||||
memset((char *)op + sizeof(PyObject), 0, size - sizeof(PyObject));
|
||||
_PyObject_Init(op, tp);
|
||||
return op;
|
||||
}
|
||||
|
|
|
@ -2595,11 +2595,12 @@ PyObject *
|
|||
PyUnstable_Object_GC_NewWithExtraData(PyTypeObject *tp, size_t extra_size)
|
||||
{
|
||||
size_t presize = _PyType_PreHeaderSize(tp);
|
||||
PyObject *op = gc_alloc(tp, _PyObject_SIZE(tp) + extra_size, presize);
|
||||
size_t size = _PyObject_SIZE(tp) + extra_size;
|
||||
PyObject *op = gc_alloc(tp, size, presize);
|
||||
if (op == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
memset(op, 0, _PyObject_SIZE(tp) + extra_size);
|
||||
memset((char *)op + sizeof(PyObject), 0, size - sizeof(PyObject));
|
||||
_PyObject_Init(op, tp);
|
||||
return op;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue