From 75fb313e5ab1dda75681f196d4ca5dc1d0c57043 Mon Sep 17 00:00:00 2001 From: icy17 <39425646+icy17@users.noreply.github.com> Date: Fri, 10 Mar 2023 05:07:33 +0800 Subject: [PATCH] Fix #19279: Memory leak in Zip.cpp upon error (#19283) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix memleak * Replace deprecated functions and throw on error --------- Co-authored-by: ζeh Matt <5415177+ZehMatt@users.noreply.github.com> --- src/openrct2/core/Zip.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/openrct2/core/Zip.cpp b/src/openrct2/core/Zip.cpp index 04d8a74106..9ecae3aa20 100644 --- a/src/openrct2/core/Zip.cpp +++ b/src/openrct2/core/Zip.cpp @@ -169,13 +169,19 @@ public: auto source = zip_source_buffer(_zip, writeBuffer.data(), writeBuffer.size(), 0); auto index = GetIndexFromPath(path); + zip_int64_t res = 0; if (index.has_value()) { - zip_replace(_zip, index.value(), source); + res = zip_file_replace(_zip, index.value(), source, 0); } else { - zip_add(_zip, path.data(), source); + res = zip_file_add(_zip, path.data(), source, 0); + } + if (res == -1) + { + zip_source_free(source); + throw std::runtime_error("Unable to set file contents."); } }