Fix issue 2380: bug in (unused) non-mmap code.

This commit is contained in:
Kevin Backhouse 2022-10-22 22:19:28 +01:00 committed by Rosen Penev
parent abd817d181
commit 5cabd97373

View File

@ -276,14 +276,19 @@ byte* FileIo::mmap(bool isWriteable) {
p_->pMappedArea_ = static_cast<byte*>(rc); p_->pMappedArea_ = static_cast<byte*>(rc);
#else #else
// Workaround for platforms without mmap: Read the file into memory // Workaround for platforms without mmap: Read the file into memory
DataBuf buf(p_->mappedLength_); byte* buf = new byte[p_->mappedLength_];
if (read(buf.data(), buf.size()) != buf.size()) { const long offset = std::ftell(p_->fp_);
std::fseek(p_->fp_, 0, SEEK_SET);
if (read(buf, p_->mappedLength_) != p_->mappedLength_) {
delete[] buf;
throw Error(ErrorCode::kerCallFailed, path(), strError(), "FileIo::read"); throw Error(ErrorCode::kerCallFailed, path(), strError(), "FileIo::read");
} }
std::fseek(p_->fp_, offset, SEEK_SET);
if (error()) { if (error()) {
delete[] buf;
throw Error(ErrorCode::kerCallFailed, path(), strError(), "FileIo::mmap"); throw Error(ErrorCode::kerCallFailed, path(), strError(), "FileIo::mmap");
} }
p_->pMappedArea_ = buf.data(); p_->pMappedArea_ = buf;
p_->isMalloced_ = true; p_->isMalloced_ = true;
#endif #endif
return p_->pMappedArea_; return p_->pMappedArea_;