Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Fix undefined behaviour in ``_PyObject_MergePerThreadRefcounts()`` on the
free-threaded build. The per-thread reference count delta being merged is
routinely negative, and it was shifted left by ``_Py_REF_SHARED_SHIFT``,
which is undefined behaviour for a negative value. The shift is now done in
the unsigned domain.
6 changes: 5 additions & 1 deletion Python/uniqueid.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,12 @@ _PyObject_MergePerThreadRefcounts(_PyThreadStateImpl *tstate)
Py_ssize_t refcnt = tstate->refcounts.values[i];
if (refcnt != 0) {
PyObject *obj = pool->table[i].obj;
/* `refcnt` is a per-thread delta, so it is routinely negative,
and shifting a negative value left is undefined behaviour.
Shift in the unsigned domain instead. */
_Py_atomic_add_ssize(&obj->ob_ref_shared,
refcnt << _Py_REF_SHARED_SHIFT);
(Py_ssize_t)((size_t)refcnt
<< _Py_REF_SHARED_SHIFT));
tstate->refcounts.values[i] = 0;
}
}
Expand Down
Loading