diff -r 14be8f02e694 Lib/test/test_mmap.py --- a/Lib/test/test_mmap.py Sat Jan 15 06:53:33 2011 +0100 +++ b/Lib/test/test_mmap.py Sat Jan 15 17:12:21 2011 +0200 @@ -320,6 +320,21 @@ mf.close() f.close() + def test_length_0_offset(self): + # test mapping of remainder of file by passing 0 for map length + # with an offset doesn't cause a segfault. + if hasattr(os, "stat"): + f = open(TESTFN, "wb+") + + f.write(49152 * b'm') # Arbitrary character + f.close() + + f = open(TESTFN, "rb") + mf = mmap.mmap(f.fileno(), 0, offset=40960, access=mmap.ACCESS_READ) + self.assertRaises(IndexError, mf.__getitem__, 45000) + mf.close() + f.close() + def test_move(self): # make move works everywhere (64-bit format problem earlier) f = open(TESTFN, 'wb+') diff -r 14be8f02e694 Modules/mmapmodule.c --- a/Modules/mmapmodule.c Sat Jan 15 06:53:33 2011 +0100 +++ b/Modules/mmapmodule.c Sat Jan 15 17:12:21 2011 +0200 @@ -1116,7 +1116,7 @@ # endif if (fd != -1 && fstat(fd, &st) == 0 && S_ISREG(st.st_mode)) { if (map_size == 0) { - map_size = st.st_size; + map_size = st.st_size - offset; } else if ((size_t)offset + (size_t)map_size > st.st_size) { PyErr_SetString(PyExc_ValueError, "mmap length is greater than file size");