Skip to content

Commit

Permalink
add structured exception handling to mmap_read_method
Browse files Browse the repository at this point in the history
  • Loading branch information
Dobatymo committed Apr 24, 2024
1 parent 7e87d30 commit 4013627
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions Modules/mmapmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,17 @@ mmap_read_line_method(mmap_object *self,
return result;
}

#if defined(MS_WIN32) && !defined(DONT_USE_SEH)
static DWORD HandlePageException(EXCEPTION_POINTERS *ptrs, EXCEPTION_RECORD *record)
{
*record = *ptrs->ExceptionRecord;
if (ptrs->ExceptionRecord->ExceptionCode == EXCEPTION_IN_PAGE_ERROR) {
return EXCEPTION_EXECUTE_HANDLER;
}
return EXCEPTION_CONTINUE_SEARCH;
}
#endif

static PyObject *
mmap_read_method(mmap_object *self,
PyObject *args)
Expand All @@ -307,8 +318,23 @@ mmap_read_method(mmap_object *self,
remaining = (self->pos < self->size) ? self->size - self->pos : 0;
if (num_bytes < 0 || num_bytes > remaining)
num_bytes = remaining;

#if defined(MS_WIN32) && !defined(DONT_USE_SEH)
EXCEPTION_RECORD record;
__try {
result = PyBytes_FromStringAndSize(&self->data[self->pos], num_bytes);
self->pos += num_bytes;
}
__except (HandlePageException(GetExceptionInformation(), &record)) {
NTSTATUS code = record.ExceptionInformation[2];

Check warning on line 329 in Modules/mmapmodule.c

View workflow job for this annotation

GitHub Actions / Windows / build and test (x64)

'initializing': conversion from 'ULONG_PTR' to 'NTSTATUS', possible loss of data [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]

Check warning on line 329 in Modules/mmapmodule.c

View workflow job for this annotation

GitHub Actions / Windows / build (arm64)

'initializing': conversion from 'ULONG_PTR' to 'NTSTATUS', possible loss of data [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]

Check warning on line 329 in Modules/mmapmodule.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / build and test (x64)

'initializing': conversion from 'ULONG_PTR' to 'NTSTATUS', possible loss of data [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]

Check warning on line 329 in Modules/mmapmodule.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / build (arm64)

'initializing': conversion from 'ULONG_PTR' to 'NTSTATUS', possible loss of data [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]
PyErr_SetFromWindowsErr(code);
result = NULL;
}
#else
result = PyBytes_FromStringAndSize(&self->data[self->pos], num_bytes);
self->pos += num_bytes;
#endif

return result;
}

Expand Down

0 comments on commit 4013627

Please sign in to comment.