]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Closes #15676: mmap: add empty file check prior to offset check
authorJesus Cea <jcea@jcea.es>
Sun, 9 Sep 2012 22:22:39 +0000 (00:22 +0200)
committerJesus Cea <jcea@jcea.es>
Sun, 9 Sep 2012 22:22:39 +0000 (00:22 +0200)
Lib/test/test_mmap.py
Misc/NEWS
Modules/mmapmodule.c

index 2c2863ebbe34a570c8e0a6ea8df0d8dbabc6d175..31b369517c5221eebb80146236d16aca03b851a5 100644 (file)
@@ -466,6 +466,22 @@ class MmapTests(unittest.TestCase):
         f.flush ()
         return mmap.mmap (f.fileno(), 0)
 
+    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))
+
     def test_offset (self):
         f = open (TESTFN, 'w+b')
 
index c0736199b7769bf806103892727afada1dd9c117..680cb18fdd200a68914d4e88e1a3bae7b3ca4f64 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -103,6 +103,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #15676: Now "mmap" check for empty files before doing the
+  offset check.  Patch by Steven Willis.
+
 - Issue #15340: Fix importing the random module when /dev/urandom cannot
   be opened.  This was a regression caused by the hash randomization patch.
 
index a5027f51e2c0323148ffc4872709a38cd9eaff94..8cf0e8743fed8c2bb628a6962bc6f504d133c460 100644 (file)
@@ -1189,6 +1189,11 @@ new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict)
     if (fd != -1 && fstat(fd, &st) == 0 && S_ISREG(st.st_mode)) {
         if (map_size == 0) {
             off_t calc_size;
+            if (st.st_size == 0) {
+                PyErr_SetString(PyExc_ValueError,
+                                "cannot mmap an empty file");
+                return NULL;
+            }
             if (offset >= st.st_size) {
                 PyErr_SetString(PyExc_ValueError,
                                 "mmap offset is greater than file size");