json_serializer: handled corrupted files
When the `/var/lib/usr_mgr.conf` file is either empty or corrupted
JSON, the daemon will crash and not recover. Handle this by catching
JSON load exceptions and deleting the corrupted file.
Fixes openbmc/phosphor-user-manager#19.
Tested: Added additional test cases to cover the corruption case and
update the test case to cover the non-throwing behavior.
Signed-off-by: Patrick Williams <patrick@stwcx.xyz>
Change-Id: I2be787771ea3d72af924615a6eee17cf2f393e9a
diff --git a/json_serializer.hpp b/json_serializer.hpp
index 81a6a4d..8882cc5 100644
--- a/json_serializer.hpp
+++ b/json_serializer.hpp
@@ -105,19 +105,35 @@
return false;
}
}
- void load()
+ bool load()
{
std::ifstream file(mfaConfPath.data());
if (file.is_open())
{
- file >> jsonData;
- file.close();
+ try
+ {
+ file >> jsonData;
+ file.close();
+ return true;
+ }
+ catch (const nlohmann::json::parse_error& e)
+ {
+ lg2::error("JSON parsing error: {MSG} in file {FILENAME}",
+ "MSG", e.what(), "FILENAME", mfaConfPath);
+ if (std::filesystem::exists(mfaConfPath))
+ {
+ std::filesystem::remove(mfaConfPath);
+ }
+ file.close();
+ return false;
+ }
}
else
{
lg2::error("Unable to open file for reading {FILENAME}", "FILENAME",
mfaConfPath);
+ return false;
}
}