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

Skip to content

Commit 0d09ba8

Browse files
committed
Issue #16743: Fix mmap overflow check on 32 bit Windows
1 parent 7e01911 commit 0d09ba8

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
@@ -693,6 +693,13 @@ def test_large_offset(self):
693693

694694
def test_large_filesize(self):
695695
with self._make_test_file(0x17FFFFFFF, b" ") as f:
696+
if sys.maxsize < 0x180000000:
697+
# On 32 bit platforms the file is larger than sys.maxsize so
698+
# mapping the whole file should fail -- Issue #16743
699+
with self.assertRaises(OverflowError):
700+
mmap.mmap(f.fileno(), 0x180000000, access=mmap.ACCESS_READ)
701+
with self.assertRaises(ValueError):
702+
mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)
696703
with mmap.mmap(f.fileno(), 0x10000, access=mmap.ACCESS_READ) as m:
697704
self.assertEqual(m.size(), 0x180000000)
698705

Modules/mmapmodule.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,7 +1140,6 @@ new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict)
11401140
# endif
11411141
if (fd != -1 && fstat(fd, &st) == 0 && S_ISREG(st.st_mode)) {
11421142
if (map_size == 0) {
1143-
off_t calc_size;
11441143
if (st.st_size == 0) {
11451144
PyErr_SetString(PyExc_ValueError,
11461145
"cannot mmap an empty file");
@@ -1151,13 +1150,12 @@ new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict)
11511150
"mmap offset is greater than file size");
11521151
return NULL;
11531152
}
1154-
calc_size = st.st_size - offset;
1155-
map_size = calc_size;
1156-
if (map_size != calc_size) {
1153+
if (st.st_size - offset > PY_SSIZE_T_MAX) {
11571154
PyErr_SetString(PyExc_ValueError,
11581155
"mmap length is too large");
1159-
return NULL;
1160-
}
1156+
return NULL;
1157+
}
1158+
map_size = (Py_ssize_t) (st.st_size - offset);
11611159
} else if (offset + (size_t)map_size > st.st_size) {
11621160
PyErr_SetString(PyExc_ValueError,
11631161
"mmap length is greater than file size");
@@ -1354,11 +1352,13 @@ new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict)
13541352
Py_DECREF(m_obj);
13551353
return NULL;
13561354
}
1357-
if (offset - size > PY_SSIZE_T_MAX)
1358-
/* Map area too large to fit in memory */
1359-
m_obj->size = (Py_ssize_t) -1;
1360-
else
1361-
m_obj->size = (Py_ssize_t) (size - offset);
1355+
if (size - offset > PY_SSIZE_T_MAX) {
1356+
PyErr_SetString(PyExc_ValueError,
1357+
"mmap length is too large");
1358+
Py_DECREF(m_obj);
1359+
return NULL;
1360+
}
1361+
m_obj->size = (Py_ssize_t) (size - offset);
13621362
} else {
13631363
m_obj->size = map_size;
13641364
size = offset + map_size;

0 commit comments

Comments
 (0)