Skip to content

Commit e8138be

Browse files
committed
gh-116738: Add NULL check for the item arg in heappush()
1 parent 75a1d3a commit e8138be

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

Modules/_heapqmodule.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,20 @@ static PyObject *
131131
_heapq_heappush_impl(PyObject *module, PyObject *heap, PyObject *item)
132132
/*[clinic end generated code: output=912c094f47663935 input=f7a4f03ef8d52e67]*/
133133
{
134+
if (item == NULL) {
135+
PyErr_BadInternalCall();
136+
return NULL;
137+
}
138+
134139
// In a free-threaded build, the heap is locked at this point.
135140
// Therefore, calling _PyList_AppendTakeRef() is safe and no overhead.
136-
if (_PyList_AppendTakeRef((PyListObject *)heap, Py_XNewRef(item)))
141+
if (_PyList_AppendTakeRef((PyListObject *)heap, Py_NewRef(item))) {
137142
return NULL;
143+
}
138144

139-
if (siftdown((PyListObject *)heap, 0, PyList_GET_SIZE(heap)-1))
145+
if (siftdown((PyListObject *)heap, 0, PyList_GET_SIZE(heap)-1)) {
140146
return NULL;
147+
}
141148
Py_RETURN_NONE;
142149
}
143150

@@ -502,9 +509,14 @@ static PyObject *
502509
_heapq_heappush_max_impl(PyObject *module, PyObject *heap, PyObject *item)
503510
/*[clinic end generated code: output=c869d5f9deb08277 input=c437e3d1ff8dcb70]*/
504511
{
512+
if (item == NULL) {
513+
PyErr_BadInternalCall();
514+
return NULL;
515+
}
516+
505517
// In a free-threaded build, the heap is locked at this point.
506518
// Therefore, calling _PyList_AppendTakeRef() is safe and no overhead.
507-
if (_PyList_AppendTakeRef((PyListObject *)heap, Py_XNewRef(item))) {
519+
if (_PyList_AppendTakeRef((PyListObject *)heap, Py_NewRef(item))) {
508520
return NULL;
509521
}
510522

0 commit comments

Comments
 (0)