Fix the core dump by using filesystem error_code
The currently used filesystem method will cause an exception if the
file system is damaged for some reason, resulting in a core dump of
the process.
So the overloaded method with the error_code parameter should be used
here to ensure that the process core dump will not be caused after an
exception is thrown.
Fixes: openbmc/phosphor-bmc-code-mgmt#12
Tested: built phosphor-bmc-code-mgmt successfully and CI passes.
Signed-off-by: George Liu <liuxiwei@inspur.com>
Change-Id: I329f78b481cb466e755bc1b78562583620f561c2
diff --git a/serialize.cpp b/serialize.cpp
index 8b7fca4..ff9e832 100644
--- a/serialize.cpp
+++ b/serialize.cpp
@@ -8,6 +8,7 @@
#include <filesystem>
#include <fstream>
+#include <system_error>
namespace phosphor
{
@@ -24,16 +25,17 @@
void storePriority(const std::string& flashId, uint8_t priority)
{
+ std::error_code ec;
auto path = fs::path(PERSIST_DIR) / flashId;
- if (!fs::is_directory(path))
+ if (!fs::is_directory(path, ec))
{
- if (fs::exists(path))
+ if (fs::exists(path, ec))
{
// Delete if it's a non-directory file
warning("Removing non-directory file: {PATH}", "PATH", path);
- fs::remove_all(path);
+ fs::remove_all(path, ec);
}
- fs::create_directories(path);
+ fs::create_directories(path, ec);
}
path = path / priorityName;
@@ -44,16 +46,17 @@
void storePurpose(const std::string& flashId, VersionPurpose purpose)
{
+ std::error_code ec;
auto path = fs::path(PERSIST_DIR) / flashId;
- if (!fs::is_directory(path))
+ if (!fs::is_directory(path, ec))
{
- if (fs::exists(path))
+ if (fs::exists(path, ec))
{
// Delete if it's a non-directory file
warning("Removing non-directory file: {PATH}", "PATH", path);
- fs::remove_all(path);
+ fs::remove_all(path, ec);
}
- fs::create_directories(path);
+ fs::create_directories(path, ec);
}
path = path / purposeName;
@@ -64,8 +67,9 @@
bool restorePriority(const std::string& flashId, uint8_t& priority)
{
+ std::error_code ec;
auto path = fs::path(PERSIST_DIR) / flashId / priorityName;
- if (fs::exists(path))
+ if (fs::exists(path, ec))
{
std::ifstream is(path.c_str(), std::ios::in);
try
@@ -76,7 +80,7 @@
}
catch (const cereal::Exception& e)
{
- fs::remove_all(path);
+ fs::remove_all(path, ec);
}
}
@@ -124,8 +128,9 @@
bool restorePurpose(const std::string& flashId, VersionPurpose& purpose)
{
+ std::error_code ec;
auto path = fs::path(PERSIST_DIR) / flashId / purposeName;
- if (fs::exists(path))
+ if (fs::exists(path, ec))
{
std::ifstream is(path.c_str(), std::ios::in);
try
@@ -136,7 +141,7 @@
}
catch (const cereal::Exception& e)
{
- fs::remove_all(path);
+ fs::remove_all(path, ec);
}
}
@@ -145,10 +150,11 @@
void removePersistDataDirectory(const std::string& flashId)
{
+ std::error_code ec;
auto path = fs::path(PERSIST_DIR) / flashId;
- if (fs::exists(path))
+ if (fs::exists(path, ec))
{
- fs::remove_all(path);
+ fs::remove_all(path, ec);
}
}