From 424d4981383e2077a1955c9ce6e4ab9a8ac50cc2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Tabin?= Date: Sun, 22 May 2022 11:16:30 +0200 Subject: [PATCH] Fixes zip_source leak in case of failure --- src/libzippp.cpp | 12 ++++++------ src/libzippp.h | 5 +++-- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/libzippp.cpp b/src/libzippp.cpp index 30bb5cc..c1b395e 100644 --- a/src/libzippp.cpp +++ b/src/libzippp.cpp @@ -142,20 +142,22 @@ bool ZipArchive::openBuffer(void* data, libzippp_uint32 size, OpenMode om, bool zip_error_init(&error); /* create source from buffer */ - zip_source* zipSource = zip_source_buffer_create(data, size, 0, &error); - if (zipSource == nullptr) { + zip_source* localZipSource = zip_source_buffer_create(data, size, 0, &error); + if (localZipSource == nullptr) { fprintf(stderr, "can't create zip source: %s\n", zip_error_strerror(&error)); zip_error_fini(&error); + zip_source_free(localZipSource); + localZipSource = nullptr; return false; } - bool open = openSource(zipSource, om, checkConsistency); + bool open = openSource(localZipSource, om, checkConsistency); if(open && (om==Write || om==New)) { bufferData = data; bufferLength = size; //prevents libzip to delete the source - zip_source_keep(zipSource); + zip_source_keep(localZipSource); } return open; } @@ -177,8 +179,6 @@ bool ZipArchive::openSource(zip_source* source, OpenMode om, bool checkConsisten zipHandle = zip_open_from_source(source, zipFlag, &error); if (zipHandle == nullptr) { fprintf(stderr, "can't open zip from source: %s\n", zip_error_strerror(&error)); - zip_source_free(zipSource); - zipSource = nullptr; zip_error_fini(&error); return false; } diff --git a/src/libzippp.h b/src/libzippp.h index f98daa5..12fd33c 100644 --- a/src/libzippp.h +++ b/src/libzippp.h @@ -176,7 +176,8 @@ namespace libzippp { /** * Creates a new ZipArchive with the given source. The archive will directly * be open with the given mode. If the archive fails to be open or - * if the consistency check fails, this method will return null. + * if the consistency check fails, this method will return null and the source + * is left untouched. */ static ZipArchive* fromSource(zip_source* source, OpenMode mode=ReadOnly, bool checkConsistency=false); @@ -529,7 +530,7 @@ namespace libzippp { public: /** - * This method is invoked during while the changes are being committed during + * This method is invoked while the changes are being committed during * the closing of the ZipArchive. * The value p is a double between 0 and 1, representing the overall progression. * The frequency of invocation of this method depends of the precision.