From 7348f7c64d8d3fd46c4f11da377f86bad7c6d91a Mon Sep 17 00:00:00 2001 From: Sebastian Messmer Date: Wed, 6 Jul 2016 15:28:14 -0700 Subject: [PATCH] Add checks to Data::LoadFromFile() and Data::StoreToFile() --- src/cpp-utils/data/Data.cpp | 6 +++++- src/cpp-utils/data/Data.h | 3 +++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/cpp-utils/data/Data.cpp b/src/cpp-utils/data/Data.cpp index cd37683c..ce649269 100644 --- a/src/cpp-utils/data/Data.cpp +++ b/src/cpp-utils/data/Data.cpp @@ -15,7 +15,11 @@ boost::optional Data::LoadFromFile(const bf::path &filepath) { if (!file.good()) { return boost::none; } - return LoadFromStream(file); + auto result = LoadFromStream(file); + if (!file.good()) { + throw std::runtime_error("Error reading from file"); + } + return result; } std::streampos Data::_getStreamSize(istream &stream) { diff --git a/src/cpp-utils/data/Data.h b/src/cpp-utils/data/Data.h index ff3916a1..3b97213f 100644 --- a/src/cpp-utils/data/Data.h +++ b/src/cpp-utils/data/Data.h @@ -124,6 +124,9 @@ inline void Data::StoreToFile(const boost::filesystem::path &filepath) const { throw std::runtime_error("Could not open file for writing"); } StoreToStream(file); + if (!file.good()) { + throw std::runtime_error("Error writing to file"); + } } inline void Data::StoreToStream(std::ostream &stream) const {