Repository navigation
Windows: Failure to check return value from lseek() in Modules/mmapmodule.c #68048
Description
Activity
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):--- 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...
- addedinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
on Apr 3, 2015 Thanks for the patch, Bill. If you want to work on similar issues see also bpo-15948.
- addedextension-modulesC modules in the Modules dirC modules in the Modules dirand removedinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)
on 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:Berker Peksag added the comment:
Thanks for the patch, Bill. If you want to work on similar issues see also
bpo-15948.----------
components: +Extension Modules -Interpreter Core
nosy: +berker.peksag, haypo, serhiy.storchaka
stage: -> patch review
versions: +Python 3.5
Python tracker <report@bugs.python.org>
<http://bugs.python.org/issue23860\>
/* Win9x appears to need us seeked to zero */
lseek(fileno, 0, SEEK_SET);Hum, is it still needed in 2015 with Python 3.5? We even dropped support for Windows XP.
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:STINNER Victor added the comment:
> /* Win9x appears to need us seeked to zero */
> lseek(fileno, 0, SEEK_SET);Hum, is it still needed in 2015 with Python 3.5? We even dropped support
for Windows XP.----------
Python tracker <report@bugs.python.org>
<http://bugs.python.org/issue23860\>
"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.
- 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 Oh right, PR 7017 does that: it removes the lseek() call ;-)
- added3.13only security fixesonly security fixesand removed3.10 (EOL)end of lifeend of life
on May 14, 2023 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.Reacted by Victor Stinner


lseekcall inmmap.mmapon Windows #7017Note: 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:
bugs.python.org fields: