From 36417922b17a15595d4fae446c3bf53c16e28ced Mon Sep 17 00:00:00 2001 From: Michael Tritz Date: Fri, 4 Aug 2017 14:00:29 -0500 Subject: Fix for restoreFromFile cereal exception This commit provides a fix for an issue in which the cereal restoreFromFile() would crash the host software updater in the event that a priority persistence file is empty. Resolves openbmc/openbmc#2091 Change-Id: Icca5d68ef250e2662cd02075c6ba731e4ab768b0 Signed-off-by: Michael Tritz --- serialize.cpp | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'serialize.cpp') diff --git a/serialize.cpp b/serialize.cpp index 8e957272b..a499798c4 100644 --- a/serialize.cpp +++ b/serialize.cpp @@ -26,15 +26,24 @@ void storeToFile(std::string versionId, uint8_t priority) oarchive(cereal::make_nvp("priority", priority)); } -void restoreFromFile(std::string versionId, uint8_t *priority) +bool restoreFromFile(std::string versionId, uint8_t& priority) { std::string path = PERSIST_DIR + versionId; if (fs::exists(path)) { std::ifstream is(path.c_str(), std::ios::in); - cereal::JSONInputArchive iarchive(is); - iarchive(cereal::make_nvp("priority", *priority)); + try + { + cereal::JSONInputArchive iarchive(is); + iarchive(cereal::make_nvp("priority", priority)); + return true; + } + catch(cereal::RapidJSONException& e) + { + fs::remove(path); + } } + return false; } void removeFile(std::string versionId) -- cgit v1.2.1