Skip to content

Commit

Permalink
Check return values of fread, etc in ReadFile/WriteFile.
Browse files Browse the repository at this point in the history
  • Loading branch information
whitequark committed Jan 10, 2019
1 parent 8f2af5b commit 8e4c4b0
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/platform/platform.cpp
Expand Up @@ -419,11 +419,15 @@ bool ReadFile(const Platform::Path &filename, std::string *data) {
FILE *f = OpenFile(filename, "rb");
if(f == NULL) return false;

fseek(f, 0, SEEK_END);
if(fseek(f, 0, SEEK_END) != 0)
return false;
data->resize(ftell(f));
fseek(f, 0, SEEK_SET);
fread(&(*data)[0], 1, data->size(), f);
fclose(f);
if(fseek(f, 0, SEEK_SET) != 0)
return false;
if(fread(&(*data)[0], 1, data->size(), f) != data->size())
return false;
if(fclose(f) != 0)
return false;

return true;
}
Expand All @@ -432,8 +436,10 @@ bool WriteFile(const Platform::Path &filename, const std::string &data) {
FILE *f = OpenFile(filename, "wb");
if(f == NULL) return false;

fwrite(&data[0], 1, data.size(), f);
fclose(f);
if(fwrite(&data[0], 1, data.size(), f) != data.size())
return false;
if(fclose(f) != 0)
return false;

return true;
}
Expand Down

0 comments on commit 8e4c4b0

Please sign in to comment.