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

jesus.cea python-checkins at python.org
Mon Sep 10 22:50:45 CEST 2012


http://hg.python.org/cpython/rev/88a88c32661e
changeset:   78977:88a88c32661e
branch:      3.2
parent:      78972:4f21f7532038
user:        Jesus Cea <jcea at jcea.es>
date:        Mon Sep 10 22:49:50 2012 +0200
summary:
  #15676: mmap: add empty file check prior to offset check <- Previous patch was incomplete

files:
  Lib/test/test_mmap.py |  10 +++++-----
  Modules/mmapmodule.c  |   5 +++++
  2 files changed, 10 insertions(+), 5 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
@@ -462,11 +462,11 @@
     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')
diff --git a/Modules/mmapmodule.c b/Modules/mmapmodule.c
--- a/Modules/mmapmodule.c
+++ b/Modules/mmapmodule.c
@@ -1342,6 +1342,11 @@
             }
 
             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");

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


More information about the Python-checkins mailing list