Merge branch 'c3V6a2Vy-fix-mmap-check'

pull/675/head
Tatsuhiro Tsujikawa 2016-05-31 22:19:00 +09:00
commit 134c804b86
1 changed files with 14 additions and 3 deletions

View File

@ -373,6 +373,13 @@ void AbstractDiskWriter::ensureMmapWrite(size_t len, int64_t offset)
return; return;
} }
if (static_cast<uint64_t>(std::numeric_limits<size_t>::max()) < static_cast<uint64_t>(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;
return;
}
int errNum = 0; int errNum = 0;
if (static_cast<int64_t>(len + offset) <= filesize) { if (static_cast<int64_t>(len + offset) <= filesize) {
#ifdef __MINGW32__ #ifdef __MINGW32__
@ -391,10 +398,14 @@ void AbstractDiskWriter::ensureMmapWrite(size_t len, int64_t offset)
errNum = GetLastError(); errNum = GetLastError();
} }
#else // !__MINGW32__ #else // !__MINGW32__
mapaddr_ = reinterpret_cast<unsigned char*>(mmap( void * pa = mmap(nullptr, filesize, PROT_READ | PROT_WRITE, MAP_SHARED, fd_, 0);
nullptr, filesize, PROT_READ | PROT_WRITE, MAP_SHARED, fd_, 0));
if (!mapaddr_) { if (pa == MAP_FAILED) {
errNum = errno; errNum = errno;
mapaddr_ = nullptr;
}
else {
mapaddr_ = reinterpret_cast<unsigned char*>(pa);
} }
#endif // !__MINGW32__ #endif // !__MINGW32__
if (mapaddr_) { if (mapaddr_) {