From 01a85ec7113098e5b3469686096d8e8b4d52951f Mon Sep 17 00:00:00 2001 From: Ivy Xu Date: Wed, 17 Jun 2026 16:51:37 +0800 Subject: [PATCH] Fix missing PyErr_NoMemory in `_winapi.c` --- .../2026-06-17-16-46-07.gh-issue-151126.vhTL0T.rst | 2 ++ Modules/_winapi.c | 5 +++++ 2 files changed, 7 insertions(+) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2026-06-17-16-46-07.gh-issue-151126.vhTL0T.rst diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-06-17-16-46-07.gh-issue-151126.vhTL0T.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-06-17-16-46-07.gh-issue-151126.vhTL0T.rst new file mode 100644 index 00000000000000..6f2d230b1dcfc0 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-06-17-16-46-07.gh-issue-151126.vhTL0T.rst @@ -0,0 +1,2 @@ +Avoid possible crash in ``_winapi.c`` where a device has no memory left. Now +it properly raises a :exc:`MemoryError`. Patch by Ivy Xu. diff --git a/Modules/_winapi.c b/Modules/_winapi.c index 369a7400eb63b9..ed9bcdadba053f 100644 --- a/Modules/_winapi.c +++ b/Modules/_winapi.c @@ -1684,6 +1684,10 @@ _winapi_GetShortPathName_impl(PyObject *module, LPCWSTR path) } PyMem_Free((void *)buffer); } + else { + PyErr_NoMemory(); + return result; + } } else { PyErr_SetFromWindowsErr(0); } @@ -2394,6 +2398,7 @@ _winapi_BatchedWaitForMultipleObjects_impl(PyObject *module, while (i < nhandles) { BatchedWaitData *data = (BatchedWaitData*)PyMem_Malloc(sizeof(BatchedWaitData)); if (!data) { + PyErr_NoMemory(); goto error; } thread_data[thread_count++] = data;