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

jesus.cea python-checkins at python.org
Mon Sep 10 22:46:01 CEST 2012


http://hg.python.org/cpython/rev/25d477647a2d
changeset:   78976:25d477647a2d
branch:      2.7
user:        Jesus Cea <jcea at jcea.es>
date:        Mon Sep 10 22:45:47 2012 +0200
summary:
  #15676: mmap: add empty file check prior to offset check <- Previous patch was incomplete

files:
  Lib/test/test_mmap.py |  21 +++++++++------------
  Modules/mmapmodule.c  |   5 +++++
  2 files changed, 14 insertions(+), 12 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
@@ -469,18 +469,15 @@
     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))
+        with open(TESTFN, "rb") as f :
+            try:
+                m = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)
+                m.close()
+                self.fail("should not have been able to mmap empty file")
+            except ValueError as e:
+                self.assertEqual(e.message, "cannot mmap an empty file")
+            except:
+                self.fail("unexpected exception: " + str(e))
 
     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
@@ -1388,6 +1388,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