Skip to content

Commit

Permalink
util: check for error after reading from a file
Browse files Browse the repository at this point in the history
`fread(3)` does not distinguish between end-of-file and error,
and callers must use `feof(3)` and `ferror(3)` to determine which
occurred.
  • Loading branch information
vasild committed May 13, 2024
1 parent f9cf6b5 commit de23848
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/streams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#include <span.h>
#include <streams.h>
#include <tinyformat.h>
#include <util/syserror.h>

#include <array>

Expand All @@ -23,8 +25,9 @@ std::size_t AutoFile::detail_fread(Span<std::byte> dst)

void AutoFile::read(Span<std::byte> dst)
{
if (detail_fread(dst) != dst.size()) {
throw std::ios_base::failure(feof() ? "AutoFile::read: end of file" : "AutoFile::read: fread failed");
if (detail_fread(dst) != dst.size() || ferror(m_file)) {
throw std::ios_base::failure(feof() ? "AutoFile::read: end of file" :
strprintf("AutoFile::read: error: %s", SysErrorString(errno)));
}
}

Expand Down

0 comments on commit de23848

Please sign in to comment.