GeezerVison is showing github.com — Many places have missing `PyErr_NoMemory()` call after failing memory allocation · Issue #151126 · python/cpython
Many places have missing PyErr_NoMemory() call after failing memory allocation #151126DescriptionCrash report
remove_unused_consts in Python/flowgraph.c
When PyMem_Malloc(nconsts * sizeof(Py_ssize_t)); fails, we need to set an exception.
|
static int |
|
remove_unused_consts(basicblock *entryblock, PyObject *consts) |
|
{ |
|
assert(PyList_CheckExact(consts)); |
|
Py_ssize_t nconsts = PyList_GET_SIZE(consts); |
|
if (nconsts == 0) { |
|
return SUCCESS; /* nothing to do */ |
|
} |
|
|
|
Py_ssize_t *index_map = NULL; |
|
Py_ssize_t *reverse_index_map = NULL; |
|
int err = ERROR; |
|
|
|
index_map = PyMem_Malloc(nconsts * sizeof(Py_ssize_t)); |
|
if (index_map == NULL) { |
|
goto end; |
|
} |
gethandlelist and getattributelist in _winapi.c
This place misses it:
|
ret = PyMem_Malloc(*size); |
|
if (ret == NULL) |
|
goto cleanup; |
All other error paths set exceptions, except this if:
|
attribute_list->attribute_list = PyMem_Malloc(attribute_list_size); |
|
if (attribute_list->attribute_list == NULL) { |
|
ret = -1; |
|
goto cleanup; |
|
} |
_channelends_new in Modules/_interpchannelsmodule.c
|
static _channelends * |
|
_channelends_new(void) |
|
{ |
|
_channelends *ends = GLOBAL_MALLOC(_channelends); |
|
if (ends== NULL) { |
|
return NULL; |
|
} |
Later we check for errors in handle_channel_error, it requires either setting err to non -1 or to set Python exception. Both are false, it fails with the assertion failure.
Linked PRs
Activitysobolevn commented on Jun 9, 2026 Well, actually a lot of places have the same problem. I will turn this issue into a meta one and will 1 PR per module, so it would be easier to review / backport. Reacted by Victor Stinner and Arseniy Terekhin changed the title [-]`remove_unused_consts` is missing `PyErr_NoMemory()` call[/-] [+]Many places have missing `PyErr_NoMemory()` call after failing memory allocation[/+] on Jun 9, 2026serhiy-storchaka commented on Jun 9, 2026 This is probably a new code. Such errors were fixed in old code years ago. added 8 commits that reference this issue on Jun 9, 2026gh-151126: Fix missing `PyErr_NoMemory()` in `remove_unused_consts` (#… added 3 commits that reference this issue on Oct 25, 2026gh-151126: Set `MemoryError` on allocation failure in CJK codec macros ( added 3 commits that reference this issue on Nov 12, 2026gh-151126: set `MemoryError` on alloc failure in `_PyXIData_InitWithS…
MetadataMetadataStatusShow more project fields
DevelopmentNo branches or pull requestsIssue actions
|