Replace std::rewind to std::fseek 23/150323/1
authorsangwan.kwon <sangwan.kwon@samsung.com>
Wed, 13 Sep 2017 04:49:02 +0000 (00:49 -0400)
committersangwan kwon <sangwan.kwon@samsung.com>
Fri, 15 Sep 2017 06:04:35 +0000 (06:04 +0000)
std::rewind cannot handle error indicators.

Change-Id: I8c4006c5378a19c6e761708df619f1d77261d8bd
Signed-off-by: sangwan.kwon <sangwan.kwon@samsung.com>
src/certificate.cpp
src/file-system.cpp

index 103c864..13a8f70 100644 (file)
@@ -58,7 +58,10 @@ std::string Certificate::getSubjectNameHash() const
        X509Ptr x509(::PEM_read_X509(fp.get(), NULL, NULL, NULL),
                                 ::X509_free);
        if (x509 == nullptr) {
-               ::rewind(fp.get());
+               std::fseek(fp.get(), 0L, SEEK_SET);
+               if (std::ferror(fp.get()))
+                       throw std::logic_error("Failed to set the position as beginning.");
+
                x509 = X509Ptr(::PEM_read_X509_AUX(fp.get(), NULL, NULL, NULL),
                                           ::X509_free);
        }
index ec1ab6d..2171638 100644 (file)
@@ -45,12 +45,18 @@ std::string File::read(const std::string &path)
                throw std::invalid_argument("Failed to open [" + path + "].");
 
        std::fseek(fp.get(), 0L, SEEK_END);
+       if (std::ferror(fp.get()))
+               throw std::logic_error("Failed to set the position as end [" + path + "].");
+
        unsigned int fsize = std::ftell(fp.get());
-       std::rewind(fp.get());
+       std::fseek(fp.get(), 0L, SEEK_SET);
+       if (std::ferror(fp.get()))
+               throw std::logic_error("Failed to set the position as beginning [" + path + "].");
 
        std::string buff(fsize, 0);
        if (fsize != std::fread(static_cast<void*>(&buff[0]), 1, fsize, fp.get()))
                throw std::logic_error("Failed to read [" + path + "]");
+
        return buff;
 }