Check return values of fread, etc in ReadFile/WriteFile.

pull/380/head
whitequark 2019-01-10 07:12:12 +00:00
parent 8f2af5b228
commit 8e4c4b0d46
1 changed files with 12 additions and 6 deletions

View File

@ -419,11 +419,15 @@ bool ReadFile(const Platform::Path &filename, std::string *data) {
FILE *f = OpenFile(filename, "rb"); FILE *f = OpenFile(filename, "rb");
if(f == NULL) return false; if(f == NULL) return false;
fseek(f, 0, SEEK_END); if(fseek(f, 0, SEEK_END) != 0)
return false;
data->resize(ftell(f)); data->resize(ftell(f));
fseek(f, 0, SEEK_SET); if(fseek(f, 0, SEEK_SET) != 0)
fread(&(*data)[0], 1, data->size(), f); return false;
fclose(f); if(fread(&(*data)[0], 1, data->size(), f) != data->size())
return false;
if(fclose(f) != 0)
return false;
return true; return true;
} }
@ -432,8 +436,10 @@ bool WriteFile(const Platform::Path &filename, const std::string &data) {
FILE *f = OpenFile(filename, "wb"); FILE *f = OpenFile(filename, "wb");
if(f == NULL) return false; if(f == NULL) return false;
fwrite(&data[0], 1, data.size(), f); if(fwrite(&data[0], 1, data.size(), f) != data.size())
fclose(f); return false;
if(fclose(f) != 0)
return false;
return true; return true;
} }