SF bug # 585792, Invalid mmap crashes Python interpreter

Raise ValueError if user passes a size to mmap which is larger
than the file.
This commit is contained in:
Neal Norwitz 2002-09-05 21:48:07 +00:00
parent 7165af23e6
commit b567392bbf
4 changed files with 30 additions and 1 deletions

View File

@ -24,6 +24,7 @@ test_mmap
Ensuring that readonly mmap can't be write() to. Ensuring that readonly mmap can't be write() to.
Ensuring that readonly mmap can't be write_byte() to. Ensuring that readonly mmap can't be write_byte() to.
Ensuring that readonly mmap can't be resized. Ensuring that readonly mmap can't be resized.
Opening mmap with size too big
Opening mmap with access=ACCESS_WRITE Opening mmap with access=ACCESS_WRITE
Modifying write-through memory map. Modifying write-through memory map.
Opening mmap with access=ACCESS_COPY Opening mmap with access=ACCESS_COPY

View File

@ -189,6 +189,21 @@ def test_both():
verify(open(TESTFN, "rb").read() == 'a'*mapsize, verify(open(TESTFN, "rb").read() == 'a'*mapsize,
"Readonly memory map data file was modified") "Readonly memory map data file was modified")
print " Opening mmap with size too big"
import sys
f = open(TESTFN, "r+b")
try:
m = mmap.mmap(f.fileno(), mapsize+1)
except ValueError:
# we do not expect a ValueError on Windows
if sys.platform.startswith('win'):
verify(0, "Opening mmap with size+1 should work on Windows.")
pass
else:
# we expect a ValueError on Unix, but not on Windows
if not sys.platform.startswith('win'):
verify(0, "Opening mmap with size+1 should raise ValueError.")
print " Opening mmap with access=ACCESS_WRITE" print " Opening mmap with access=ACCESS_WRITE"
f = open(TESTFN, "r+b") f = open(TESTFN, "r+b")
m = mmap.mmap(f.fileno(), mapsize, access=mmap.ACCESS_WRITE) m = mmap.mmap(f.fileno(), mapsize, access=mmap.ACCESS_WRITE)

View File

@ -274,6 +274,9 @@ Core and builtins
Extension modules Extension modules
- If the size passed to mmap.mmap() is larger than the length of the
file on non-Windows platforms, a ValueError is raised. [SF bug 585792]
- The xreadlines module is slated for obsolescence. - The xreadlines module is slated for obsolescence.
- The strptime function in the time module is now always available (a - The strptime function in the time module is now always available (a

View File

@ -850,6 +850,9 @@ _GetMapSize(PyObject *o)
static PyObject * static PyObject *
new_mmap_object(PyObject *self, PyObject *args, PyObject *kwdict) new_mmap_object(PyObject *self, PyObject *args, PyObject *kwdict)
{ {
#ifdef HAVE_FSTAT
struct stat st;
#endif
mmap_object *m_obj; mmap_object *m_obj;
PyObject *map_size_obj = NULL; PyObject *map_size_obj = NULL;
int map_size; int map_size;
@ -891,6 +894,13 @@ new_mmap_object(PyObject *self, PyObject *args, PyObject *kwdict)
"mmap invalid access parameter."); "mmap invalid access parameter.");
} }
#ifdef HAVE_FSTAT
if (fstat(fd, &st) == 0 && (size_t)map_size > st.st_size) {
PyErr_SetString(PyExc_ValueError,
"mmap length is greater than file size");
return NULL;
}
#endif
m_obj = PyObject_New (mmap_object, &mmap_object_type); m_obj = PyObject_New (mmap_object, &mmap_object_type);
if (m_obj == NULL) {return NULL;} if (m_obj == NULL) {return NULL;}
m_obj->size = (size_t) map_size; m_obj->size = (size_t) map_size;