summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2016-05-31 22:16:42 +0900
committerTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2016-05-31 22:16:42 +0900
commiteac72b90452a5af61108b53f80a41aebcc227906 (patch)
tree0224691e9d637c48e5a3a4505edebdc72797c838 /src
parent3e00be26e8f8fe44c7344a3e8026031a99b22400 (diff)
parentfa434319746e7ca9fb023cf8b1a4c1d0de67ed58 (diff)
Merge branch 'fix-mmap-check' of https://github.com/c3V6a2Vy/aria2 into c3V6a2Vy-fix-mmap-check
Diffstat (limited to 'src')
-rw-r--r--src/AbstractDiskWriter.cc17
1 files changed, 14 insertions, 3 deletions
diff --git a/src/AbstractDiskWriter.cc b/src/AbstractDiskWriter.cc
index 4d62bf97..a6191490 100644
--- a/src/AbstractDiskWriter.cc
+++ b/src/AbstractDiskWriter.cc
@@ -373,6 +373,13 @@ void AbstractDiskWriter::ensureMmapWrite(size_t len, int64_t offset)
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;
if (static_cast<int64_t>(len + offset) <= filesize) {
#ifdef __MINGW32__
@@ -391,10 +398,14 @@ void AbstractDiskWriter::ensureMmapWrite(size_t len, int64_t offset)
errNum = GetLastError();
}
#else // !__MINGW32__
- mapaddr_ = reinterpret_cast<unsigned char*>(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<unsigned char*>(pa);
}
#endif // !__MINGW32__
if (mapaddr_) {