From 43aea5c9402ff0a4c2bc8365ea9aa2ff3447a356 Mon Sep 17 00:00:00 2001 From: suzker Date: Tue, 24 May 2016 16:35:15 -0700 Subject: [PATCH 1/3] added filesize overflow check for mmap on 32bit os --- src/AbstractDiskWriter.cc | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/AbstractDiskWriter.cc b/src/AbstractDiskWriter.cc index 4d62bf97..df2d0a85 100644 --- a/src/AbstractDiskWriter.cc +++ b/src/AbstractDiskWriter.cc @@ -373,6 +373,14 @@ void AbstractDiskWriter::ensureMmapWrite(size_t len, int64_t offset) return; } + uint32_t filesize_lo = filesize & 0xffffffffu; + if (sizeof(off_t) > sizeof(size_t) && (filesize_lo) > 0) { + // filesize could overflow in 32bit OS with 64bit off_t type + // the filesize will be truncated if provided as a 32bit size_t + enableMmap_ = false; + return; + } + int errNum = 0; if (static_cast(len + offset) <= filesize) { #ifdef __MINGW32__ From ef0a2e601f64904b87cf56244e5600a1c6baa3e9 Mon Sep 17 00:00:00 2001 From: suzker Date: Wed, 25 May 2016 07:43:57 -0700 Subject: [PATCH 2/3] Compare filesize directly to the numeric max of size_t --- src/AbstractDiskWriter.cc | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/AbstractDiskWriter.cc b/src/AbstractDiskWriter.cc index df2d0a85..9e3dac4b 100644 --- a/src/AbstractDiskWriter.cc +++ b/src/AbstractDiskWriter.cc @@ -373,8 +373,7 @@ void AbstractDiskWriter::ensureMmapWrite(size_t len, int64_t offset) return; } - uint32_t filesize_lo = filesize & 0xffffffffu; - if (sizeof(off_t) > sizeof(size_t) && (filesize_lo) > 0) { + if (static_cast(std::numeric_limits::max()) < static_cast(filesize)) { // filesize could overflow in 32bit OS with 64bit off_t type // the filesize will be truncated if provided as a 32bit size_t enableMmap_ = false; From fa434319746e7ca9fb023cf8b1a4c1d0de67ed58 Mon Sep 17 00:00:00 2001 From: suzker Date: Fri, 27 May 2016 02:07:02 -0700 Subject: [PATCH 3/3] fixed mmap failure check with MAP_FAILED flag --- src/AbstractDiskWriter.cc | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/AbstractDiskWriter.cc b/src/AbstractDiskWriter.cc index 9e3dac4b..a6191490 100644 --- a/src/AbstractDiskWriter.cc +++ b/src/AbstractDiskWriter.cc @@ -398,10 +398,14 @@ void AbstractDiskWriter::ensureMmapWrite(size_t len, int64_t offset) errNum = GetLastError(); } #else // !__MINGW32__ - mapaddr_ = reinterpret_cast(mmap( - nullptr, filesize, PROT_READ | PROT_WRITE, MAP_SHARED, fd_, 0)); - if (!mapaddr_) { + void * pa = mmap(nullptr, filesize, PROT_READ | PROT_WRITE, MAP_SHARED, fd_, 0); + + if (pa == MAP_FAILED) { errNum = errno; + mapaddr_ = nullptr; + } + else { + mapaddr_ = reinterpret_cast(pa); } #endif // !__MINGW32__ if (mapaddr_) {