Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes zip_source leak in case of failure #132

Merged
merged 1 commit into from
May 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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