summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoão Abecasis <joao@abecasis.name>2009-10-28 14:09:04 +0100
committerJoão Abecasis <joao@abecasis.name>2009-10-28 14:45:18 +0100
commit79da7bb4739f9f63178ce5146702dce6b8feafb9 (patch)
tree5a885231046f4752e236c2d9663a4fc26cc193ea
parent22b223c31ff961f52f62eaf20aa571b71dfe3bb8 (diff)
Don't try to mmap past EOF
On Mac OS, mmap would succeed, returning a valid pointer, but trying to read from it would result in a SIGBUS. By adding this check we commit to a safe cross-platform behavior users can depend on. Reviewed-by: Thiago Macieira
-rw-r--r--src/corelib/io/qfsfileengine_unix.cpp6
1 files changed, 6 insertions, 0 deletions
diff --git a/src/corelib/io/qfsfileengine_unix.cpp b/src/corelib/io/qfsfileengine_unix.cpp
index 6af567429e..7824520890 100644
--- a/src/corelib/io/qfsfileengine_unix.cpp
+++ b/src/corelib/io/qfsfileengine_unix.cpp
@@ -1250,6 +1250,12 @@ uchar *QFSFileEnginePrivate::map(qint64 offset, qint64 size, QFile::MemoryMapFla
return 0;
}
+ // If we know the mapping will extend beyond EOF, fail early to avoid
+ // undefined behavior. Otherwise, let mmap have its say.
+ if (doStat()
+ && (QT_OFF_T(size) > st.st_size - QT_OFF_T(offset)))
+ return 0;
+
int access = 0;
if (openMode & QIODevice::ReadOnly) access |= PROT_READ;
if (openMode & QIODevice::WriteOnly) access |= PROT_WRITE;