Skip to content

gh-116738: Make _heapq module thread-safe #135036

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jun 9, 2025
Prev Previous commit
Next Next commit
gh-116738: Add NULL check for the item arg in heappush()
  • Loading branch information
yoney committed Jun 5, 2025
commit e8138be42a97cd63511773e42c76d390b4d84bc4
18 changes: 15 additions & 3 deletions Modules/_heapqmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,20 @@ static PyObject *
_heapq_heappush_impl(PyObject *module, PyObject *heap, PyObject *item)
/*[clinic end generated code: output=912c094f47663935 input=f7a4f03ef8d52e67]*/
{
if (item == NULL) {
PyErr_BadInternalCall();
return NULL;
}

// In a free-threaded build, the heap is locked at this point.
// Therefore, calling _PyList_AppendTakeRef() is safe and no overhead.
if (_PyList_AppendTakeRef((PyListObject *)heap, Py_XNewRef(item)))
if (_PyList_AppendTakeRef((PyListObject *)heap, Py_NewRef(item))) {
return NULL;
}

if (siftdown((PyListObject *)heap, 0, PyList_GET_SIZE(heap)-1))
if (siftdown((PyListObject *)heap, 0, PyList_GET_SIZE(heap)-1)) {
return NULL;
}
Py_RETURN_NONE;
}

Expand Down Expand Up @@ -502,9 +509,14 @@ static PyObject *
_heapq_heappush_max_impl(PyObject *module, PyObject *heap, PyObject *item)
/*[clinic end generated code: output=c869d5f9deb08277 input=c437e3d1ff8dcb70]*/
{
if (item == NULL) {
PyErr_BadInternalCall();
return NULL;
}

// In a free-threaded build, the heap is locked at this point.
// Therefore, calling _PyList_AppendTakeRef() is safe and no overhead.
if (_PyList_AppendTakeRef((PyListObject *)heap, Py_XNewRef(item))) {
if (_PyList_AppendTakeRef((PyListObject *)heap, Py_NewRef(item))) {
return NULL;
}

Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy