From 1f2799bef45673419e96ad4ef6a923c59d1c6a85 Mon Sep 17 00:00:00 2001 From: Jesus Cea Date: Mon, 10 Sep 2012 22:49:50 +0200 Subject: #15676: mmap: add empty file check prior to offset check <- Previous patch was incomplete --- 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 index c060e61..28b52a9 100644 --- a/Lib/test/test_mmap.py +++ b/Lib/test/test_mmap.py @@ -462,11 +462,11 @@ class MmapTests(unittest.TestCase): 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 index e23613e..b993fdf 100644 --- a/Modules/mmapmodule.c +++ b/Modules/mmapmodule.c @@ -1342,6 +1342,11 @@ new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict) } 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"); -- cgit v0.12