]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
#15676: mmap: add empty file check prior to offset check <- Previous patch was incomplete
authorJesus Cea <jcea@jcea.es>
Mon, 10 Sep 2012 20:49:50 +0000 (22:49 +0200)
committerJesus Cea <jcea@jcea.es>
Mon, 10 Sep 2012 20:49:50 +0000 (22:49 +0200)
Lib/test/test_mmap.py
Modules/mmapmodule.c

index c060e61dda84a84b0b3e3ecb2826feecc99b6aaf..28b52a98e6e4365700341681c73cc54bfe086eba 100644 (file)
@@ -462,11 +462,11 @@ class MmapTests(unittest.TestCase):
     def test_empty_file (self):
         f = open (TESTFN, 'w+b')
         f.close()
-        f = open(TESTFN, "rb")
-        self.assertRaisesRegex(ValueError,
-                               "cannot mmap an empty file",
-                               mmap.mmap, f.fileno(), 0, access=mmap.ACCESS_READ)
-        f.close()
+        with open(TESTFN, "rb") as f :
+            self.assertRaisesRegex(ValueError,
+                                   "cannot mmap an empty file",
+                                   mmap.mmap, f.fileno(), 0,
+                                   access=mmap.ACCESS_READ)
 
     def test_offset (self):
         f = open (TESTFN, 'w+b')
index e23613eb12552277949b5a5bbe51486201ac8622..b993fdf92de408f0c11c3c76d93b1b660024d751 100644 (file)
@@ -1342,6 +1342,11 @@ new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict)
             }
 
             size = (((PY_LONG_LONG) high) << 32) + low;
+            if (size == 0) {
+                PyErr_SetString(PyExc_ValueError,
+                                "cannot mmap an empty file");
+                return NULL;
+            }
             if (offset >= size) {
                 PyErr_SetString(PyExc_ValueError,
                                 "mmap offset is greater than file size");