[Python-checkins] cpython (3.2): Closes #15676: mmap: add empty file check prior to offset check

jesus.cea python-checkins at python.org
Mon Sep 10 01:24:45 CEST 2012


http://hg.python.org/cpython/rev/d85f80b31b54
changeset:   78929:d85f80b31b54
branch:      3.2
parent:      78927:b05547e8ff92
user:        Jesus Cea <jcea at jcea.es>
date:        Mon Sep 10 00:27:55 2012 +0200
summary:
  Closes #15676: mmap: add empty file check prior to offset check

files:
  Lib/test/test_mmap.py |  9 +++++++++
  Misc/NEWS             |  3 +++
  Modules/mmapmodule.c  |  5 +++++
  3 files changed, 17 insertions(+), 0 deletions(-)


diff --git a/Lib/test/test_mmap.py b/Lib/test/test_mmap.py
--- a/Lib/test/test_mmap.py
+++ b/Lib/test/test_mmap.py
@@ -459,6 +459,15 @@
         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')
 
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -463,6 +463,9 @@
 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.
 
diff --git a/Modules/mmapmodule.c b/Modules/mmapmodule.c
--- a/Modules/mmapmodule.c
+++ b/Modules/mmapmodule.c
@@ -1141,6 +1141,11 @@
     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");

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list