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

Skip to content

Commit 6d40134

Browse files
committed
Merge
2 parents 40f42d9 + 0d09ba8 commit 6d40134

2 files changed

Lines changed: 18 additions & 11 deletions

File tree

Lib/test/test_mmap.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,13 @@ def test_large_offset(self):
721721

722722
def test_large_filesize(self):
723723
with self._make_test_file(0x17FFFFFFF, b" ") as f:
724+
if sys.maxsize < 0x180000000:
725+
# On 32 bit platforms the file is larger than sys.maxsize so
726+
# mapping the whole file should fail -- Issue #16743
727+
with self.assertRaises(OverflowError):
728+
mmap.mmap(f.fileno(), 0x180000000, access=mmap.ACCESS_READ)
729+
with self.assertRaises(ValueError):
730+
mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)
724731
with mmap.mmap(f.fileno(), 0x10000, access=mmap.ACCESS_READ) as m:
725732
self.assertEqual(m.size(), 0x180000000)
726733

Modules/mmapmodule.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,7 +1162,6 @@ new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict)
11621162
# endif
11631163
if (fd != -1 && fstat(fd, &st) == 0 && S_ISREG(st.st_mode)) {
11641164
if (map_size == 0) {
1165-
off_t calc_size;
11661165
if (st.st_size == 0) {
11671166
PyErr_SetString(PyExc_ValueError,
11681167
"cannot mmap an empty file");
@@ -1173,13 +1172,12 @@ new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict)
11731172
"mmap offset is greater than file size");
11741173
return NULL;
11751174
}
1176-
calc_size = st.st_size - offset;
1177-
map_size = calc_size;
1178-
if (map_size != calc_size) {
1175+
if (st.st_size - offset > PY_SSIZE_T_MAX) {
11791176
PyErr_SetString(PyExc_ValueError,
11801177
"mmap length is too large");
1181-
return NULL;
1182-
}
1178+
return NULL;
1179+
}
1180+
map_size = (Py_ssize_t) (st.st_size - offset);
11831181
} else if (offset + (size_t)map_size > st.st_size) {
11841182
PyErr_SetString(PyExc_ValueError,
11851183
"mmap length is greater than file size");
@@ -1376,11 +1374,13 @@ new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict)
13761374
Py_DECREF(m_obj);
13771375
return NULL;
13781376
}
1379-
if (offset - size > PY_SSIZE_T_MAX)
1380-
/* Map area too large to fit in memory */
1381-
m_obj->size = (Py_ssize_t) -1;
1382-
else
1383-
m_obj->size = (Py_ssize_t) (size - offset);
1377+
if (size - offset > PY_SSIZE_T_MAX) {
1378+
PyErr_SetString(PyExc_ValueError,
1379+
"mmap length is too large");
1380+
Py_DECREF(m_obj);
1381+
return NULL;
1382+
}
1383+
m_obj->size = (Py_ssize_t) (size - offset);
13841384
} else {
13851385
m_obj->size = map_size;
13861386
size = offset + map_size;

0 commit comments

Comments
 (0)