From 99c159d46e659d2897f3123b64fd760e637482a6 Mon Sep 17 00:00:00 2001 From: Automatic Release Builder Date: Wed, 23 Sep 2020 22:08:55 +0100 Subject: [PATCH] Harden Repo::computeHashForPath Check for some error cases in computeHashForPath, since this showed up in some crash reports. --- simgear/io/HTTPRepository.cxx | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/simgear/io/HTTPRepository.cxx b/simgear/io/HTTPRepository.cxx index 0e98771f..1523f59c 100644 --- a/simgear/io/HTTPRepository.cxx +++ b/simgear/io/HTTPRepository.cxx @@ -984,16 +984,24 @@ HTTPRepository::failure() const std::string HTTPRepoPrivate::computeHashForPath(const SGPath& p) { if (!p.exists()) - return std::string(); + return {}; + sha1nfo info; sha1_init(&info); - char* buf = static_cast(malloc(1024 * 1024)); + + const int bufSize = 1024 * 1024; + char* buf = static_cast(malloc(bufSize)); + if (!buf) { + sg_io_exception("Couldn't allocate SHA1 computation buffer"); + } + size_t readLen; SGBinaryFile f(p); if (!f.open(SG_IO_IN)) { + free(buf); throw sg_io_exception("Couldn't open file for compute hash", p); } - while ((readLen = f.read(buf, 1024 * 1024)) > 0) { + while ((readLen = f.read(buf, bufSize)) > 0) { sha1_write(&info, buf, readLen); }