Skip to content

Commit

Permalink
Throw MMapException if mmap fail.
Browse files Browse the repository at this point in the history
  • Loading branch information
mgautierfr committed Apr 3, 2024
1 parent e333968 commit 916afdf
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/file_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,13 @@ mmapReadOnly(int fd, offset_type offset, size_type size)
#endif

const auto p = (char*)mmap(NULL, size, PROT_READ, MAP_FLAGS, fd, offset);
if (p == MAP_FAILED)
throw std::runtime_error(Formatter()
<< "Cannot mmap size " << size << " at off "
<< offset << " : " << strerror(errno));

if (p == MAP_FAILED) {
// mmap may fails for a lot of reason.
// Most of them (io error, too big size...) may not recoverable but some of
// them may be relative to mmap only and a "simple" read from the file would work.
// Let's throw a MMapException to fallback to read (and potentially fail again there).
throw MMapException();

Check warning on line 154 in src/file_reader.cpp

View check run for this annotation

Codecov / codecov/patch

src/file_reader.cpp#L154

Added line #L154 was not covered by tests
}
return p;
}

Expand Down Expand Up @@ -189,7 +191,8 @@ const Buffer BaseFileReader::get_buffer(offset_t offset, zsize_t size) const {
// - Mmap offset is too big (>4GB on 32 bits)
// - The range is several part
// - We are on Windows.
// We will have to do some memory copies :/
// - Mmap itself has failed
// We will have to do some memory copies (or fail trying to) :/
// [TODO] Use Windows equivalent for mmap.
auto ret_buffer = Buffer::makeBuffer(size);
read(const_cast<char*>(ret_buffer.data()), offset, size);
Expand Down

0 comments on commit 916afdf

Please sign in to comment.