Skip to content

Windows: Failure to check return value from lseek() in Modules/mmapmodule.c #68048

Closed

Description

@dogbert2
mannequin
BPO 23860
Nosy @pfmoore, @vstinner, @tjguk, @berkerpeksag, @zware, @serhiy-storchaka, @zooba, @ZackerySpytz
PRs
  • gh-68048: Remove unneeded lseek call in mmap.mmap on Windows #7017
  • Files
  • mmapmodule.c.patch: patch file (diff -u) for this bug
  • 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 = None
    closed_at = None
    created_at = <Date 2015-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 = <Date 2020-07-15.17:06:15.045>
    user = 'https://bugs.python.org/dogbert2'

    bugs.python.org fields:

    activity = <Date 2020-07-15.17:06:15.045>
    actor = 'serhiy.storchaka'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Extension Modules', 'Windows']
    creation = <Date 2015-04-03.17:44:00.434>
    creator = 'dogbert2'
    dependencies = []
    files = ['38823']
    hgrepos = []
    issue_num = 23860
    keywords = ['patch']
    message_count = 7.0
    messages = ['240015', '240051', '240088', '240383', '240384', '317284', '317285']
    nosy_count = 9.0
    nosy_names = ['paul.moore', 'vstinner', 'tim.golden', 'berker.peksag', 'zach.ware', 'serhiy.storchaka', 'steve.dower', 'dogbert2', 'ZackerySpytz']
    pr_nums = ['7017']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue23860'
    versions = ['Python 3.10']

    Activity

    1. dogbert2 commented on Apr 3, 2015

      dogbert2mannequin
      MannequinAuthor

      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...

    2. added
      interpreter-core(Objects, Python, Grammar, and Parser dirs)
      type-bugAn unexpected behavior, bug, or error
      on Apr 3, 2015
    3. berkerpeksag commented on Apr 4, 2015

      @berkerpeksag
      Member

      Thanks for the patch, Bill. If you want to work on similar issues see also bpo-15948.

    4. added and removed
      interpreter-core(Objects, Python, Grammar, and Parser dirs)
      on Apr 4, 2015
    5. dogbert2 commented on Apr 4, 2015

      dogbert2mannequin
      MannequinAuthor

      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\>


    6. vstinner commented on Apr 9, 2015

      @vstinner
      Member

      /* 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.

    7. dogbert2 commented on Apr 9, 2015

      dogbert2mannequin
      MannequinAuthor

      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\>


    8. vstinner commented on May 22, 2018

      @vstinner
      Member

      "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.

    9. 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
    10. vstinner commented on May 22, 2018

      @vstinner
      Member

      Oh right, PR 7017 does that: it removes the lseek() call ;-)

    11. transferred this issue fromon Apr 10, 2022
    12. added
      3.13only security fixes
      and removed on May 14, 2023
    13. serhiy-storchaka commented on Aug 9, 2026

      @serhiy-storchaka
      Member

      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.

    14. added a commit that references this issue on Aug 9, 2026
    15. added a commit that references this issue on Aug 14, 2026
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.13only security fixesOS-windowsextension-modulesC modules in the Modules dirtype-bugAn unexpected behavior, bug, or error

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions