From 4ba4ea560280f54b476dd80f443dc6873ba96b86 Mon Sep 17 00:00:00 2001 From: Automatic Release Builder Date: Thu, 4 Feb 2021 10:06:23 +0000 Subject: [PATCH] Adding code to see if it improves crash on computeHash. Also add a test case to verify behaviour on empty files is correct. Sentry-Id: FLIGHTGEAR-AY2 --- simgear/io/sg_file.cxx | 4 +++- simgear/io/test_repository.cxx | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/simgear/io/sg_file.cxx b/simgear/io/sg_file.cxx index 9f223e34..1747e971 100644 --- a/simgear/io/sg_file.cxx +++ b/simgear/io/sg_file.cxx @@ -82,7 +82,9 @@ std::string SGFile::computeHash() [](char* p) { free(p); }}; if (!buf) { - SG_LOG(SG_IO, SG_ALERT, "Failed to malloc buffer for SHA1 check"); + // @TODO report out of memory error + SG_LOG(SG_IO, SG_ALERT, "Failed to malloc buffer for SHA1 check:" << file_name); + return {}; } size_t readLen; diff --git a/simgear/io/test_repository.cxx b/simgear/io/test_repository.cxx index 6e3f86d4..ad4e9568 100644 --- a/simgear/io/test_repository.cxx +++ b/simgear/io/test_repository.cxx @@ -901,6 +901,27 @@ void testPersistentSocketFailure(HTTP::Client *cl) { verifyRequestCount("dirD/subdirDB/fileDBA", 1); } +void testHashOnEmptyFile() +{ + std::unique_ptr repo; + SGPath p(simgear::Dir::current().path()); + p.append("sgfile_compute_hash"); + simgear::Dir pd(p); + if (pd.exists()) { + pd.removeChildren(); + } else { + pd.create(0700); + } + + SGPath fPath = p / "test_empty_file"; + + SGFile file(fPath); + file.open(SG_IO_OUT); + file.close(); + + const auto hash = file.computeHash(); +} + int main(int argc, char* argv[]) { sglog().setLogLevels( SG_ALL, SG_INFO ); @@ -951,6 +972,8 @@ int main(int argc, char* argv[]) testRetryAfterSocketFailure(&cl); testPersistentSocketFailure(&cl); + testHashOnEmptyFile(); + std::cout << "all tests passed ok" << std::endl; return 0; }