]> granicus.if.org Git - python/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:45:47 +0000 (22:45 +0200)
committerJesus Cea <jcea@jcea.es>
Mon, 10 Sep 2012 20:45:47 +0000 (22:45 +0200)
Lib/test/test_mmap.py
Modules/mmapmodule.c

index 31b369517c5221eebb80146236d16aca03b851a5..0701b9621d82b389c4b46d7a45c8e391436efe96 100644 (file)
@@ -469,18 +469,15 @@ class MmapTests(unittest.TestCase):
     def test_empty_file (self):
         f = open (TESTFN, 'w+b')
         f.close()
-        f = open(TESTFN, "rb")
-        try:
-            m = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)
-            m.close()
-            f.close()
-            self.fail("should not have been able to mmap empty file")
-        except ValueError as e:
-            f.close()
-            self.assertEqual(e.message, "cannot mmap an empty file")
-        except:
-            f.close()
-            self.fail("unexpected exception: " + str(e))
+        with open(TESTFN, "rb") as f :
+            try:
+                m = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)
+                m.close()
+                self.fail("should not have been able to mmap empty file")
+            except ValueError as e:
+                self.assertEqual(e.message, "cannot mmap an empty file")
+            except:
+                self.fail("unexpected exception: " + str(e))
 
     def test_offset (self):
         f = open (TESTFN, 'w+b')
index 8cf0e8743fed8c2bb628a6962bc6f504d133c460..62d934c607fafb2d9aebd090a19b41e4a5220339 100644 (file)
@@ -1388,6 +1388,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");