Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 85f4615

Browse files
committed
Issue #10916: mmap should not segfault when a file is mapped using 0 as
length and a non-zero offset, and an attempt to read past the end of file is made (IndexError is raised instead). Patch by Ross Lagerwall. Requested by Georg.
1 parent f5cf435 commit 85f4615

3 files changed

Lines changed: 18 additions & 1 deletion

File tree

Lib/test/test_mmap.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,19 @@ def test_entire_file(self):
320320
mf.close()
321321
f.close()
322322

323+
def test_length_0_offset(self):
324+
# Issue #10916: test mapping of remainder of file by passing 0 for
325+
# map length with an offset doesn't cause a segfault.
326+
if not hasattr(os, "stat"):
327+
self.skipTest("needs os.stat")
328+
with open(TESTFN, "wb+") as f:
329+
f.write(49152 * b'm') # Arbitrary character
330+
331+
with open(TESTFN, "rb") as f:
332+
mf = mmap.mmap(f.fileno(), 0, offset=40960, access=mmap.ACCESS_READ)
333+
self.assertRaises(IndexError, mf.__getitem__, 45000)
334+
mf.close()
335+
323336
def test_move(self):
324337
# make move works everywhere (64-bit format problem earlier)
325338
f = open(TESTFN, 'wb+')

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ Core and Builtins
4343
Library
4444
-------
4545

46+
- Issue #10916: mmap should not segfault when a file is mapped using 0 as
47+
length and a non-zero offset, and an attempt to read past the end of file
48+
is made (IndexError is raised instead). Patch by Ross Lagerwall.
49+
4650
- Issue #10907: Warn OS X 10.6 IDLE users to use ActiveState Tcl/Tk 8.5,
4751
rather than the currently problematic Apple-supplied one, when running
4852
with the 64-/32-bit installer variant.

Modules/mmapmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1116,7 +1116,7 @@ new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict)
11161116
# endif
11171117
if (fd != -1 && fstat(fd, &st) == 0 && S_ISREG(st.st_mode)) {
11181118
if (map_size == 0) {
1119-
map_size = st.st_size;
1119+
map_size = st.st_size - offset;
11201120
} else if ((size_t)offset + (size_t)map_size > st.st_size) {
11211121
PyErr_SetString(PyExc_ValueError,
11221122
"mmap length is greater than file size");

0 commit comments

Comments
 (0)