Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4341095

Browse files
authored
gh-133572: Avoid using NTSTATUS on unsupported WinAPI partitions (GH-133573)
1 parent 54a6875 commit 4341095

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Avoid LsaNtStatus to WinError conversion on unsupported WinAPI partitions.

Modules/mmapmodule.c

+20-6
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,24 @@ filter_page_exception_method(mmap_object *self, EXCEPTION_POINTERS *ptrs,
290290
}
291291
return EXCEPTION_CONTINUE_SEARCH;
292292
}
293+
294+
static void
295+
_PyErr_SetFromNTSTATUS(ULONG status)
296+
{
297+
#if defined(MS_WINDOWS_DESKTOP) || defined(MS_WINDOWS_SYSTEM)
298+
PyErr_SetFromWindowsErr(LsaNtStatusToWinError((NTSTATUS)status));
299+
#else
300+
if (status & 0x80000000) {
301+
// HRESULT-shaped codes are supported by PyErr_SetFromWindowsErr
302+
PyErr_SetFromWindowsErr((int)status);
303+
}
304+
else {
305+
// No mapping for NTSTATUS values, so just return it for diagnostic purposes
306+
// If we provide it as winerror it could incorrectly change the type of the exception.
307+
PyErr_Format(PyExc_OSError, "Operating system error NTSTATUS=0x%08lX", status);
308+
}
309+
#endif
310+
}
293311
#endif
294312

295313
#if defined(MS_WINDOWS) && !defined(DONT_USE_SEH)
@@ -303,9 +321,7 @@ do { \
303321
assert(record.ExceptionCode == EXCEPTION_IN_PAGE_ERROR || \
304322
record.ExceptionCode == EXCEPTION_ACCESS_VIOLATION); \
305323
if (record.ExceptionCode == EXCEPTION_IN_PAGE_ERROR) { \
306-
NTSTATUS status = (NTSTATUS) record.ExceptionInformation[2]; \
307-
ULONG code = LsaNtStatusToWinError(status); \
308-
PyErr_SetFromWindowsErr(code); \
324+
_PyErr_SetFromNTSTATUS((ULONG)record.ExceptionInformation[2]); \
309325
} \
310326
else if (record.ExceptionCode == EXCEPTION_ACCESS_VIOLATION) { \
311327
PyErr_SetFromWindowsErr(ERROR_NOACCESS); \
@@ -332,9 +348,7 @@ do { \
332348
assert(record.ExceptionCode == EXCEPTION_IN_PAGE_ERROR || \
333349
record.ExceptionCode == EXCEPTION_ACCESS_VIOLATION); \
334350
if (record.ExceptionCode == EXCEPTION_IN_PAGE_ERROR) { \
335-
NTSTATUS status = (NTSTATUS) record.ExceptionInformation[2]; \
336-
ULONG code = LsaNtStatusToWinError(status); \
337-
PyErr_SetFromWindowsErr(code); \
351+
_PyErr_SetFromNTSTATUS((ULONG)record.ExceptionInformation[2]); \
338352
} \
339353
else if (record.ExceptionCode == EXCEPTION_ACCESS_VIOLATION) { \
340354
PyErr_SetFromWindowsErr(ERROR_NOACCESS); \

0 commit comments

Comments
 (0)