From 5e37c2092a40e8cd9db0a8555fa3134921893a29 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 37e37014..388996f6 100644 --- a/simgear/io/HTTPRepository.cxx +++ b/simgear/io/HTTPRepository.cxx @@ -985,16 +985,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); }