You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Windows: Failure to check return value from lseek() in Modules/mmapmodule.c #68048
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=Nonecreated_at=<Date2015-04-03.17:44:00.434>labels= ['extension-modules', 'type-bug', '3.10', 'OS-windows']
title='Windows: Failure to check return value from lseek() in Modules/mmapmodule.c'updated_at=<Date2020-07-15.17:06:15.045>user='https://bugs.python.org/dogbert2'
In reviewing code in directory Python-3.4.3/Modules, file
'mmapmodule', I found a call to 'lseek()' without a check for
a return value of -1, indicating failure. The patch file below
corrects this issue (diff -u format):
--- mmapmodule.c.orig 2015-04-02 19:05:30.380554538 -0700+++ mmapmodule.c 2015-04-02 19:11:00.320488207 -0700@@ -1335,7 +1335,11 @@
return NULL;
}
/* Win9x appears to need us seeked to zero */
- lseek(fileno, 0, SEEK_SET);+ if (lseek(fileno, 0, SEEK_SET) == -1) { /* call to lseek() failed */+ PyErr_SetFromErrno(PyExc_OSError);+ return NULL;+ }+
}
m_obj = (mmap_object *)type->tp_alloc(type, 0);
I am attaching the patch file to this bug report...
changed the title [-]Failure to check return value from lseek() in Modules/mmapmodule.c[/-][+]Windows: Failure to check return value from lseek() in Modules/mmapmodule.c[/+]on May 22, 2018
The lseek() this issue is about is vestigial -- it has had no effect since 2003.
It started life in 1ef4e2d (2000-04-05, Mark Hammond) as fseek(&_iob[fileno], 0, SEEK_SET), not lseek, with the comment "Win9x appears to need us seeked to zero". The operative part was not the seek but the flush: fseek() on an update stream flushes pending output, and the test of the day mapped a file that had just been written through the buffered FILE* with no flush(), closing it only after the mapping was created. Tim Peters diagnosed the same symptom independently in e12cda9 (2002-04-23): "On Win2K it thought 'foo' started at byte offset 0 instead of at the pagesize, and on Win98 it thought 'foo' didn't exist at all. Flushing the file in test_mmap after writing to it makes the problem go away." The f.flush() he added then is still in test_basic today. Win9x was affected and NT was not because on Windows 95 a file handle used to create a file-mapping object must not be used in subsequent ReadFile/WriteFile calls, so the late flush never became visible through the view.
In 69c2b88 (2003-04-09) the call became lseek(fileno, 0, SEEK_SET) for an unrelated reason -- &_iob[fileno], which indexed the stdio array by file descriptor, crashed for fd >= 20. That silently dropped the flush, i.e. the only part that ever did anything.
What is left is a bare file-position side effect: mmap.mmap() rewinds the underlying file on Windows and nowhere else. Removing the call (#7017) checks nothing and loses nothing; it just makes Windows consistent with the other platforms. Verified on Windows 11 with a debug build of main -- the only observable change is f.tell(), and test_mmap passes.
Activity
dogbert2 commentedon Apr 3, 2015
Hello All,
In reviewing code in directory Python-3.4.3/Modules, file
'mmapmodule', I found a call to 'lseek()' without a check for
a return value of -1, indicating failure. The patch file below
corrects this issue (diff -u format):
I am attaching the patch file to this bug report...
berkerpeksag commentedon Apr 4, 2015
Thanks for the patch, Bill. If you want to work on similar issues see also bpo-15948.
dogbert2 commentedon Apr 4, 2015
I would check 23855 as well, since the malloc() missing a sanity check,
which could be a more serious issue ..
On Sat, Apr 4, 2015 at 1:32 AM, Berker Peksag <report@bugs.python.org>
wrote:
vstinner commentedon Apr 9, 2015
Hum, is it still needed in 2015 with Python 3.5? We even dropped support for Windows XP.
dogbert2 commentedon Apr 9, 2015
At the moment, I'm not sure if it's needed or not, but if it's only an
issue with XP, then it might not be worth fixing...:)
On Thu, Apr 9, 2015 at 1:35 PM, STINNER Victor <report@bugs.python.org>
wrote:
vstinner commentedon May 22, 2018
"At the moment, I'm not sure if it's needed or not, but if it's only an
issue with XP, then it might not be worth fixing...:)"
If the workaround is no longer needed, the lseek() call should be removed.
[-]Failure to check return value from lseek() in Modules/mmapmodule.c[/-][+]Windows: Failure to check return value from lseek() in Modules/mmapmodule.c[/+]vstinner commentedon May 22, 2018
Oh right, PR 7017 does that: it removes the lseek() call ;-)
serhiy-storchaka commentedon Aug 9, 2026
The
lseek()this issue is about is vestigial -- it has had no effect since 2003.It started life in 1ef4e2d (2000-04-05, Mark Hammond) as
fseek(&_iob[fileno], 0, SEEK_SET), notlseek, with the comment "Win9x appears to need us seeked to zero". The operative part was not the seek but the flush:fseek()on an update stream flushes pending output, and the test of the day mapped a file that had just been written through the bufferedFILE*with noflush(), closing it only after the mapping was created. Tim Peters diagnosed the same symptom independently in e12cda9 (2002-04-23): "On Win2K it thought 'foo' started at byte offset 0 instead of at the pagesize, and on Win98 it thought 'foo' didn't exist at all. Flushing the file in test_mmap after writing to it makes the problem go away." Thef.flush()he added then is still intest_basictoday. Win9x was affected and NT was not because on Windows 95 a file handle used to create a file-mapping object must not be used in subsequentReadFile/WriteFilecalls, so the late flush never became visible through the view.In 69c2b88 (2003-04-09) the call became
lseek(fileno, 0, SEEK_SET)for an unrelated reason --&_iob[fileno], which indexed the stdio array by file descriptor, crashed for fd >= 20. That silently dropped the flush, i.e. the only part that ever did anything.What is left is a bare file-position side effect:
mmap.mmap()rewinds the underlying file on Windows and nowhere else. Removing the call (#7017) checks nothing and loses nothing; it just makes Windows consistent with the other platforms. Verified on Windows 11 with a debug build of main -- the only observable change isf.tell(), andtest_mmappasses.gh-68048: Remove unneeded lseek() call in mmap.mmap on Windows (GH-7017)
pythongh-68048: Remove unneeded lseek() call in mmap.mmap on Windows (p…