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/image_verify.cpp b/image_verify.cpp
index 9551e4e..2b20340 100644
--- a/image_verify.cpp
+++ b/image_verify.cpp
@@ -18,6 +18,7 @@
 #include <cassert>
 #include <fstream>
 #include <set>
+#include <system_error>
 
 namespace phosphor
 {
@@ -57,9 +58,11 @@
     AvailableKeyTypes keyTypes{};
 
     // Find the path of all the files
-    if (!fs::is_directory(signedConfPath))
+    std::error_code ec;
+    if (!fs::is_directory(signedConfPath, ec))
     {
-        error("Signed configuration path not found in the system");
+        error("Signed configuration path not found in the system: {ERROR_MSG}",
+              "ERROR_MSG", ec.message());
         elog<InternalFailure>();
     }
 
@@ -129,7 +132,9 @@
     fs::path publicKeyFile(imageDirPath / PUBLICKEY_FILE_NAME);
 
     ret = verifyFile(pkeyFullFile, pkeyFullFileSig, publicKeyFile, hashType);
-    fs::remove(tmpFullFile);
+
+    std::error_code ec;
+    fs::remove(tmpFullFile, ec);
 #endif
 
     return ret;
@@ -186,7 +191,8 @@
             fs::path file(imageDirPath);
             file /= optionalImage;
 
-            if (fs::exists(file))
+            std::error_code ec;
+            if (fs::exists(file, ec))
             {
                 optionalFilesFound = true;
                 // Build Signature File name
@@ -297,10 +303,11 @@
 {
 
     // Check existence of the files in the system.
-    if (!(fs::exists(file) && fs::exists(sigFile)))
+    std::error_code ec;
+    if (!(fs::exists(file, ec) && fs::exists(sigFile, ec)))
     {
-        error("Failed to find the Data or signature file {PATH}.", "PATH",
-              file);
+        error("Failed to find the Data or signature file {PATH}: {ERROR_MSG}",
+              "PATH", file, "ERROR_MSG", ec.message());
         elog<InternalFailure>();
     }
 
@@ -338,7 +345,7 @@
     }
 
     // Hash the data file and update the verification context
-    auto size = fs::file_size(file);
+    auto size = fs::file_size(file, ec);
     auto dataPtr = mapFile(file, size);
 
     result = EVP_DigestVerifyUpdate(rsaVerifyCtx.get(), dataPtr(), size);
@@ -350,7 +357,7 @@
     }
 
     // Verify the data with signature.
-    size = fs::file_size(sigFile);
+    size = fs::file_size(sigFile, ec);
     auto signature = mapFile(sigFile, size);
 
     result = EVP_DigestVerifyFinal(
@@ -376,7 +383,8 @@
 
 inline EVP_PKEY_Ptr Signature::createPublicRSA(const fs::path& publicKey)
 {
-    auto size = fs::file_size(publicKey);
+    std::error_code ec;
+    auto size = fs::file_size(publicKey, ec);
 
     // Read public key file
     auto data = mapFile(publicKey, size);
@@ -414,7 +422,8 @@
         fs::path file(filePath);
         file /= bmcImage;
 
-        if (!fs::exists(file))
+        std::error_code ec;
+        if (!fs::exists(file, ec))
         {
             valid = false;
             break;