From 549806e4726c0bae448d189fcfcd2bd60bccc845 Mon Sep 17 00:00:00 2001 From: "sangwan.kwon" Date: Wed, 13 Sep 2017 00:49:02 -0400 Subject: [PATCH] Replace std::rewind to std::fseek std::rewind cannot handle error indicators. Change-Id: I8c4006c5378a19c6e761708df619f1d77261d8bd Signed-off-by: sangwan.kwon --- src/certificate.cpp | 5 ++++- src/file-system.cpp | 8 +++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/certificate.cpp b/src/certificate.cpp index 103c864..13a8f70 100644 --- a/src/certificate.cpp +++ b/src/certificate.cpp @@ -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); } diff --git a/src/file-system.cpp b/src/file-system.cpp index ec1ab6d..2171638 100644 --- a/src/file-system.cpp +++ b/src/file-system.cpp @@ -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(&buff[0]), 1, fsize, fp.get())) throw std::logic_error("Failed to read [" + path + "]"); + return buff; } -- 2.34.1