Skip to content

Commit

Permalink
Add return value check for fread
Browse files Browse the repository at this point in the history
  • Loading branch information
rascani committed Jul 15, 2023
1 parent b57dbfe commit b5378e3
Showing 1 changed file with 6 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ void SetRandomInput(const uint32_t random_seed,
bool ReadFile(const char* file_name, void* buffer, size_t buffer_size) {
std::unique_ptr<FILE, decltype(&fclose)> file(fopen(file_name, "rb"), fclose);

fread(buffer, sizeof(char), buffer_size, file.get());
const size_t bytes_read =
fread(buffer, sizeof(char), buffer_size, file.get());
if (ferror(file.get())) {
MicroPrintf("Unable to read model file: %d\n", ferror(file.get()));
return false;
Expand All @@ -88,6 +89,10 @@ bool ReadFile(const char* file_name, void* buffer, size_t buffer_size) {
MicroPrintf("Model buffer is too small for the model.\n");
return false;
}
if (bytes_read == 0) {
MicroPrintf("No bytes read from model file.\n");
return false;
}

return true;
}
Expand Down

0 comments on commit b5378e3

Please sign in to comment.