Skip to content

Commit 941bfcc

Browse files
committed
Closes python#15676: mmap: add empty file check prior to offset check
1 parent 7330da4 commit 941bfcc

3 files changed

Lines changed: 17 additions & 0 deletions

File tree

Lib/test/test_mmap.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,15 @@ def make_mmap_file (self, f, halfsize):
459459
f.flush ()
460460
return mmap.mmap (f.fileno(), 0)
461461

462+
def test_empty_file (self):
463+
f = open (TESTFN, 'w+b')
464+
f.close()
465+
f = open(TESTFN, "rb")
466+
self.assertRaisesRegex(ValueError,
467+
"cannot mmap an empty file",
468+
mmap.mmap, f.fileno(), 0, access=mmap.ACCESS_READ)
469+
f.close()
470+
462471
def test_offset (self):
463472
f = open (TESTFN, 'w+b')
464473

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,9 @@ Library
463463
Extension Modules
464464
-----------------
465465

466+
- Issue #15676: Now "mmap" check for empty files before doing the
467+
offset check. Patch by Steven Willis.
468+
466469
- Issue #6493: An issue in ctypes on Windows that caused structure bitfields
467470
of type ctypes.c_uint32 and width 32 to incorrectly be set has been fixed.
468471

Modules/mmapmodule.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,6 +1141,11 @@ new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict)
11411141
if (fd != -1 && fstat(fd, &st) == 0 && S_ISREG(st.st_mode)) {
11421142
if (map_size == 0) {
11431143
off_t calc_size;
1144+
if (st.st_size == 0) {
1145+
PyErr_SetString(PyExc_ValueError,
1146+
"cannot mmap an empty file");
1147+
return NULL;
1148+
}
11441149
if (offset >= st.st_size) {
11451150
PyErr_SetString(PyExc_ValueError,
11461151
"mmap offset is greater than file size");

0 commit comments

Comments
 (0)