From 8e4c4b0d4661bbf59843953d08ab4b9ad82e5e32 Mon Sep 17 00:00:00 2001 From: whitequark Date: Thu, 10 Jan 2019 07:12:12 +0000 Subject: [PATCH] Check return values of fread, etc in ReadFile/WriteFile. --- src/platform/platform.cpp | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/platform/platform.cpp b/src/platform/platform.cpp index 57cdcb88..eb5779c4 100644 --- a/src/platform/platform.cpp +++ b/src/platform/platform.cpp @@ -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; } @@ -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; }