Skip to content

Commit d858720

Browse files
add structured exception handling to mmap_read_method
1 parent 7e87d30 commit d858720

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

Modules/mmapmodule.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,17 @@ mmap_read_line_method(mmap_object *self,
291291
return result;
292292
}
293293

294+
#if defined(MS_WIN32) && !defined(DONT_USE_SEH)
295+
static DWORD HandlePageException(EXCEPTION_POINTERS *ptrs, EXCEPTION_RECORD *record)
296+
{
297+
*record = *ptrs->ExceptionRecord;
298+
if (ptrs->ExceptionRecord->ExceptionCode == EXCEPTION_IN_PAGE_ERROR) {
299+
return EXCEPTION_EXECUTE_HANDLER;
300+
}
301+
return EXCEPTION_CONTINUE_SEARCH;
302+
}
303+
#endif
304+
294305
static PyObject *
295306
mmap_read_method(mmap_object *self,
296307
PyObject *args)
@@ -307,8 +318,23 @@ mmap_read_method(mmap_object *self,
307318
remaining = (self->pos < self->size) ? self->size - self->pos : 0;
308319
if (num_bytes < 0 || num_bytes > remaining)
309320
num_bytes = remaining;
321+
322+
#ifdef DONT_USE_SEH
310323
result = PyBytes_FromStringAndSize(&self->data[self->pos], num_bytes);
311324
self->pos += num_bytes;
325+
#else
326+
EXCEPTION_RECORD record;
327+
__try {
328+
result = PyBytes_FromStringAndSize(&self->data[self->pos], num_bytes);
329+
self->pos += num_bytes;
330+
}
331+
__except (HandlePageException(GetExceptionInformation(), &record)) {
332+
NTSTATUS code = record.ExceptionInformation[2];
333+
PyErr_SetFromWindowsErr(code);
334+
result = NULL;
335+
}
336+
#endif
337+
312338
return result;
313339
}
314340

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy