]> granicus.if.org Git - python/commitdiff
Patch #2111: Avoid mmap segfault when modifying a PROT_READ block.
authorMartin v. Löwis <martin@v.loewis.de>
Fri, 23 May 2008 14:30:44 +0000 (14:30 +0000)
committerMartin v. Löwis <martin@v.loewis.de>
Fri, 23 May 2008 14:30:44 +0000 (14:30 +0000)
Lib/test/test_mmap.py
Misc/NEWS
Modules/mmapmodule.c

index d2a24770cf203d74620ae71ca14c4e794a9e1064..2fe0211f63ba4a1b1397c796568f28541003ad5c 100644 (file)
@@ -380,6 +380,23 @@ def test_both():
     finally:
         os.unlink(TESTFN)
 
+    # Test that setting access to PROT_READ gives exception
+    # rather than crashing
+    if hasattr(mmap, "PROT_READ"):
+        try:
+            mapsize = 10
+            open(TESTFN, "wb").write("a"*mapsize)
+            f = open(TESTFN, "rb")
+            m = mmap.mmap(f.fileno(), mapsize, prot=mmap.PROT_READ)
+            try:
+                m.write("foo")
+            except TypeError:
+                pass
+            else:
+                verify(0, "PROT_READ is not working")
+        finally:
+            os.unlink(TESTFN)
+
 def test_anon():
     print "  anonymous mmap.mmap(-1, PAGESIZE)..."
     m = mmap.mmap(-1, PAGESIZE)
index f7d732717c699dfc778584a6ce41acadf6c36454..2a60b051cbea750151769f6bba5969ad42bb647e 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -86,6 +86,8 @@ Library
 Extension Modules
 -----------------
 
+- Patch #2111: Avoid mmap segfault when modifying a PROT_READ block.
+
 - zlib.decompressobj().flush(value) no longer crashes the interpreter when
   passed a value less than or equal to zero.
 
index 091ecd6e1c30e82be2e0f4ba6c29e9c92875caf4..3565ab6add0ba03b278047b4de0bc5077ed634da 100644 (file)
@@ -881,6 +881,10 @@ new_mmap_object(PyObject *self, PyObject *args, PyObject *kwdict)
                                    "mmap invalid access parameter.");
        }
 
+    if (prot == PROT_READ) {
+        access = ACCESS_READ;
+    }
+
 #ifdef HAVE_FSTAT
 #  ifdef __VMS
        /* on OpenVMS we must ensure that all bytes are written to the file */