[Python-checkins] cpython (merge 3.2 -> default): #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:46 CEST 2012


http://hg.python.org/cpython/rev/0ac94ae29abe
changeset:   78978:0ac94ae29abe
parent:      78973:4fdc6c501e63
parent:      78977:88a88c32661e
user:        Jesus Cea <jcea at jcea.es>
date:        Mon Sep 10 22:50:21 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
@@ -491,11 +491,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
@@ -1364,6 +1364,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