From 57a4dc53f2f87c5df2d41d4c39aa6d88b1cf9983 Mon Sep 17 00:00:00 2001 From: James Turner Date: Wed, 2 Dec 2020 21:28:37 +0000 Subject: [PATCH] =?UTF-8?q?HTTPRepository:=20don=E2=80=99t=20crash=20on=20?= =?UTF-8?q?empty=20files?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix some additional crash cases around 0-length files --- simgear/io/HTTPRepository.cxx | 53 +++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 24 deletions(-) diff --git a/simgear/io/HTTPRepository.cxx b/simgear/io/HTTPRepository.cxx index 5d57f1c7..c4ee4b4a 100644 --- a/simgear/io/HTTPRepository.cxx +++ b/simgear/io/HTTPRepository.cxx @@ -438,32 +438,37 @@ public: ArchiveExtractTask(const ArchiveExtractTask &) = delete; - HTTPRepoPrivate::ProcessResult run(HTTPRepoPrivate *repo) { - size_t rd = file.read((char *)buffer, bufferSize); - extractor.extractBytes(buffer, rd); - - if (file.eof()) { - extractor.flush(); - file.close(); - - if (!extractor.isAtEndOfArchive()) { - SG_LOG(SG_TERRASYNC, SG_ALERT, "Corrupt tarball " << relativePath); - repo->failedToUpdateChild(relativePath, - HTTPRepository::REPO_ERROR_IO); - return HTTPRepoPrivate::ProcessFailed; + HTTPRepoPrivate::ProcessResult run(HTTPRepoPrivate* repo) + { + if (!buffer) { + return HTTPRepoPrivate::ProcessFailed; } - if (extractor.hasError()) { - SG_LOG(SG_TERRASYNC, SG_ALERT, "Error extracting " << relativePath); - repo->failedToUpdateChild(relativePath, - HTTPRepository::REPO_ERROR_IO); - return HTTPRepoPrivate::ProcessFailed; + size_t rd = file.read((char*)buffer, bufferSize); + extractor.extractBytes(buffer, rd); + + if (file.eof()) { + extractor.flush(); + file.close(); + + if (!extractor.isAtEndOfArchive()) { + SG_LOG(SG_TERRASYNC, SG_ALERT, "Corrupt tarball " << relativePath); + repo->failedToUpdateChild(relativePath, + HTTPRepository::REPO_ERROR_IO); + return HTTPRepoPrivate::ProcessFailed; + } + + if (extractor.hasError()) { + SG_LOG(SG_TERRASYNC, SG_ALERT, "Error extracting " << relativePath); + repo->failedToUpdateChild(relativePath, + HTTPRepository::REPO_ERROR_IO); + return HTTPRepoPrivate::ProcessFailed; + } + + return HTTPRepoPrivate::ProcessDone; } - return HTTPRepoPrivate::ProcessDone; - } - - return HTTPRepoPrivate::ProcessContinue; + return HTTPRepoPrivate::ProcessContinue; } ~ArchiveExtractTask() { free(buffer); } @@ -974,6 +979,8 @@ HTTPRepository::failure() const { pathInRepo = _directory->absolutePath(); pathInRepo.append(fileName); + + sha1_init(&hashContext); } protected: @@ -986,8 +993,6 @@ HTTPRepository::failure() const _directory->repository()->http->cancelRequest( this, "Unable to create output file:" + pathInRepo.utf8Str()); } - - sha1_init(&hashContext); } sha1_write(&hashContext, s, n);