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; }