Skip to content

Commit

Permalink
Correctly throw an exception if we fail to read in file.
Browse files Browse the repository at this point in the history
`FileReader` excepts that `readAt` throws an exception is something goes
wrong. So `readAt` must throw an exception instead of returning `-1`.
  • Loading branch information
mgautierfr committed May 30, 2024
1 parent fe6c43e commit ac69abc
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/fs_unix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,11 @@ zsize_t FD::readAt(char* dest, zsize_t size, offset_t offset) const
errno = 0;
while (size_to_read > 0) {
auto size_read = PREAD(m_fd, dest, size_to_read, current_offset);
if (size_read == 0) {
throw std::runtime_error("Cannot read past the end of the file");
}
if (size_read == -1) {
return zsize_t(-1);
throw std::runtime_error("Cannot read file");
}
size_to_read -= size_read;
current_offset += size_read;
Expand Down
4 changes: 2 additions & 2 deletions src/fs_windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ FD::~FD()
zsize_t FD::readAt(char* dest, zsize_t size, offset_t offset) const
{
if (!mp_impl)
return zsize_t(-1);
throw std::runtime_error("FD is not open");
EnterCriticalSection(&mp_impl->m_criticalSection);
LARGE_INTEGER off;
off.QuadPart = offset.v;
Expand All @@ -88,7 +88,7 @@ zsize_t FD::readAt(char* dest, zsize_t size, offset_t offset) const
return size;
err:
LeaveCriticalSection(&mp_impl->m_criticalSection);
return zsize_t(-1);
throw std::runtime_error("Cannot read");
}

bool FD::seek(offset_t offset)
Expand Down

0 comments on commit ac69abc

Please sign in to comment.