Skip to content

Commit

Permalink
Merge pull request #132 from ctabin/fix-source-leak
Browse files Browse the repository at this point in the history
Fixes zip_source leak in case of failure
  • Loading branch information
ctabin authored May 22, 2022
2 parents bc9f4c5 + 424d498 commit efea55b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
12 changes: 6 additions & 6 deletions src/libzippp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down
5 changes: 3 additions & 2 deletions src/libzippp.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit efea55b

Please sign in to comment.