# HG changeset patch # Parent 9d51225480966a61b22fc45e62c2d50373d96ca8 Issue #16743: Fix calling mmap() with length=0 on large files for Windows. diff -r 9d5122548096 Lib/test/test_mmap.py --- a/Lib/test/test_mmap.py Sun Dec 16 21:11:35 2012 +0100 +++ b/Lib/test/test_mmap.py Wed Dec 26 17:48:11 2012 +0000 @@ -721,6 +721,13 @@ def test_large_filesize(self): with self._make_test_file(0x17FFFFFFF, b" ") as f: + if sys.maxsize < 0x180000000: + # On 32 bit platforms the file is larger than sys.maxsize so + # mapping the whole file should fail -- Issue #16743 + with self.assertRaises(OverflowError): + mmap.mmap(f.fileno(), 0x180000000, access=mmap.ACCESS_READ) + with self.assertRaises(ValueError): + mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ) with mmap.mmap(f.fileno(), 0x10000, access=mmap.ACCESS_READ) as m: self.assertEqual(m.size(), 0x180000000) diff -r 9d5122548096 Modules/mmapmodule.c --- a/Modules/mmapmodule.c Sun Dec 16 21:11:35 2012 +0100 +++ b/Modules/mmapmodule.c Wed Dec 26 17:48:11 2012 +0000 @@ -1162,7 +1162,6 @@ # endif 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"); @@ -1173,13 +1172,12 @@ "mmap offset is greater than file size"); return NULL; } - calc_size = st.st_size - offset; - map_size = calc_size; - if (map_size != calc_size) { + if (st.st_size - offset > PY_SSIZE_T_MAX) { PyErr_SetString(PyExc_ValueError, "mmap length is too large"); - return NULL; - } + return NULL; + } + map_size = (Py_ssize_t) (st.st_size - offset); } else if (offset + (size_t)map_size > st.st_size) { PyErr_SetString(PyExc_ValueError, "mmap length is greater than file size"); @@ -1376,11 +1374,13 @@ Py_DECREF(m_obj); return NULL; } - if (offset - size > PY_SSIZE_T_MAX) - /* Map area too large to fit in memory */ - m_obj->size = (Py_ssize_t) -1; - else - m_obj->size = (Py_ssize_t) (size - offset); + if (size - offset > PY_SSIZE_T_MAX) { + PyErr_SetString(PyExc_ValueError, + "mmap length is too large"); + Py_DECREF(m_obj); + return NULL; + } + m_obj->size = (Py_ssize_t) (size - offset); } else { m_obj->size = map_size; size = offset + map_size;