[Patches] [ python-Patches-665913 ] Fix mmap module core dump with unix

SourceForge.net noreply@sourceforge.net
Fri, 10 Jan 2003 13:05:40 -0800


Patches item #665913, was opened at 2003-01-10 14:03
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=665913&group_id=5470

Category: Modules
>Group: Python 2.2.x
>Status: Closed
>Resolution: Accepted
Priority: 5
Submitted By: Clay Spence (cspence)
Assigned to: Neal Norwitz (nnorwitz)
Summary: Fix mmap module core dump with unix

Initial Comment:
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.)


----------------------------------------------------------------------

>Comment By: Neal Norwitz (nnorwitz)
Date: 2003-01-10 16:05

Message:
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

----------------------------------------------------------------------

Comment By: Clay Spence (cspence)
Date: 2003-01-10 15:36

Message:
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);


----------------------------------------------------------------------

Comment By: Neal Norwitz (nnorwitz)
Date: 2003-01-10 15:22

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

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=665913&group_id=5470