Skip to content

Many places have missing PyErr_NoMemory() call after failing memory allocation #151126

Open

Description

@sobolevn

Crash report

  1. remove_unused_consts in Python/flowgraph.c

When PyMem_Malloc(nconsts * sizeof(Py_ssize_t)); fails, we need to set an exception.

cpython/Python/flowgraph.c

Lines 3267 to 3283 in 29a920e

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;
}

  1. gethandlelist and getattributelist in _winapi.c

This place misses it:

cpython/Modules/_winapi.c

Lines 1196 to 1198 in 9fdbade

ret = PyMem_Malloc(*size);
if (ret == NULL)
goto cleanup;

All other error paths set exceptions, except this if:

cpython/Modules/_winapi.c

Lines 1278 to 1282 in 9fdbade

attribute_list->attribute_list = PyMem_Malloc(attribute_list_size);
if (attribute_list->attribute_list == NULL) {
ret = -1;
goto cleanup;
}

  1. _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

Activity

  1. added a commit that references this issue on Jun 9, 2026
  2. added a commit that references this issue on Jun 9, 2026
  3. added 12 commits that reference this issue on Jun 10, 2026
  4. added 5 commits that reference this issue on Jun 17, 2026
  5. added a commit that references this issue on Jun 22, 2026
  6. added 4 commits that reference this issue on Jun 23, 2026
  7. added 18 commits that reference this issue on Jun 25, 2026
  8. added 2 commits that reference this issue on Jul 5, 2026
  9. added a commit that references this issue on Jul 14, 2026
  10. added a commit that references this issue on Jul 15, 2026
  11. added a commit that references this issue on Aug 4, 2026
  12. added 3 commits that reference this issue on Aug 23, 2026
  13. added a commit that references this issue on Oct 9, 2026
  14. added 3 commits that reference this issue on Oct 25, 2026
  15. added a commit that references this issue on Nov 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

interpreter-core(Objects, Python, Grammar, and Parser dirs)type-crashA hard crash of the interpreter, possibly with a core dump

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions