diff -r abb858a0740f Lib/test/test_mmap.py --- a/Lib/test/test_mmap.py Fri Sep 07 02:24:58 2012 +0200 +++ b/Lib/test/test_mmap.py Sun Sep 09 10:59:13 2012 -0400 @@ -459,6 +459,22 @@ f.flush () return mmap.mmap (f.fileno(), 0) + 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(str(e), "cannot mmap an empty file") + except: + f.close() + self.fail("unexpected exception: " + str(e)) + def test_offset (self): f = open (TESTFN, 'w+b') diff -r abb858a0740f Modules/mmapmodule.c --- a/Modules/mmapmodule.c Fri Sep 07 02:24:58 2012 +0200 +++ b/Modules/mmapmodule.c Sun Sep 09 10:59:13 2012 -0400 @@ -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");