From 76a9f3efbc08d3a73bac0ad9fffe4e0ce32c1e73 Mon Sep 17 00:00:00 2001 From: "sangwan.kwon" Date: Fri, 23 Mar 2018 16:01:48 +0900 Subject: [PATCH] Fix coverity defects - Error handling issues - Control flow issues Change-Id: I1de11998b681834f179a86b93e8f8638a02d6d95 Signed-off-by: sangwan.kwon --- src/file-system.cpp | 8 ++++++-- tests/test-certificate.cpp | 4 ++++ tests/test-curl.cpp | 3 --- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/file-system.cpp b/src/file-system.cpp index 2171638..55783e0 100644 --- a/src/file-system.cpp +++ b/src/file-system.cpp @@ -48,13 +48,17 @@ std::string File::read(const std::string &path) if (std::ferror(fp.get())) throw std::logic_error("Failed to set the position as end [" + path + "]."); - unsigned int fsize = std::ftell(fp.get()); + long fsize = std::ftell(fp.get()); + if (fsize == -1L) + throw std::logic_error("Failed to get the position [" + path + "]."); + 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())) + size_t rsize = std::fread(static_cast(&buff[0]), 1, fsize, fp.get()); + if (rsize != static_cast(fsize)) throw std::logic_error("Failed to read [" + path + "]"); return buff; diff --git a/tests/test-certificate.cpp b/tests/test-certificate.cpp index 6313163..738960c 100644 --- a/tests/test-certificate.cpp +++ b/tests/test-certificate.cpp @@ -36,8 +36,10 @@ TESTCASE(GET_SUBJECT_NAME_HASH) TEST_EXPECT(true, hash.compare(TEST_PEM_HASH) == 0); } catch (const std::exception &e) { std::cout << "std::exception occured." << e.what() << std::endl; + TEST_EXPECT(true, false); } catch (...) { std::cout << "Unknown exception occured." << std::endl; + TEST_EXPECT(true, false); } } @@ -49,7 +51,9 @@ TESTCASE(GET_CERTIFICATE_DATA) TEST_EXPECT(true, data.compare(TEST_PEM_DATA) == 0); } catch (const std::exception &e) { std::cout << "std::exception occured." << e.what() << std::endl; + TEST_EXPECT(true, false); } catch (...) { std::cout << "Unknown exception occured." << std::endl; + TEST_EXPECT(true, false); } } diff --git a/tests/test-curl.cpp b/tests/test-curl.cpp index 338b9b5..09d183e 100644 --- a/tests/test-curl.cpp +++ b/tests/test-curl.cpp @@ -35,9 +35,6 @@ TESTCASE(CONNECT_SSL) { int ret = test::util::connectSSL("https://google.com"); TEST_EXPECT(true, ret == 0); - - if (ret != 0) - std::cout << "Check wifi connection.." << std::endl; } TESTCASE(TRUST_ANCHOR_LAUNCH) -- 2.34.1