This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Fix mmap module core dump with unix
Type: Stage:
Components: Extension Modules Versions: Python 2.2
process
Status: closed Resolution: accepted
Dependencies: Superseder:
Assigned To: nnorwitz Nosy List: cspence, nnorwitz
Priority: normal Keywords: patch

Created on 2003-01-10 19:03 by cspence, last changed 2022-04-10 16:06 by admin. This issue is now closed.

Messages (4)
msg42397 - (view) Author: Clay Spence (cspence) Date: 2003-01-10 19:03
Calling the close method of an mmap object more than
once would cause a segmentation fault for me (python
2.2.2, compiled with Sun's Forte compilers, though I
doubt that matters, Solaris 2.8 on an ultra-sparc). 
The following code would do it reliably:

"""Demonstrate mmap bug?"""

import os
f=open('junk', 'w')
f.write(2**24 * 'a') # Arbitrary character
f.close()
s = os.stat('junk')    
s.st_size

from mmap import *
f = open('junk')
mf = mmap(f.fileno(), 2**24, access=ACCESS_READ)
print mf[0], mf[2**23]
mf.close()
mf.close()

The problem seems to be that mmap_close_method does an
munmap of the data and then sets it to NULL without
checking first whether the data pointer is already
NULL.  The windows part already checks this.  (Please
excuse me if I mess up the protocol for suggesting
patches.)
msg42398 - (view) Author: Neal Norwitz (nnorwitz) * (Python committer) Date: 2003-01-10 20:22
Logged In: YES 
user_id=33168

There's no patch attached.  I think I can guess what it
would look like though. :-)  This code works on linux, but I
was able to provoke the problem on Solaris.

I suspect this problem affects 2.2.2 as well.  Assigning to
me, I'll fix, add a test, and NEWS.
msg42399 - (view) Author: Clay Spence (cspence) Date: 2003-01-10 20:36
Logged In: YES 
user_id=685289

Sorry, I tried to attach a patch file and it failed to
upload.  It is pretty simple, and looks like:

diff -c -d -T --recursive mmapmodule.c~ mmapmodule.c
*** mmapmodule.c~       Thu Sep  5 18:30:03 2002
--- mmapmodule.c        Fri Jan 10 13:53:28 2003
***************
*** 141,148 ****
        #endif /* MS_WIN32 */
 
        #ifdef UNIX
!               munmap(self->data, self->size);
!               self->data = NULL;
        #endif
 
                Py_INCREF (Py_None);
--- 141,150 ----
        #endif /* MS_WIN32 */
 
        #ifdef UNIX
!               if (self->data != NULL) {
!                       munmap(self->data, self->size);
!                       self->data = NULL;
!               }
        #endif
 
                Py_INCREF (Py_None);
msg42400 - (view) Author: Neal Norwitz (nnorwitz) * (Python committer) Date: 2003-01-10 21:05
Logged In: YES 
user_id=33168

That's what I guessed. :-)  Thanks!

Checked in as:
 Modules/mmapmodule.c 2.42 and 2.35.6.4
 Lib/test/test_mmap.py 1.29 and 1.19.8.6
 Misc/NEWS 1.610 and 1.337.2.4.2.52
History
Date User Action Args
2022-04-10 16:06:07adminsetgithub: 37757
2003-01-10 19:03:52cspencecreate