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 segfault when reading from an empty source #210

Merged
merged 1 commit into from
Sep 16, 2023
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ using namespace libzippp;

int main(int argc, char** argv) {
//important to use calloc/malloc for the fromWritableBuffer !
char* buffer = (char*)calloc(4096, sizeof(char));
void* buffer = calloc(4096, sizeof(char));

ZipArchive* z1 = ZipArchive::fromWritableBuffer(&buffer, 4096, ZipArchive::New);
/* add content to the archive */
Expand Down
10 changes: 7 additions & 3 deletions src/libzippp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,12 @@ namespace Helper {
}

static void callErrorHandlingCallback(zip* zipHandle, const std::string& msg, ErrorHandlerCallback* callback) {
zip_error_t* error_code = zip_get_error(zipHandle);
callErrorHandlingCallbackFunc(error_code->str, error_code->zip_err, error_code->sys_err, callback);
if(zipHandle!=nullptr) {
zip_error_t* error_code = zip_get_error(zipHandle);
callErrorHandlingCallbackFunc(error_code->str, error_code->zip_err, error_code->sys_err, callback);
} else {
callErrorHandlingCallbackFunc("", -1, -1, callback);
}
}

static void callErrorHandlingCallback(zip_error_t* error, const std::string& msg, ErrorHandlerCallback* callback) {
Expand Down Expand Up @@ -433,7 +437,7 @@ int ZipArchive::close(void) {
*bufferData = sourceBuffer;
bufferLength = totalRead;
} else {
Helper::callErrorHandlingCallback(zipHandle, "can't read back from source: changes were not pushed in the buffer\n", errorHandlingCallback);
Helper::callErrorHandlingCallback((zip*)nullptr, "can't read back from source: changes were not pushed in the buffer\n", errorHandlingCallback);
return LIBZIPPP_ERROR_HANDLE_FAILURE;
}

Expand Down
30 changes: 29 additions & 1 deletion tests/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,34 @@ void test23() {
}*/
}

void test23_2() {
//important to use calloc/malloc for the fromWritableBuffer !
void* buffer = calloc(4096, sizeof(char));

ZipArchive* z1 = ZipArchive::fromWritableBuffer(&buffer, 4096, ZipArchive::New);
/* add content to the archive */

//will update the content of the buffer
z1->close();

//length of the buffer content
int bufferContentLength = z1->getBufferLength();

ZipArchive::free(z1);

//read again from the archive:
ZipArchive* z2 = ZipArchive::fromBuffer(buffer, bufferContentLength);
/* read the archive - no modification allowed */
ZipArchive::free(z2);

//read again from the archive, for modification:
ZipArchive* z3 = ZipArchive::fromWritableBuffer(&buffer, bufferContentLength);
/* read/write the archive */
ZipArchive::free(z3);

free(buffer);
}

static void myErrorHandler(const std::string& message,
const std::string& strerror,
int zip_error_code,
Expand All @@ -920,7 +948,7 @@ int main() {
test6(); test7(); test8(); test9(); test10();
test11(); test12(); test13(); test14(); test15();
test16(); test17(); test18(); test19(); test20();
test21(); test22(); test23(); test24();
test21(); test22(); test23(); test23_2(); test24();
return 0;
}

Expand Down