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

Skip to content

Commit 6107a4e

Browse files
committed
Merged revisions 88131 via svnmerge from
svn+ssh://[email protected]/python/branches/py3k ........ r88131 | antoine.pitrou | 2011-01-20 22:07:24 +0100 (jeu., 20 janv. 2011) | 6 lines Issue #10955: Fix a potential crash when trying to mmap() a file past its length. Initial patch by Ross Lagerwall. This fixes a regression introduced by r88022. ........
1 parent 710e966 commit 6107a4e

3 files changed

Lines changed: 27 additions & 0 deletions

File tree

Lib/test/test_mmap.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,19 @@ def test_length_0_offset(self):
341341
finally:
342342
mf.close()
343343

344+
def test_length_0_large_offset(self):
345+
# Issue #10959: test mapping of a file by passing 0 for
346+
# map length with a large offset doesn't cause a segfault.
347+
if not hasattr(os, "stat"):
348+
self.skipTest("needs os.stat")
349+
350+
with open(TESTFN, "wb") as f:
351+
f.write(115699 * b'm') # Arbitrary character
352+
353+
with open(TESTFN, "w+b") as f:
354+
self.assertRaises(ValueError, mmap.mmap, f.fileno(), 0,
355+
offset=2147418112)
356+
344357
def test_move(self):
345358
# make move works everywhere (64-bit format problem earlier)
346359
f = open(TESTFN, 'wb+')

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ Core and Builtins
3737
Library
3838
-------
3939

40+
- Issue #10955: Fix a potential crash when trying to mmap() a file past its
41+
length. Initial patch by Ross Lagerwall.
42+
4043
- Issue #10898: Allow compiling the posix module when the C library defines
4144
a symbol named FSTAT.
4245

Modules/mmapmodule.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,6 +1085,11 @@ new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict)
10851085
# endif
10861086
if (fd != -1 && fstat(fd, &st) == 0 && S_ISREG(st.st_mode)) {
10871087
if (map_size == 0) {
1088+
if (offset >= st.st_size) {
1089+
PyErr_SetString(PyExc_ValueError,
1090+
"mmap offset is greater than file size");
1091+
return NULL;
1092+
}
10881093
map_size = st.st_size - offset;
10891094
} else if ((size_t)offset + (size_t)map_size > st.st_size) {
10901095
PyErr_SetString(PyExc_ValueError,
@@ -1269,6 +1274,12 @@ new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict)
12691274
else
12701275
m_obj->size = low;
12711276
#endif
1277+
if (offset >= m_obj->size) {
1278+
PyErr_SetString(PyExc_ValueError,
1279+
"mmap offset is greater than file size");
1280+
Py_DECREF(m_obj);
1281+
return NULL;
1282+
}
12721283
m_obj->size -= offset;
12731284
} else {
12741285
m_obj->size = map_size;

0 commit comments

Comments
 (0)