Closes #15676: mmap: add empty file check prior to offset check
authorJesus Cea <jcea@jcea.es>
Sun, 9 Sep 2012 22:27:55 +0000 (00:27 +0200)
committerJesus Cea <jcea@jcea.es>
Sun, 9 Sep 2012 22:27:55 +0000 (00:27 +0200)
Lib/test/test_mmap.py
Misc/NEWS
Modules/mmapmodule.c

index 0e18aabd662a9210ed09c80b41143ff327f53703..c060e61dda84a84b0b3e3ecb2826feecc99b6aaf 100644 (file)
@@ -459,6 +459,15 @@ 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")
+        self.assertRaisesRegex(ValueError,
+                               "cannot mmap an empty file",
+                               mmap.mmap, f.fileno(), 0, access=mmap.ACCESS_READ)
+        f.close()
+
     def test_offset (self):
         f = open (TESTFN, 'w+b')
 
index 8672b0a468b8e226cc1b463c8daf0a1196048e00..725fb9e5c13eb64c2424783c44dc48da93c34eac 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -463,6 +463,9 @@ Library
 Extension Modules
 -----------------
 
+- Issue #15676: Now "mmap" check for empty files before doing the
+  offset check.  Patch by Steven Willis.
+
 - Issue #6493: An issue in ctypes on Windows that caused structure bitfields
   of type ctypes.c_uint32 and width 32 to incorrectly be set has been fixed.
 
index da9283bf1b783524b2158ddd2e56e874d6359428..e23613eb12552277949b5a5bbe51486201ac8622 100644 (file)
@@ -1141,6 +1141,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");