Skip to content

gh-151126: Add missing PyErr_NoMemory in _winapi module#151154

Merged
sobolevn merged 1 commit into
python:mainfrom
sobolevn:issue-151126-3
Jun 9, 2026
Merged

gh-151126: Add missing PyErr_NoMemory in _winapi module#151154
sobolevn merged 1 commit into
python:mainfrom
sobolevn:issue-151126-3

Conversation

@sobolevn

@sobolevn sobolevn commented Jun 9, 2026

Copy link
Copy Markdown
Member

This place misses it:

cpython/Modules/_winapi.c

Lines 1196 to 1198 in 9fdbade

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

It is used as:

attribute_list->handle_list = gethandlelist(value, "handle_list", &handle_list_size);
if (attribute_list->handle_list == NULL && PyErr_Occurred()) {
    ret = -1;
    goto cleanup;
}

It would be rather strange to swallow MemoryError here.
Before that we allow KeyError to pass silently and return early if this code path should not be taken:

value = PyMapping_GetItemString(mapping, name);
if (!value) {
    PyErr_Clear();
    return NULL;
}

All other error paths in the same function 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;
}

Later this NULL is returned from _winapi.CreateProcess without an exception set.

@sobolevn sobolevn requested a review from zooba June 9, 2026 13:06
@sobolevn sobolevn added needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes needs backport to 3.15 pre-release feature fixes, bugs and security fixes labels Jun 9, 2026
@sobolevn sobolevn merged commit 8d94fa7 into python:main Jun 9, 2026
67 checks passed
@miss-islington-app

Copy link
Copy Markdown

Thanks @sobolevn for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14, 3.15.
🐍🍒⛏🤖

@bedevere-app

bedevere-app Bot commented Jun 9, 2026

Copy link
Copy Markdown

GH-151180 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label Jun 9, 2026
@bedevere-app

bedevere-app Bot commented Jun 9, 2026

Copy link
Copy Markdown

GH-151181 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Jun 9, 2026
@bedevere-app

bedevere-app Bot commented Jun 9, 2026

Copy link
Copy Markdown

GH-151182 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.13 bugs and security fixes label Jun 9, 2026
sobolevn added a commit that referenced this pull request Jun 9, 2026
…H-151154) (#151181)

gh-151126: Add missing `PyErr_NoMemory` in `_winapi` module (GH-151154)
(cherry picked from commit 8d94fa7)

Co-authored-by: sobolevn <mail@sobolevn.me>
sobolevn added a commit that referenced this pull request Jun 9, 2026
…H-151154) (#151182)

gh-151126: Add missing `PyErr_NoMemory` in `_winapi` module (GH-151154)
(cherry picked from commit 8d94fa7)

Co-authored-by: sobolevn <mail@sobolevn.me>
sobolevn added a commit that referenced this pull request Jun 9, 2026
…H-151154) (#151180)

gh-151126: Add missing `PyErr_NoMemory` in `_winapi` module (GH-151154)
(cherry picked from commit 8d94fa7)

Co-authored-by: sobolevn <mail@sobolevn.me>
clin1234 pushed a commit to clin1234/cpython that referenced this pull request Jun 9, 2026
@serhiy-storchaka

Copy link
Copy Markdown
Member

I am not sure this caused crash. At least not in the first case. But adding PyErr_NoMemory() was correct.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants