]> granicus.if.org Git - python/commitdiff
SF bug # 585792, Invalid mmap crashes Python interpreter
authorNeal Norwitz <nnorwitz@gmail.com>
Thu, 5 Sep 2002 21:48:07 +0000 (21:48 +0000)
committerNeal Norwitz <nnorwitz@gmail.com>
Thu, 5 Sep 2002 21:48:07 +0000 (21:48 +0000)
Raise ValueError if user passes a size to mmap which is larger
than the file.

Lib/test/output/test_mmap
Lib/test/test_mmap.py
Misc/NEWS
Modules/mmapmodule.c

index 4adf7027b78fb8f4a5c373700d6fcd12dc89e1ad..1706ad58680585e1d760d3ee3616c0c31984127e 100644 (file)
@@ -24,6 +24,7 @@ test_mmap
   Ensuring that readonly mmap can't be write() to.
   Ensuring that readonly mmap can't be write_byte() to.
   Ensuring that readonly mmap can't be resized.
+  Opening mmap with size too big
   Opening mmap with access=ACCESS_WRITE
   Modifying write-through memory map.
   Opening mmap with access=ACCESS_COPY
index 11edbaf797fe354ec91c65bc97fb9cabe2ee1ade..dd1fb3f359c5fd2ae499d622ad5f634d671b3b82 100644 (file)
@@ -189,6 +189,21 @@ def test_both():
         verify(open(TESTFN, "rb").read() == 'a'*mapsize,
                "Readonly memory map data file was modified")
 
+        print "  Opening mmap with size too big"
+        import sys
+        f = open(TESTFN, "r+b")
+        try:
+            m = mmap.mmap(f.fileno(), mapsize+1)
+        except ValueError:
+            # we do not expect a ValueError on Windows
+            if sys.platform.startswith('win'):
+                verify(0, "Opening mmap with size+1 should work on Windows.")
+            pass
+        else:
+            # we expect a ValueError on Unix, but not on Windows
+            if not sys.platform.startswith('win'):
+                verify(0, "Opening mmap with size+1 should raise ValueError.")
+
         print "  Opening mmap with access=ACCESS_WRITE"
         f = open(TESTFN, "r+b")
         m = mmap.mmap(f.fileno(), mapsize, access=mmap.ACCESS_WRITE)
index f638e50e920c01ab84bbe8c7faff04748dc41009..b877703afb16a957fb9d2c18f800ce2d3629abb6 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -274,6 +274,9 @@ Core and builtins
 
 Extension modules
 
+- If the size passed to mmap.mmap() is larger than the length of the
+  file on non-Windows platforms, a ValueError is raised. [SF bug 585792]
+
 - The xreadlines module is slated for obsolescence.
 
 - The strptime function in the time module is now always available (a
index 2bb6edc8088b095737eb087a260ab4aeee275966..cff3c146e6c8d2b98266ab95a2987437aa2c1a72 100644 (file)
@@ -850,6 +850,9 @@ _GetMapSize(PyObject *o)
 static PyObject *
 new_mmap_object(PyObject *self, PyObject *args, PyObject *kwdict)
 {
+#ifdef HAVE_FSTAT
+       struct stat st;
+#endif
        mmap_object *m_obj;
        PyObject *map_size_obj = NULL;
        int map_size;
@@ -890,7 +893,14 @@ new_mmap_object(PyObject *self, PyObject *args, PyObject *kwdict)
                return PyErr_Format(PyExc_ValueError, 
                                    "mmap invalid access parameter.");
        }
-       
+
+#ifdef HAVE_FSTAT
+       if (fstat(fd, &st) == 0 && (size_t)map_size > st.st_size) {
+               PyErr_SetString(PyExc_ValueError, 
+                               "mmap length is greater than file size");
+               return NULL;
+       }
+#endif
        m_obj = PyObject_New (mmap_object, &mmap_object_type);
        if (m_obj == NULL) {return NULL;}
        m_obj->size = (size_t) map_size;