Fix #19279: Memory leak in Zip.cpp upon error (#19283)

* fix memleak

* Replace deprecated functions and throw on error

---------

Co-authored-by: ζeh Matt <5415177+ZehMatt@users.noreply.github.com>
This commit is contained in:
icy17 2023-03-10 05:07:33 +08:00 committed by GitHub
parent fdbc3d29bb
commit 75fb313e5a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 8 additions and 2 deletions

View File

@ -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.");
}
}