SGFile::computeHash: check malloc result

Trying to fix a reported crash, in this code. malloc() returning null
seems unlikely but worth checking for. ALso use a unique_ptr with
a custom deleter to avoid a leak if we throw.

Sentry-Id: FLIGHTGEAR-3Y
This commit is contained in:
Automatic Release Builder 2020-10-06 11:33:17 +01:00
parent 1171d57b72
commit e39036a635

View File

@ -71,21 +71,31 @@ SGFile::~SGFile() {
std::string SGFile::computeHash() std::string SGFile::computeHash()
{ {
if (!file_name.exists()) if (!file_name.exists())
return std::string(); return {};
simgear::sha1nfo info; simgear::sha1nfo info;
sha1_init(&info); sha1_init(&info);
char* buf = static_cast<char*>(malloc(1024 * 1024));
// unique_ptr with custom deleter for exception safety
const int bufSize = 1024 * 1024;
std::unique_ptr<char, std::function<void(char*)>> buf{static_cast<char*>(malloc(bufSize)),
[](char* p) { free(p); }};
if (!buf) {
SG_LOG(SG_IO, SG_ALERT, "Failed to malloc buffer for SHA1 check");
}
size_t readLen; size_t readLen;
SGBinaryFile f(file_name); SGBinaryFile f(file_name);
if (!f.open(SG_IO_IN)) { if (!f.open(SG_IO_IN)) {
throw sg_io_exception("Couldn't open file for compute hash", file_name); SG_LOG(SG_IO, SG_ALERT, "SGFile::computeHash: Failed to open " << file_name);
return {};
} }
while ((readLen = f.read(buf, 1024 * 1024)) > 0) { while ((readLen = f.read(buf.get(), bufSize)) > 0) {
sha1_write(&info, buf, readLen); sha1_write(&info, buf.get(), readLen);
} }
f.close(); f.close();
free(buf);
std::string hashBytes((char*)sha1_result(&info), HASH_LENGTH); std::string hashBytes((char*)sha1_result(&info), HASH_LENGTH);
return simgear::strutils::encodeHex(hashBytes); return simgear::strutils::encodeHex(hashBytes);
} }