Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

mmap lacks error handling (SEH) on Windows which can lead to interpreter crashes #118209

Closed
Labels
OS-windowsextension-modulesC modules in the Modules dirtype-crashA hard crash of the interpreter, possibly with a core dump
@Dobatymo

Description

@Dobatymo

Crash report

What happened?

mmap reads and writes require structured exception handling (SEH) for correct handling of errors (like hardware read issues, or disk full write issues) on Windows. Seehttps://learn.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-mapviewoffile#remarks for a description of the possible exceptions andhttps://learn.microsoft.com/en-us/windows/win32/memory/reading-and-writing-from-a-file-view for how to handle them.

I found this issue when trying to read a file from a corrupt hdd. A normalopen().read() would raise aOSError: [Errno 22] Invalid argument exception. Whereas doing the same usingmmap crashes the interpreter.

I only tested on Windows 7 and Python 3.8, but from looking at the Microsoft docs and the Python source code, this issue should exist for all Windows versions and the latest Python source. (I don't have access to the broken file from a newer machine)

I think there is nothing here which guards against these errors

staticPyObject*
mmap_read_method(mmap_object*self,
PyObject*args)
{
Py_ssize_tnum_bytes=PY_SSIZE_T_MAX,remaining;
PyObject*result;
CHECK_VALID(NULL);
if (!PyArg_ParseTuple(args,"|O&:read",_Py_convert_optional_to_ssize_t,&num_bytes))
returnNULL;
CHECK_VALID(NULL);
/* silently 'adjust' out-of-range requests */
remaining= (self->pos<self->size) ?self->size-self->pos :0;
if (num_bytes<0||num_bytes>remaining)
num_bytes=remaining;
result=PyBytes_FromStringAndSize(&self->data[self->pos],num_bytes);
self->pos+=num_bytes;
returnresult;
}

withopen(path,"rb")asfp:withmmap.mmap(fp.fileno(),0,access=mmap.ACCESS_READ)asfr:fr.read(1024*8)# this crashes when `path` is on a corrupt hdd for example.

CPython versions tested on:

3.8

Operating systems tested on:

Windows

Output from running 'python -VV' on the command line:

No response

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    OS-windowsextension-modulesC modules in the Modules dirtype-crashA hard crash of the interpreter, possibly with a core dump

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp