From e95b703b7fe7a7d7c116195e4e0c44b6d0c8ae93 Mon Sep 17 00:00:00 2001 From: Dmitri Smirnov Date: Tue, 6 Oct 2015 16:19:58 -0700 Subject: [PATCH] Mmap reads should not return error if reading past file Summary: This mirrors https://reviews.facebook.net/D45645 Currently, mmap returns IOError when user tries to read data past the end of the file. This diff changes the behavior. Now, we return just the bytes that we can, and report the size we returned via a Slice result. This is consistent with non-mmap behavior and also pread() system call. --- port/win/env_win.cc | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/port/win/env_win.cc b/port/win/env_win.cc index 2668ad1d2..45847324b 100644 --- a/port/win/env_win.cc +++ b/port/win/env_win.cc @@ -233,13 +233,14 @@ class WinMmapReadableFile : public RandomAccessFile { char* scratch) const override { Status s; - if (offset + n > length_) { + if (offset > length_) { *result = Slice(); - s = IOError(fileName_, EINVAL); - } else { - *result = - Slice(reinterpret_cast(mapped_region_) + offset, n); + return IOError(fileName_, EINVAL); + } else if (offset + n > length_) { + n = length_ - offset; } + *result = + Slice(reinterpret_cast(mapped_region_) + offset, n); return s; } -- GitLab