Git Product home page Git Product logo

libzip's Introduction

This is libzip, a C library for reading, creating, and modifying zip and zip64 archives. Files can be added from data buffers, files, or compressed data copied directly from other zip archives. Changes made without closing the archive can be reverted. Decryption and encryption of Winzip AES and legacy PKware encrypted files is supported.

libzip is fully documented via man pages. HTML versions of the man pages are on libzip.org and in the man directory. You can start with libzip(3), which lists all others. Example source code is in the examples and src subdirectories.

See the INSTALL.md file for installation instructions and dependencies.

If you have developed an application using libzip, you can find out about API changes and how to adapt your code for them in the included file API-CHANGES.md.

If you make a binary distribution, please include a pointer to the distribution site:

https://libzip.org/

The latest version can always be found there. The official repository is at github.

If you want to reach the authors in private, use [email protected].

Github Actions Build Status Appveyor Build status Coverity Status Fuzzing Status

libzip's People

Contributors

0-wiz-0 avatar andrewmolyneux avatar boaz001 avatar dec1 avatar dillof avatar fcharlie avatar ftzpetruska avatar gabibguti avatar goutnet avatar janisozaur avatar krnowak avatar malytomas avatar micmac1 avatar misterda avatar mmha avatar neheb avatar nmlsg avatar ogretransporter avatar peiweihu avatar praiskup avatar r-burns avatar randy408 avatar remicollet avatar richardschuetz avatar rikardfalkeborn avatar scribam avatar shahms avatar spaceim avatar tellowkrinkle avatar vishwin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

libzip's Issues

This is important one!

Is it important to download and configure both OpenSSL and GnuTLS
or I can just go with OpenSSL that I have right now.

please tell me its just one step ahead.

The ZIP_TRUNCATE flag leads to memory leak

The simple test:

const char* path = "some/path";
int errorp = 0;
zip_t *zip = zip_open(path, ZIP_CREATE | ZIP_TRUNCATE, &errorp);
zip_close(zip);

leads to memory leak:
==12432== HEAP SUMMARY:
==12432== in use at exit: 73,139 bytes in 6 blocks
==12432== total heap usage: 826 allocs, 820 frees, 3,828,722 bytes allocated
==12432==
==12432== 435 (128 direct, 307 indirect) bytes in 1 blocks are definitely lost in loss record 5 of 6
==12432== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==12432== by 0x43A871: _zip_new (zip_new.c:49)
==12432== by 0x42E5EB: _zip_allocate_new (zip_open.c:526)
==12432== by 0x42D632: zip_open_from_source (zip_open.c:130)
==12432== by 0x42D41B: zip_open (zip_open.c:74)
==12432== by 0x41CEC7: Zip_MemoryLeak::test_method() (zip_tests.cpp:122)
==12432== by 0x41CE99: Zip_MemoryLeak_invoker() (zip_tests.cpp:118)

if the function zip_open are using without flag ZIP_TRUNCATE, there no memory leak ...

Renaming temporary file failed: File exists

I compile with mingw32 for windows, got ziptool.exe and libzip-4.dll.
When using command:
ziptool.exe abc.zip add_dir abc

to add abc folder to abc.zip, i got this error.

SFX is gone after modification.

If I want to update ZIP archives with SFX (created by zip XX.zip DIR; cat XX.zip >> sfx.sh; zip -A sfx.sh), after zip_open, zip_file_add, zip_close, the output becomes a pure ZIP archive without SFX. Other zip (like infozip) will keep SFX unchanged.

Is there any way so we can keep SFX unchanged? For example adding an flag to indicate we have to copy anything before first entry of CDIR?

heap-buffer-overflow bug in buffer_new

Hello.

I found a heap-buffer-overflow bug in libzip.

Please confirm.

Thanks.

Summary: heap-buffer-overflow
OS: CentOS 7 64bit
Version: commit 17485a2
PoC Download: Ov_buffer_new.zip
Steps to reproduce:
1.Download the .POC files.
2.Compile the source code with ASan.
3.Execute the following command
: ./ziptool $PoC cat index

=================================================================
==31998==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x602000000138 at pc 0x7f43e49cb855 bp 0x7ffc49d62410 sp 0x7ffc49d62408
WRITE of size 8 at 0x602000000138 thread T0
    #0 0x7f43e49cb854 in buffer_new /home/karas/libzip/lib/zip_source_buffer.c:465:46
    #1 0x7f43e49ca2d6 in zip_source_buffer_fragment_create /home/karas/libzip/lib/zip_source_buffer.c:133:19
    #2 0x7f43e49c9f35 in zip_source_buffer_create /home/karas/libzip/lib/zip_source_buffer.c:106:12
    #3 0x7f43e49c9f35 in zip_source_buffer /home/karas/libzip/lib/zip_source_buffer.c:89
    #4 0x7f43e49deddf in _zip_source_zip_new /home/karas/libzip/lib/zip_source_zip_new.c:101:9
    #5 0x7f43e49ba7a2 in zip_fopen_index_encrypted /home/karas/libzip/lib/zip_fopen_index_encrypted.c:50:14
    #6 0x515f3d in cat /home/karas/libzip/src/ziptool.c:181:13
    #7 0x51a5bf in dispatch /home/karas/libzip/src/ziptool.c:728:10
    #8 0x51a5bf in main /home/karas/libzip/src/ziptool.c:889
    #9 0x7f43e3679c04 in __libc_start_main (/lib64/libc.so.6+0x21c04)
    #10 0x41c10b in _start (/home/karas/libzip/build/src/ziptool+0x41c10b)

0x602000000139 is located 0 bytes to the right of 9-byte region [0x602000000130,0x602000000139)
allocated by thread T0 here:
    #0 0x4dfe6d in realloc /home/karas/src/llvm/projects/compiler-rt/lib/asan/asan_malloc_linux.cc:79
    #1 0x7f43e49cad94 in buffer_grow_fragments /home/karas/libzip/lib/zip_source_buffer.c:397:23
    #2 0x7f43e49cad94 in buffer_new /home/karas/libzip/lib/zip_source_buffer.c:441

SUMMARY: AddressSanitizer: heap-buffer-overflow /home/karas/libzip/lib/zip_source_buffer.c:465:46 in buffer_new
Shadow bytes around the buggy address:
  0x0c047fff7fd0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c047fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c047fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c047fff8000: fa fa 00 07 fa fa fd fd fa fa 00 01 fa fa fd fd
  0x0c047fff8010: fa fa 05 fa fa fa fd fa fa fa 00 03 fa fa 00 00
=>0x0c047fff8020: fa fa 00 00 fa fa 00[01]fa fa fa fa fa fa fa fa
  0x0c047fff8030: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c047fff8040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c047fff8050: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c047fff8060: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c047fff8070: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
Shadow byte legend (one shadow byte represents 8 application bytes):
  Addressable:           00
  Partially addressable: 01 02 03 04 05 06 07
  Heap left redzone:       fa
  Freed heap region:       fd
  Stack left redzone:      f1
  Stack mid redzone:       f2
  Stack right redzone:     f3
  Stack after return:      f5
  Stack use after scope:   f8
  Global redzone:          f9
  Global init order:       f6
  Poisoned by user:        f7
  Container overflow:      fc
  Array cookie:            ac
  Intra object redzone:    bb
  ASan internal:           fe
  Left alloca redzone:     ca
  Right alloca redzone:    cb
==31998==ABORTING

[Acknowledgement]
This work was supported by ICT R&D program of MSIP/IITP. [R7518-16-1001, Innovation hub for high Performance Computing]

ssize_t checks in config, end up with conflicting redefinition on linux

In file included from libzip-0.11.1/lib/zipint.h:54:0,
                 from libzip/libzip-0.11.1/lib/zip_add.c:37:
libzip-0.11.1/lib/config.h:51:13: error: conflicting types for ‘ssize_t’
 typedef int ssize_t;
             ^~~~~~~
In file included from /usr/include/zconf.h:421:0,
                 from /usr/include/zlib.h:34,
                 from libzip-0.11.1/lib/zipint.h:40,
                 from libzip-0.11.1/lib/zip_add.c:37:
/usr/include/x86_64-linux-gnu/sys/types.h:109:19: note: previous declaration of ‘ssize_t’ was here
 typedef __ssize_t ssize_t;

PKWARE encryption encode support

For backward compatibility reasons i need to create zip files with PKWARE style encryption.
Tried AES-128 but my counterpart does not support this.

What should I do now?

Could NOT find NETTLE (missing: NETTLE_LIBRARY NETTLE_INCLUDE_DIR)
Could NOT find GnuTLS (missing: GNUTLS_LIBRARY GNUTLS_INCLUDE_DIR)
Could NOT find OpenSSL, try to set the path to OpenSSL root folder in the system variable OPENSSL_ROOT_DIR (missing: OPENSSL_INCLUDE_DIR)
Could NOT find BZip2 (missing: BZIP2_LIBRARIES BZIP2_INCLUDE_DIR)
CMake Warning at CMakeLists.txt:188 (MESSAGE):
-- bzip2 library not found; bzip2 support disabled

CMake Warning at CMakeLists.txt:207 (MESSAGE):
-- neither OpenSSL nor GnuTLS found; AES support disabled

Configuring done
Generating done

CMake - proper exports or find module missing, Windows builds not self-contained

When we first tried to use libzip with MSVC++ 14.1 (Visual Studio 2017), we stumbled upon the fact that without modifying the build system, a proper build is not possible. The "Packages" page refers to Microsofts huge library porting repository which has some patches and MS-specific CMake stuff to properly automate MSVC builds. In addition, proper generation of config-based libraries with appropriate library suffixes currently isn't implemented (i.e. debug and release build artifacts replace each other).

During install, no import targets are generated, so integration into a CMake-based environment either has to be done manually, by doing all the legwork directly in your projects or by writing proper modules for use with find_package(). Unless I overlooked it, it seems libzip doesn't provide a find module.

The dependency on zlib isn't an issue with CMake, as the proper find module is distributed with CMake.

Now, we could do all of this ourselves and leave it that, but I'd much rather simply use the upstream.

I suggest the following set of fixes:

  • make the library properly build out of the box on Windows (no patching or anything else needed)
  • fix library naming to at least have different outputs for debug and release builds
  • either provide the proper mechanism to export target definitions or add a proper find module to ease integration in other projects (I'd likely just do both)

Would you be interested in a corresponding PR?

Inconsistent error handling in _zip_open

Specifically, if there is an error with _zip_allocate_new in the zero length file case, the source is freed. In all other cases, care is taken to ensure that source remains valid.

Include appveyor.yml in repository

It looks like libzip has appveyor enabled, however appveyor.yaml file is nowhere to be found. I assume you have AppVeyor configured via WebUI, which prevents external contributors from setting their own instances to replicate your setup and prevents from improving this setup.

Please include appveyor.yaml as part of the repository.

What is this error?

CMake Error: Target DependInfo.cmake file not found
CMake Error: Directory Information file not found
Scanning dependencies of target zip
Building C object lib/CMakeFiles/zip.dir/zip_add.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_add_dir.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_add_entry.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_algorithm_deflate.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_buffer.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_close.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_delete.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_dir_add.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_dirent.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_discard.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_entry.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_err_str.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_error.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_error_clear.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_error_get.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_error_get_sys_type.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_error_strerror.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_error_to_str.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_extra_field.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_extra_field_api.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_fclose.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_fdopen.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_file_add.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_file_error_clear.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_file_error_get.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_file_get_comment.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_file_get_external_attributes.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_file_get_offset.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_file_rename.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_file_replace.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_file_set_comment.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_file_set_encryption.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_file_set_external_attributes.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_file_set_mtime.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_file_strerror.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_filerange_crc.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_fopen.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_fopen_encrypted.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_fopen_index.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_fopen_index_encrypted.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_fread.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_fseek.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_ftell.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_get_archive_comment.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_get_archive_flag.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_get_encryption_implementation.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_get_file_comment.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_get_name.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_get_num_entries.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_get_num_files.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_hash.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_io_util.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_libzip_version.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_memdup.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_name_locate.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_new.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_open.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_progress.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_rename.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_replace.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_set_archive_comment.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_set_archive_flag.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_set_default_password.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_set_file_comment.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_set_file_compression.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_set_name.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_source_begin_write.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_source_begin_write_cloning.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_source_buffer.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_source_call.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_source_close.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_source_commit_write.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_source_compress.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_source_crc.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_source_error.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_source_filep.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_source_free.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_source_function.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_source_get_compression_flags.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_source_is_deleted.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_source_layered.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_source_open.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_source_pkware.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_source_read.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_source_remove.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_source_rollback_write.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_source_seek.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_source_seek_write.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_source_stat.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_source_supports.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_source_tell.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_source_tell_write.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_source_window.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_source_write.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_source_zip.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_source_zip_new.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_stat.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_stat_index.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_stat_init.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_strerror.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_string.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_unchange.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_unchange_all.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_unchange_archive.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_unchange_data.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_utf-8.c.obj
Building C object lib/CMakeFiles/zip.dir/mkstemp.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_algorithm_bzip2.c.obj
Building C object lib/CMakeFiles/zip.dir/zip_crypto_openssl.c.obj
C:\Users\Dell\Desktop\libzip-master\lib\zip_crypto_openssl.c: In function '_zip_crypto_hmac_new':
C:\Users\Dell\Desktop\libzip-master\lib\zip_crypto_openssl.c:97:9: error: void value not ignored as it ought to be
if (HMAC_Init_ex(hmac, secret, (int)secret_length, EVP_sha1(), NULL) != 1) {
^
C:\Users\Dell\Desktop\libzip-master\lib\zip_crypto_openssl.c: In function '_zip_crypto_hmac_output':
C:\Users\Dell\Desktop\libzip-master\lib\zip_crypto_openssl.c:133:12: error: void value not ignored as it ought to be
return HMAC_Final(hmac, data, &length) == 1;
^
make[2]: *** [lib/CMakeFiles/zip.dir/zip_crypto_openssl.c.obj] Error 1
make[1]: *** [lib/CMakeFiles/zip.dir/all] Error 2
make: *** [all] Error 2

cmake policy warning (CMP0063)

When I build libzip as static library, cmake complains:

CMake Warning (dev) at .../zip/lib/CMakeLists.txt:215 (ADD_LIBRARY):                                                                                     
  Policy CMP0063 is not set: Honor visibility properties for all target
  types.  Run "cmake --help-policy CMP0063" for policy details.  Use the
  cmake_policy command to set the policy and suppress this warning.

  Target "zip" of type "STATIC_LIBRARY" has the following visibility
  properties set for C:

    C_VISIBILITY_PRESET

  For compatibility CMake is not honoring them for this target.
This warning is for project developers.  Use -Wno-dev to suppress it.

cmake version 3.10.0

cmake: building with cmake does not generate linux man pages

Building from git on linux:

CMake Error at man/cmake_install.cmake:36 (file):
  file INSTALL cannot find "/home/asavah/kross/src/libzip/man/zipcmp.man".
Call Stack (most recent call first):
  cmake_install.cmake:46 (include)

mandoc is present,
building with autotools is fine, man pages are generated and installed.

zip_fdopen leaves dangling file descriptor in case of error

I'm running code that uses zip_fdopen under a long-running process and have noticed that file descriptors are leaking if an invalid file is passed to zip_fdopen (like a jpg or dmg).

Here's a really crude repro case, with my executable named zipchecker when built:

#include <zip.h>

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>

int main() {
    /* Any non-valid zipfile works here */
    char *fname = "/home/wkl/image.jpg";
    int fd = open(fname, O_RDONLY);
    int *zep = NULL;
    zip_t z = zip_fdopen(fd, ZIP_CHECKCONS, zep);

    if (z) {
        printf("Valid zipfile\n");
        zip_close(z);
    } else {
        printf("Invalid zipfile\n");
        close(fd);
    }

    printf("Waiting on exit...\n");
    while(1) {}
    return 0;
}

After I start this and while it's still running, lsof returns this:

> sudo lsof -c zipchecker | grep -i jpg

zipchecke 618540  wkl    4r   REG   8,17   560511 283748705 /home/wkl/image.jpg

Haven't tested yet, but from a quick glance, I think the problem is from this section of zip_fdopen. dup is called on the passed-in file descriptor but the duplicated fd is not closed if either zip_source_filep_create or zip_open_from_source fail.

    if ((fd = dup(fd_orig)) < 0) {
	_zip_set_open_error(zep, NULL, ZIP_ER_OPEN);
	return NULL;
    }

    if ((fp = fdopen(fd, "rb")) == NULL) {
	close(fd);
	_zip_set_open_error(zep, NULL, ZIP_ER_OPEN);
	return NULL;
    }

    zip_error_init(&error);
    if ((src = zip_source_filep_create(fp, 0, -1, &error)) == NULL) {
        /* fd isn't closed here */
	_zip_set_open_error(zep, &error, 0);
	zip_error_fini(&error);
	return NULL;
    }

    if ((za = zip_open_from_source(src, _flags, &error)) == NULL) {
        /* fd isn't closed here either */
	_zip_set_open_error(zep, &error, 0);
	zip_error_fini(&error);
	return NULL;
    }

I could test and patch this if necessary.

Please provide a maintainance WORKING 1.3.3 release

I understand the desired to only have to maintain one build system for libzip but unfortunately this means that the last two releases in the 1.3 series (1.3.1 & 1.3.2) do not compile on mingw(64).

As a maintainer of a Qt project that provides support for building with the native qmake and a CMake alternative it is a non-starter to require the latter on Windows where the user, building from sources, wants to use the former. Given that the patch in https://libzip.org/libzip-discuss/msg00790.html does fix the issue would it be possible to backport it to finish the 1.3 series with an official released version that will compile from sources for those cases where 1.4, and the CMake dependency, is not an option?

feature request: option to not build the tools

Hi,

would you accept a pull request to have the option not to build the tools, but only the library?

I need this to integrate properly libzip in an ndk project. The problem being that new versions of the ndk do not provide stdout/stderr (needed by zipmerge and other tools).

Regards,

memory allocation failure in _zip_cdir_grow

On 1.2.0:

# ziptool $FILE cat index
==16798==ERROR: AddressSanitizer failed to allocate 0xc0a96a2000 (827476025344) bytes of LargeMmapAllocator (error code: 12)                                                                                      
==16798==Process memory map follows:                                                                                                                                                                              
        0x000000400000-0x00000054b000   /usr/bin/ziptool                                                                                                                                                          
        0x00000074b000-0x00000074c000   /usr/bin/ziptool                                                                                                                                                          
        0x00000074c000-0x000000753000   /usr/bin/ziptool                                                                                                                                                          
        0x000000753000-0x0000013c8000                                                                                                                                                                             
        0x00007fff7000-0x00008fff7000                                                                                                                                                                             
        0x00008fff7000-0x02008fff7000                                                                                                                                                                             
        0x02008fff7000-0x10007fff8000                                                                                                                                                                             
        0x600000000000-0x602000000000                                                                                                                                                                             
        0x602000000000-0x602000010000                                                                                                                                                                             
        0x602000010000-0x602e00000000                                                                                                                                                                             
        0x602e00000000-0x602e00010000                                                                                                                                                                             
        0x602e00010000-0x603000000000                                                                                                                                                                             
        0x603000000000-0x603000010000                                                                                                                                                                             
        0x603000010000-0x603e00000000                                                                                                                                                                             
        0x603e00000000-0x603e00010000                                                                                                                                                                             
        0x603e00010000-0x606000000000                                                                                                                                                                             
        0x606000000000-0x606000010000                                                                                                                                                                             
        0x606000010000-0x606e00000000                                                                                                                                                                             
        0x606e00000000-0x606e00010000                                                                                                                                                                             
        0x606e00010000-0x607000000000                                                                                                                                                                             
        0x607000000000-0x607000010000                                                                                                                                                                             
        0x607000010000-0x607e00000000                                                                                                                                                                             
        0x607e00000000-0x607e00010000
        0x607e00010000-0x60c000000000
        0x60c000000000-0x60c000010000
        0x60c000010000-0x60ce00000000
        0x60ce00000000-0x60ce00010000
        0x60ce00010000-0x60e000000000
        0x60e000000000-0x60e000010000
        0x60e000010000-0x60ee00000000
        0x60ee00000000-0x60ee00010000
        0x60ee00010000-0x616000000000
        0x616000000000-0x616000010000
        0x616000010000-0x616e00000000
        0x616e00000000-0x616e00010000
        0x616e00010000-0x61d000000000
        0x61d000000000-0x61d000010000
        0x61d000010000-0x61de00000000
        0x61de00000000-0x61de00010000
        0x61de00010000-0x621000000000
        0x621000000000-0x621000010000
        0x621000010000-0x621e00000000
        0x621e00000000-0x621e00010000
        0x621e00010000-0x631000000000
        0x631000000000-0x631000020000
        0x631000020000-0x631e00000000
        0x631e00000000-0x631e00010000
        0x631e00010000-0x640000000000
        0x640000000000-0x640000003000
        0x7f4ed67f7000-0x7f4ed7400000
        0x7f4ed7500000-0x7f4ed7600000
        0x7f4ed76ba000-0x7f4ed9a0c000
        0x7f4ed9a0c000-0x7f4ed9a22000   /lib64/libz.so.1.2.11
        0x7f4ed9a22000-0x7f4ed9c21000   /lib64/libz.so.1.2.11
        0x7f4ed9c21000-0x7f4ed9c22000   /lib64/libz.so.1.2.11
        0x7f4ed9c22000-0x7f4ed9c23000   /lib64/libz.so.1.2.11
        0x7f4ed9c23000-0x7f4ed9db2000   /lib64/libc-2.23.so
        0x7f4ed9db2000-0x7f4ed9fb2000   /lib64/libc-2.23.so
        0x7f4ed9fb2000-0x7f4ed9fb6000   /lib64/libc-2.23.so
        0x7f4ed9fb6000-0x7f4ed9fb8000   /lib64/libc-2.23.so
        0x7f4ed9fb8000-0x7f4ed9fbc000
        0x7f4ed9fbc000-0x7f4ed9fd2000   /usr/lib64/gcc/x86_64-pc-linux-gnu/6.4.0/libgcc_s.so.1
        0x7f4ed9fd2000-0x7f4eda1d1000   /usr/lib64/gcc/x86_64-pc-linux-gnu/6.4.0/libgcc_s.so.1
        0x7f4eda1d1000-0x7f4eda1d2000   /usr/lib64/gcc/x86_64-pc-linux-gnu/6.4.0/libgcc_s.so.1
        0x7f4eda1d2000-0x7f4eda1d3000   /usr/lib64/gcc/x86_64-pc-linux-gnu/6.4.0/libgcc_s.so.1
        0x7f4eda1d3000-0x7f4eda1d5000   /lib64/libdl-2.23.so
        0x7f4eda1d5000-0x7f4eda3d5000   /lib64/libdl-2.23.so
        0x7f4eda3d5000-0x7f4eda3d6000   /lib64/libdl-2.23.so
        0x7f4eda3d6000-0x7f4eda3d7000   /lib64/libdl-2.23.so
        0x7f4eda3d7000-0x7f4eda4d9000   /lib64/libm-2.23.so
        0x7f4eda4d9000-0x7f4eda6d8000   /lib64/libm-2.23.so
        0x7f4eda6d8000-0x7f4eda6d9000   /lib64/libm-2.23.so
        0x7f4eda6d9000-0x7f4eda6da000   /lib64/libm-2.23.so
        0x7f4eda6da000-0x7f4eda6e0000   /lib64/librt-2.23.so
        0x7f4eda6e0000-0x7f4eda8e0000   /lib64/librt-2.23.so
        0x7f4eda8e0000-0x7f4eda8e1000   /lib64/librt-2.23.so
        0x7f4eda8e1000-0x7f4eda8e2000   /lib64/librt-2.23.so
        0x7f4eda8e2000-0x7f4eda8f9000   /lib64/libpthread-2.23.so
        0x7f4eda8f9000-0x7f4edaaf8000   /lib64/libpthread-2.23.so
        0x7f4edaaf8000-0x7f4edaaf9000   /lib64/libpthread-2.23.so
        0x7f4edaaf9000-0x7f4edaafa000   /lib64/libpthread-2.23.so
        0x7f4edaafa000-0x7f4edaafe000
        0x7f4edaafe000-0x7f4edab5c000   /usr/lib64/libzip.so.5.0.0
        0x7f4edab5c000-0x7f4edad5c000   /usr/lib64/libzip.so.5.0.0
        0x7f4edad5c000-0x7f4edad5d000   /usr/lib64/libzip.so.5.0.0
        0x7f4edad5d000-0x7f4edad5f000   /usr/lib64/libzip.so.5.0.0
        0x7f4edad5f000-0x7f4edad83000   /lib64/ld-2.23.so
        0x7f4edadfc000-0x7f4edaf74000
        0x7f4edaf74000-0x7f4edaf82000
        0x7f4edaf82000-0x7f4edaf83000   /lib64/ld-2.23.so
        0x7f4edaf83000-0x7f4edaf84000   /lib64/ld-2.23.so
        0x7f4edaf84000-0x7f4edaf85000
        0x7ffe1f2be000-0x7ffe1f2df000   [stack]
        0x7ffe1f2e7000-0x7ffe1f2e9000   [vvar]
        0x7ffe1f2e9000-0x7ffe1f2eb000   [vdso]
        0xffffffffff600000-0xffffffffff601000   [vsyscall]
==16798==End of process memory map.
==16798==AddressSanitizer CHECK failed: /var/tmp/portage/sys-libs/compiler-rt-sanitizers-4.0.1/work/compiler-rt-4.0.1.src/lib/sanitizer_common/sanitizer_common.cc:120 "((0 && "unable to mmap")) != (0)" (0x0, 0x0)
    #0 0x4da9ef in AsanCheckFailed /var/tmp/portage/sys-libs/compiler-rt-sanitizers-4.0.1/work/compiler-rt-4.0.1.src/lib/asan/asan_rtl.cc:69
    #1 0x4f5755 in __sanitizer::CheckFailed(char const*, int, char const*, unsigned long long, unsigned long long) /var/tmp/portage/sys-libs/compiler-rt-sanitizers-4.0.1/work/compiler-rt-4.0.1.src/lib/sanitizer_common/sanitizer_termination.cc:79
    #2 0x4e4d82 in __sanitizer::ReportMmapFailureAndDie(unsigned long, char const*, char const*, int, bool) /var/tmp/portage/sys-libs/compiler-rt-sanitizers-4.0.1/work/compiler-rt-4.0.1.src/lib/sanitizer_common/sanitizer_common.cc:120
    #3 0x4ee685 in __sanitizer::MmapOrDie(unsigned long, char const*, bool) /var/tmp/portage/sys-libs/compiler-rt-sanitizers-4.0.1/work/compiler-rt-4.0.1.src/lib/sanitizer_common/sanitizer_posix.cc:132
    #4 0x42608a in __sanitizer::LargeMmapAllocator<__asan::AsanMapUnmapCallback>::Allocate(__sanitizer::AllocatorStats*, unsigned long, unsigned long) /var/tmp/portage/sys-libs/compiler-rt-sanitizers-4.0.1/work/compiler-rt-4.0.1.src/lib/asan/../sanitizer_common/sanitizer_allocator_secondary.h:41
    #5 0x42608a in __sanitizer::CombinedAllocator<__sanitizer::SizeClassAllocator64<__asan::AP64>, __sanitizer::SizeClassAllocatorLocalCache<__sanitizer::SizeClassAllocator64<__asan::AP64> >, __sanitizer::LargeMmapAllocator<__asan::AsanMapUnmapCallback> >::Allocate(__sanitizer::SizeClassAllocatorLocalCache<__sanitizer::SizeClassAllocator64<__asan::AP64> >*, unsigned long, unsigned long, bool, bool) /var/tmp/portage/sys-libs/compiler-rt-sanitizers-4.0.1/work/compiler-rt-4.0.1.src/lib/asan/../sanitizer_common/sanitizer_allocator_combined.h:70
    #6 0x42608a in __asan::Allocator::Allocate(unsigned long, unsigned long, __sanitizer::BufferedStackTrace*, __asan::AllocType, bool) /var/tmp/portage/sys-libs/compiler-rt-sanitizers-4.0.1/work/compiler-rt-4.0.1.src/lib/asan/asan_allocator.cc:407
    #7 0x420d3f in __asan::asan_realloc(void*, unsigned long, __sanitizer::BufferedStackTrace*) /var/tmp/portage/sys-libs/compiler-rt-sanitizers-4.0.1/work/compiler-rt-4.0.1.src/lib/asan/asan_allocator.cc:791
    #8 0x4d0df1 in realloc /var/tmp/portage/sys-libs/compiler-rt-sanitizers-4.0.1/work/compiler-rt-4.0.1.src/lib/asan/asan_malloc_linux.cc:93
    #9 0x7f4edab1b7e5 in _zip_cdir_grow /var/tmp/portage/dev-libs/libzip-1.2.0/work/libzip-1.2.0/lib/zip_dirent.c:108:37
    #10 0x7f4edab1b7e5 in _zip_cdir_new /var/tmp/portage/dev-libs/libzip-1.2.0/work/libzip-1.2.0/lib/zip_dirent.c:82
    #11 0x7f4edab390d0 in _zip_read_eocd64 /var/tmp/portage/dev-libs/libzip-1.2.0/work/libzip-1.2.0/lib/zip_open.c:854:13
    #12 0x7f4edab390d0 in _zip_read_cdir /var/tmp/portage/dev-libs/libzip-1.2.0/work/libzip-1.2.0/lib/zip_open.c:285
    #13 0x7f4edab390d0 in _zip_find_central_dir /var/tmp/portage/dev-libs/libzip-1.2.0/work/libzip-1.2.0/lib/zip_open.c:613
    #14 0x7f4edab390d0 in _zip_open /var/tmp/portage/dev-libs/libzip-1.2.0/work/libzip-1.2.0/lib/zip_open.c:200
    #15 0x7f4edab369b7 in zip_open_from_source /var/tmp/portage/dev-libs/libzip-1.2.0/work/libzip-1.2.0/lib/zip_open.c:148:11
    #16 0x7f4edab35e93 in zip_open /var/tmp/portage/dev-libs/libzip-1.2.0/work/libzip-1.2.0/lib/zip_open.c:74:15
    #17 0x513392 in read_from_file /var/tmp/portage/dev-libs/libzip-1.2.0/work/libzip-1.2.0/src/ziptool.c:698:13
    #18 0x513392 in main /var/tmp/portage/dev-libs/libzip-1.2.0/work/libzip-1.2.0/src/ziptool.c:1113
    #19 0x7f4ed9c43680 in __libc_start_main /var/tmp/portage/sys-libs/glibc-2.23-r4/work/glibc-2.23/csu/../csu/libc-start.c:289
    #20 0x41b058 in _init (/usr/bin/ziptool+0x41b058)

Testcase:
https://github.com/asarubbo/poc/blob/master/00330-libzip-memallocfailure-_zip_cdir_grow

runtime error: null pointer passed as argument 1, which is declared to never be null (zip_extra_field.c:165)

The attached malformed zip file triggers this undefined behavior/runtime error in 0cf4ff, compiled with clang 6.0.0-trunk and -fsanitize=undefined.

./ziptool -c test000.zip cat index

zip_extra_field.c:165:65: runtime error: null pointer passed as argument 1, which is declared to never be null
/usr/include/string.h:70:33: note: nonnull attribute specified here
    #0 0x51e668 in _zip_ef_merge /root/libzip/lib/zip_extra_field.c:165:58
    #1 0x53f292 in _zip_checkcons /root/libzip/lib/zip_open.c:479:36
    #2 0x53b127 in _zip_find_central_dir /root/libzip/lib/zip_open.c:627:28
    #3 0x53b127 in _zip_open /root/libzip/lib/zip_open.c:194
    #4 0x538617 in zip_open_from_source /root/libzip/lib/zip_open.c:148:11
    #5 0x537f6b in zip_open /root/libzip/lib/zip_open.c:74:15
    #6 0x4fe7bc in read_from_file /root/libzip/src/ziptool.c:709:12
    #7 0x4fe7bc in main /root/libzip/src/ziptool.c:1139
    #8 0x7f0ee8c9cb44 in __libc_start_main /build/glibc-6V9RKT/glibc-2.19/csu/libc-start.c:287
    #9 0x41d1db in _start (/root/libzip/src/ziptool+0x41d1db)

SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior zip_extra_field.c:165:65 in
Aborted

test000.zip

Cmake doesn't work, Why?

When configuring CMake it generates "Incomplete files : Configuring incomplete, errors occurred!",

CMake Error at G:/CMake/share/cmake-3.9/Modules/FindPackageHandleStandardArgs.cmake:137 (message):
Could NOT find ZLIB (missing: ZLIB_INCLUDE_DIR)
Call Stack (most recent call first):
G:/CMake/share/cmake-3.9/Modules/FindPackageHandleStandardArgs.cmake:377 (_FPHSA_FAILURE_MESSAGE)
G:/CMake/share/cmake-3.9/Modules/FindZLIB.cmake:112 (FIND_PACKAGE_HANDLE_STANDARD_ARGS)
CMakeLists.txt:174 (FIND_PACKAGE)

Failing to get comment with some zips

addonsu-15.1-arm64-signed.zip

Consider the attached zip (or any other zip file downloaded from lineageos.org).

If I use zip_get_archive_comment(archive, nullptr, ZIP_FL_ENC_GUESS) I get the following string:

signed by SignApk 0é♣ô♠○*åHå≈♪☺•☻áé♣ä0é♣Ç☻☺☺1♂0○♠♣+♫♥☻→♣ 0♂♠○*åHå≈♪☺•☺áé♥╖0é♥│0é☻¢á♥☻☺☻☻○ ß♦‼╟s├┼O0♪♠○*åHå≈♪☺☺♣♣ 0p1♂0○♠♥U♦♠‼☻US1‼0◄♠♥U♦◘♀◙Washington1►0♫♠♥U♦•♀•Seattle1↕0►♠♥U♦◙♀○LineageOS1↕0►♠♥U♦♂♀○LineageOS1↕0►♠♥U♦♥♀○LineageOS0▲↨♪170107042125Z↨♪440525042125Z0p1♂0○♠♥U♦♠‼☻US1‼0◄♠♥U♦◘♀◙Washington1►0♫♠♥U♦•♀•Seattle1↕0►♠♥U♦◙♀○LineageOS1↕0►♠♥U♦♂♀○LineageOS1↕0►♠♥U♦♥♀○LineageOS0é☺\"0♪♠○*åHå≈♪☺☺☺♣ ♥é☺☼ 0é☺◙☻é☺☺ ªM╙ß°B♥Å≡?g╕Θ┐○S☼┬æ<╡>6T╟Ä┬♪╝ï▲q‼bîѽ└å♣`╦D,←Q∙ïm╬^Y─É7≥⌂d⌠è∩♦ɽÖ►o◘•ó‼♫→ï:¼╪4µV≡àK`&w╢l {¶┬╨┬ì♪╞‼A▐dìç¥╡Ü₧yß(ês→;▒╞▼╡ÅrZ└q▐↕╢►ô▐╠Và¼$t╬Γ╧\"≡ΣK≥◘A♪æ┼╤÷◙ºöUHé+¡ujpα■↨╚?5σ·Y╛M╥┤F♦Å¢B3!å ◙ñ~╙£!♪Θ²ÿ∟Uq₧╚≤♀¬─A\\!`_‼⌡}♫═≡h,HNöVtìδΦ♫/.♂∟⌠⌂╣]Æ ⌠g>▬÷7ü≤▓╟═KÉ@A½δ]°PYÖ☻♥☺ ☺úP0N0↔♠♥U↔♫♦▬♦¶rû2'╓lLM_áæj┬,y<╘_C\\0▼♠♥U↔#♦↑0▬Ƕrû2'╓lLM_áæj┬,y<╘_C\\0♀♠♥U↔‼♦♣0♥☺☺ 0♪♠○*åHå≈♪☺☺♣♣ ♥é☺☺ è$~☺╔╟.qg▐┬Iô≥?S£•aî(↓εM>«û╥â↨:δß↕Fö♣──ÇR∩▲╟/)I¼♠w┤₧¼PU╛ó■u¿▌=ⁿï<╧ê√►─á┬w¥┬å▲Ç┤E↕î↑H\\Γ┤┤≈O≡8ÆGe¢SåW#t╨∙┐&╠┬aτâ9|úÇìZ↓▐&°█ä┘*╞ç+♂█╞◙╟hΣ¼t╤Bτ≥┼╒┴╩╗╩]←¬█Æ2R▒¢áò♥p╒Vp|7├Ö╔6ª~◘²↨H╬Ñemè▌⌂ó↨~╓0ymìα·ΘW♂'J♦Y○ä╒└e■0zSFδTû.A⌡Ç╤ñHF1↕Γ╒⌠æú¢╣∙óá╩┼à\\(ú_╜▒¡JjΩ▓«ó∙♣nsⁿHw7δ1é☺ñ0é☺á☻☺☺0}0p1♂0○♠♥U♦♠‼☻US1‼0◄♠♥U♦◘♀◙Washington1►0♫♠♥U♦•♀•Seattle1↕0►♠♥U♦◙♀○LineageOS1↕0►♠♥U♦♂♀○LineageOS1↕0►♠♥U♦♥♀○LineageOS☻○ ß♦‼╟s├┼O0○♠♣+♫♥☻→♣ 0♪♠○*åHå≈♪☺☺☺♣ ♦é☺ z#┐╒└äÑ@►∙╬f◙c♫ƒ╙└àτZnα1t ¬u◄µ0┐02◄Öë:⌂èï‼├X╞¡°bO♀M→?5♥╙♀ë±aæ*à¿(ò≥h╒Öq<@[╣Eá%♦'╝gbuY╞±5\"«|╬╣4½♥☻k☻Erjj/⌡₧§<╜α5╟w≈I░┤3└╠▀,O╚π↔∙▄≈/πw╧╠|▬h☼?c$≈▐9µRgDm◄v+╣N«7π1►°«¢!`¥_û∩╬○6Knƒ⌠]!└▀╙ïè≥▒╧@┌+▌ ≡╘Å╔l╖T+└\"OæoCâ~↑à╟-ùö⌐▌eÆ♪Å♦π○`zÉ≥â▀0╛♂╞║Θp╕⌡î÷¥▼╤Φ↨├,╥jd▒▲♀│↕·╩ö¥♣  »♣

instead of just:

signed by SignApk

use-after-free in _zip_buffer_free (zip_buffer.c)

On 1.2.0:

# ziptool $FILE cat index
==1771==ERROR: AddressSanitizer: heap-use-after-free on address 0x6030000000d1 at pc 0x7f267d085fc1 bp 0x7ffed21f65f0 sp 0x7ffed21f65e8                                                                           
READ of size 1 at 0x6030000000d1 thread T0                                                                                                                                                                        
    #0 0x7f267d085fc0 in _zip_buffer_free /var/tmp/portage/dev-libs/libzip-1.2.0/work/libzip-1.2.0/lib/zip_buffer.c:53:17                                                                                         
    #1 0x7f267d092646 in _zip_dirent_read /var/tmp/portage/dev-libs/libzip-1.2.0/work/libzip-1.2.0/lib/zip_dirent.c                                                                                               
    #2 0x7f267d0aabfe in _zip_read_cdir /var/tmp/portage/dev-libs/libzip-1.2.0/work/libzip-1.2.0/lib/zip_open.c:380:69                                                                                            
    #3 0x7f267d0aabfe in _zip_find_central_dir /var/tmp/portage/dev-libs/libzip-1.2.0/work/libzip-1.2.0/lib/zip_open.c:613                                                                                        
    #4 0x7f267d0aabfe in _zip_open /var/tmp/portage/dev-libs/libzip-1.2.0/work/libzip-1.2.0/lib/zip_open.c:200                                                                                                    
    #5 0x7f267d0a89b7 in zip_open_from_source /var/tmp/portage/dev-libs/libzip-1.2.0/work/libzip-1.2.0/lib/zip_open.c:148:11                                                                                      
    #6 0x7f267d0a7e93 in zip_open /var/tmp/portage/dev-libs/libzip-1.2.0/work/libzip-1.2.0/lib/zip_open.c:74:15                                                                                                   
    #7 0x513392 in read_from_file /var/tmp/portage/dev-libs/libzip-1.2.0/work/libzip-1.2.0/src/ziptool.c:698:13                                                                                                   
    #8 0x513392 in main /var/tmp/portage/dev-libs/libzip-1.2.0/work/libzip-1.2.0/src/ziptool.c:1113                                                                                                               
    #9 0x7f267c1b5680 in __libc_start_main /var/tmp/portage/sys-libs/glibc-2.23-r4/work/glibc-2.23/csu/../csu/libc-start.c:289                                                                                    
    #10 0x41b058 in _init (/usr/bin/ziptool+0x41b058)                                                                                                                                                             
                                                                                                                                                                                                                  
0x6030000000d1 is located 1 bytes inside of 32-byte region [0x6030000000d0,0x6030000000f0)                                                                                                                        
freed by thread T0 here:                                                                                                                                                                                          
    #0 0x4d0850 in __interceptor_cfree /var/tmp/portage/sys-libs/compiler-rt-sanitizers-4.0.1/work/compiler-rt-4.0.1.src/lib/asan/asan_malloc_linux.cc:55                                                         
    #1 0x7f267d085f83 in _zip_buffer_free /var/tmp/portage/dev-libs/libzip-1.2.0/work/libzip-1.2.0/lib/zip_buffer.c:57:5                                                                                          
    #2 0x7f267d0935ea in _zip_dirent_read /var/tmp/portage/dev-libs/libzip-1.2.0/work/libzip-1.2.0/lib/zip_dirent.c:571:9                                                                                         
    #3 0x7f267d0aabfe in _zip_read_cdir /var/tmp/portage/dev-libs/libzip-1.2.0/work/libzip-1.2.0/lib/zip_open.c:380:69                                                                                            
    #4 0x7f267d0aabfe in _zip_find_central_dir /var/tmp/portage/dev-libs/libzip-1.2.0/work/libzip-1.2.0/lib/zip_open.c:613                                                                                        
    #5 0x7f267d0aabfe in _zip_open /var/tmp/portage/dev-libs/libzip-1.2.0/work/libzip-1.2.0/lib/zip_open.c:200                                                                                                    
    #6 0x7f267d0a89b7 in zip_open_from_source /var/tmp/portage/dev-libs/libzip-1.2.0/work/libzip-1.2.0/lib/zip_open.c:148:11                                                                                      
    #7 0x7f267d0a7e93 in zip_open /var/tmp/portage/dev-libs/libzip-1.2.0/work/libzip-1.2.0/lib/zip_open.c:74:15                                                                                                   
    #8 0x513392 in read_from_file /var/tmp/portage/dev-libs/libzip-1.2.0/work/libzip-1.2.0/src/ziptool.c:698:13
    #9 0x513392 in main /var/tmp/portage/dev-libs/libzip-1.2.0/work/libzip-1.2.0/src/ziptool.c:1113
    #10 0x7f267c1b5680 in __libc_start_main /var/tmp/portage/sys-libs/glibc-2.23-r4/work/glibc-2.23/csu/../csu/libc-start.c:289

previously allocated by thread T0 here:
    #0 0x4d0a08 in malloc /var/tmp/portage/sys-libs/compiler-rt-sanitizers-4.0.1/work/compiler-rt-4.0.1.src/lib/asan/asan_malloc_linux.cc:66
    #1 0x7f267d0879d8 in _zip_buffer_new /var/tmp/portage/dev-libs/libzip-1.2.0/work/libzip-1.2.0/lib/zip_buffer.c:168:35
    #2 0x7f267d0879d8 in _zip_buffer_new_from_source /var/tmp/portage/dev-libs/libzip-1.2.0/work/libzip-1.2.0/lib/zip_buffer.c:190
    #3 0x7f267d0927c2 in _zip_dirent_read /var/tmp/portage/dev-libs/libzip-1.2.0/work/libzip-1.2.0/lib/zip_dirent.c:443:23
    #4 0x7f267d0aabfe in _zip_read_cdir /var/tmp/portage/dev-libs/libzip-1.2.0/work/libzip-1.2.0/lib/zip_open.c:380:69
    #5 0x7f267d0aabfe in _zip_find_central_dir /var/tmp/portage/dev-libs/libzip-1.2.0/work/libzip-1.2.0/lib/zip_open.c:613
    #6 0x7f267d0aabfe in _zip_open /var/tmp/portage/dev-libs/libzip-1.2.0/work/libzip-1.2.0/lib/zip_open.c:200
    #7 0x7f267d0a89b7 in zip_open_from_source /var/tmp/portage/dev-libs/libzip-1.2.0/work/libzip-1.2.0/lib/zip_open.c:148:11
    #8 0x7f267d0a7e93 in zip_open /var/tmp/portage/dev-libs/libzip-1.2.0/work/libzip-1.2.0/lib/zip_open.c:74:15
    #9 0x513392 in read_from_file /var/tmp/portage/dev-libs/libzip-1.2.0/work/libzip-1.2.0/src/ziptool.c:698:13
    #10 0x513392 in main /var/tmp/portage/dev-libs/libzip-1.2.0/work/libzip-1.2.0/src/ziptool.c:1113
    #11 0x7f267c1b5680 in __libc_start_main /var/tmp/portage/sys-libs/glibc-2.23-r4/work/glibc-2.23/csu/../csu/libc-start.c:289

SUMMARY: AddressSanitizer: heap-use-after-free /var/tmp/portage/dev-libs/libzip-1.2.0/work/libzip-1.2.0/lib/zip_buffer.c:53:17 in _zip_buffer_free
Shadow bytes around the buggy address:
  0x0c067fff7fc0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c067fff7fd0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c067fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c067fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c067fff8000: fa fa 00 00 00 00 fa fa 00 00 00 00 fa fa fd fd
=>0x0c067fff8010: fd fd fa fa fd fd fd fd fa fa[fd]fd fd fd fa fa
  0x0c067fff8020: 00 00 00 00 fa fa fd fd fd fa fa fa fd fd fd fd
  0x0c067fff8030: fa fa 00 00 00 fa fa fa 00 00 00 fa fa fa 00 00
  0x0c067fff8040: 00 fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c067fff8050: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c067fff8060: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
Shadow byte legend (one shadow byte represents 8 application bytes):
  Addressable:           00
  Partially addressable: 01 02 03 04 05 06 07 
  Heap left redzone:       fa
  Freed heap region:       fd
  Stack left redzone:      f1
  Stack mid redzone:       f2
  Stack right redzone:     f3
  Stack after return:      f5
  Stack use after scope:   f8
  Global redzone:          f9
  Global init order:       f6
  Poisoned by user:        f7
  Container overflow:      fc
  Array cookie:            ac
  Intra object redzone:    bb
  ASan internal:           fe
  Left alloca redzone:     ca
  Right alloca redzone:    cb
==1771==ABORTING

Testcase:
https://github.com/asarubbo/poc/blob/master/00239-libzip-UAF-_zip_buffer_free

I don't know if it is related to the bug fixed here 2217022

Memory leak on simple test#1

I used sources libzip-1.4.0
The test_zip.cpp sources:
#include "zip.h"

int main()
{
const char* path = "./test.zip";
int errorp = 0;
zip_t *zip = zip_open(path, ZIP_CREATE | ZIP_TRUNCATE, &errorp);
zip_close(zip);
}

Compilation:
gcc test_zip.cpp -L. -lzip -lz -lbz2 -o test_zip

Run:
$ valgrind --leak-check=full --show-leak-kinds=all ./test_zip

Results:
$ valgrind --leak-check=full --show-leak-kinds=all ./test_zip
==19045== Memcheck, a memory error detector
==19045== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==19045== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==19045== Command: ./test_zip
==19045==
==19045==
==19045== HEAP SUMMARY:
==19045== in use at exit: 403 bytes in 5 blocks
==19045== total heap usage: 5 allocs, 0 frees, 403 bytes allocated
==19045==
==19045== 11 bytes in 1 blocks are indirectly lost in loss record 1 of 5
==19045== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==19045== by 0x52E8B49: strdup (strdup.c:42)
==19045== by 0x4164C2: _zip_source_file_or_p (zip_source_filep.c:150)
==19045== by 0x40F588: zip_source_file_create (zip_source_file.c:62)
==19045== by 0x407F2B: zip_open (zip_open.c:68)
==19045== by 0x401558: main (in ./test_zip)
==19045==
==19045== 24 bytes in 1 blocks are indirectly lost in loss record 2 of 5
==19045== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==19045== by 0x407211: _zip_hash_new (zip_hash.c:174)
==19045== by 0x414E73: _zip_new (zip_new.c:55)
==19045== by 0x409146: _zip_allocate_new (zip_open.c:526)
==19045== by 0x40818D: zip_open_from_source (zip_open.c:130)
==19045== by 0x407F76: zip_open (zip_open.c:74)
==19045== by 0x401558: main (in ./test_zip)
==19045==
==19045== 80 bytes in 1 blocks are indirectly lost in loss record 3 of 5
==19045== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==19045== by 0x40B74E: _zip_source_new (zip_source_function.c:83)
==19045== by 0x40B6B3: zip_source_function_create (zip_source_function.c:56)
==19045== by 0x4168AD: _zip_source_file_or_p (zip_source_filep.c:230)
==19045== by 0x40F588: zip_source_file_create (zip_source_file.c:62)
==19045== by 0x407F2B: zip_open (zip_open.c:68)
==19045== by 0x401558: main (in ./test_zip)
==19045==
==19045== 160 bytes in 1 blocks are indirectly lost in loss record 4 of 5
==19045== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==19045== by 0x416466: _zip_source_file_or_p (zip_source_filep.c:143)
==19045== by 0x40F588: zip_source_file_create (zip_source_file.c:62)
==19045== by 0x407F2B: zip_open (zip_open.c:68)
==19045== by 0x401558: main (in ./test_zip)
==19045==
==19045== 403 (128 direct, 275 indirect) bytes in 1 blocks are definitely lost in loss record 5 of 5
==19045== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==19045== by 0x414E3C: _zip_new (zip_new.c:49)
==19045== by 0x409146: _zip_allocate_new (zip_open.c:526)
==19045== by 0x40818D: zip_open_from_source (zip_open.c:130)
==19045== by 0x407F76: zip_open (zip_open.c:74)
==19045== by 0x401558: main (in ./test_zip)
==19045==
==19045== LEAK SUMMARY:
==19045== definitely lost: 128 bytes in 1 blocks
==19045== indirectly lost: 275 bytes in 4 blocks
==19045== possibly lost: 0 bytes in 0 blocks
==19045== still reachable: 0 bytes in 0 blocks
==19045== suppressed: 0 bytes in 0 blocks
==19045==
==19045== For counts of detected and suppressed errors, rerun with: -v
==19045== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

If I'm doing anything wrong, please comment

libzip 1.4.0 : encryption-nonrandom-aes tests fail

These tests are successful with libzip 1.3.2
Build on Solus (x86_64 GNU/Linux)

42/128 Testing: encryption-nonrandom-aes128.test
42/128 Test: encryption-nonrandom-aes128.test
Command: "/usr/bin/perl" "/home/build/YPKG/root/libzip/build/libzip-1.4.0/regre
ss/runtest" "/home/build/YPKG/root/libzip/build/libzip-1.4.0/regress/encryption
-nonrandom-aes128.test"
Directory: /home/build/YPKG/root/libzip/build/libzip-1.4.0/regress
"encryption-nonrandom-aes128.test" start time: Dec 29 21:28 UTC
Output:
----------------------------------------------------------
<end of output>
Test time =   0.05 sec
----------------------------------------------------------
Test Failed.
"encryption-nonrandom-aes128.test" end time: Dec 29 21:28 UTC
"encryption-nonrandom-aes128.test" time elapsed: 00:00:00
----------------------------------------------------------

43/128 Testing: encryption-nonrandom-aes192.test
43/128 Test: encryption-nonrandom-aes192.test
Command: "/usr/bin/perl" "/home/build/YPKG/root/libzip/build/libzip-1.4.0/regre
ss/runtest" "/home/build/YPKG/root/libzip/build/libzip-1.4.0/regress/encryption
-nonrandom-aes192.test"
Directory: /home/build/YPKG/root/libzip/build/libzip-1.4.0/regress
"encryption-nonrandom-aes192.test" start time: Dec 29 21:28 UTC
Output:
----------------------------------------------------------
<end of output>
Test time =   0.05 sec
----------------------------------------------------------
Test Failed.
"encryption-nonrandom-aes192.test" end time: Dec 29 21:28 UTC
"encryption-nonrandom-aes192.test" time elapsed: 00:00:00
----------------------------------------------------------

44/128 Testing: encryption-nonrandom-aes256.test
44/128 Test: encryption-nonrandom-aes256.test
Command: "/usr/bin/perl" "/home/build/YPKG/root/libzip/build/libzip-1.4.0/regre
ss/runtest" "/home/build/YPKG/root/libzip/build/libzip-1.4.0/regress/encryption
-nonrandom-aes256.test"
Directory: /home/build/YPKG/root/libzip/build/libzip-1.4.0/regress
"encryption-nonrandom-aes256.test" start time: Dec 29 21:28 UTC
Output:
----------------------------------------------------------
<end of output>
Test time =   0.05 sec
----------------------------------------------------------
Test Failed.
"encryption-nonrandom-aes256.test" end time: Dec 29 21:28 UTC
"encryption-nonrandom-aes256.test" time elapsed: 00:00:00
----------------------------------------------------------

Libraries and pkgconfig file need to install to /usr/lib64 on x86_64 systems

I've patched it here with the following, and while it's not useful for inclusion as-is, it can give an easy starting point:

diff -Nur libzip-1.4.0.orig/CMakeLists.txt libzip-1.4.0/CMakeLists.txt
--- libzip-1.4.0.orig/CMakeLists.txt    2017-12-29 06:37:22.000000000 -0600
+++ libzip-1.4.0/CMakeLists.txt 2017-12-29 15:00:54.745740270 -0600
@@ -141,7 +141,7 @@
 ADD_DEFINITIONS("-DHAVE_CONFIG_H")
 
 # rpath handling: use rpath in installed binaries
-SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
+SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib@LIBSUFFIX@")
 SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
 
 # Testing
@@ -157,7 +157,7 @@
 # pkgconfig file
 SET(prefix ${CMAKE_INSTALL_PREFIX})
 SET(exec_prefix \${prefix})
-SET(libdir \${exec_prefix}/lib)
+SET(libdir \${exec_prefix}/lib@LIBSUFFIX@)
 SET(includedir \${prefix}/include)
 IF(BZIP2_FOUND)
   SET(LIBS "${LIBS} -lbz2")
@@ -167,7 +167,7 @@
   SET(PKG_CONFIG_RPATH "-Wl,-R\${libdir}")
 ENDIF(CMAKE_SYSTEM_NAME MATCHES BSD)
 CONFIGURE_FILE(libzip.pc.in libzip.pc @ONLY)
-INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/libzip.pc DESTINATION lib/pkgconfig)
+INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/libzip.pc DESTINATION lib@LIBSUFFIX@/pkgconfig)
 
 # write out config file
 CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/cmake-config.h.in ${CMAKE_CURRENT_BINARY_DIR}/config.h)
diff -Nur libzip-1.4.0.orig/lib/CMakeLists.txt libzip-1.4.0/lib/CMakeLists.txt
--- libzip-1.4.0.orig/lib/CMakeLists.txt        2017-12-29 06:37:22.000000000 -0600
+++ libzip-1.4.0/lib/CMakeLists.txt     2017-12-29 15:01:29.343028229 -0600
@@ -204,6 +204,6 @@
 TARGET_LINK_LIBRARIES(zip ${ZLIB_LIBRARY} ${OPTIONAL_LIBRARY})
 INSTALL(TARGETS zip
   RUNTIME DESTINATION bin
-  ARCHIVE DESTINATION lib
-  LIBRARY DESTINATION lib)
+  ARCHIVE DESTINATION lib@LIBSUFFIX@
+  LIBRARY DESTINATION lib@LIBSUFFIX@)
 #CREATE_LIBTOOL_FILE(zip lib)

static lib and dynamic lib shouldn't be duplicate cmake targets

the cmake file for libraries is meant just to define how things are related, then you rely on the compiler to interpret those and put them together.

Static libs and shared libs can be built with pretty much the same definition of relationships.

The preferred way of doing it is: add_library(target sources), without STATIC or SHARED, thus relying on whatever is specified in BUILD_SHARED_LIBS to determine it. See https://cgold.readthedocs.io/en/latest/tutorials/libraries/static-shared.html

zip_fopen works with error ZIP_ER_ZLIB on Windows

I have a problem trying to read a ZIP archive by using zip_fopen function:

    auto archive = cip_server.get_archive_name();
    if ((zip_archive = zip_open(archive.c_str(), 0, &err)) == NULL) {
    	LOG_ERROR("Error: can't open zip archive.");
        GoToIdleState();
        return;
    }

    for (uint32_t i = 0; i < zip_get_num_entries(zip_archive, 0); i++) {
        struct zip_stat zip_stat;

        if (zip_stat_index(zip_archive, i, 0, &zip_stat) == 0) {
            LOG_INFO("Start uncompressing archive file");

            LOG_DEBUG_FMT("Zip Entry Name: [%s], ", zip_stat.name);
            LOG_DEBUG_FMT("Entry Size: [%llu], ", zip_stat.size);
            LOG_DEBUG_FMT("mtime: [%u]/n", (unsigned int)zip_stat.mtime);
            struct zip_file *zip_file = zip_fopen(zip_archive, zip_stat.name, 0);
            if (zip_file == NULL) {
            	zip_error_t* error = zip_get_error(zip_archive);
                LOG_ERROR_FMT("ERROR Zip: can't open uncompress entry in archive: %i",  zip_error_code_zip(error));
            	zip_error_fini(error);
                has_error = true;
                break;
            }
/* unrelated part of code */

This code shows message ERROR Zip: can't open uncompress entry in archive: 13 after it has tried to read an entry of the archive. I use it on Linux and don't have any problems. but it doesn't work on Windows at all. Do you have any ideas what I do wrong?

Thanks,

Confusing documentation for zip_get_archive_comment

Hi,
I'm having some issues to understand the zip_get_archive_comment manpage. I'm quoting the relevant part:

The name is in UTF-8 encoding unless ZIP_FL_ENC_RAW was specified (see below).

Do you mean the "comment", i.e. are you referring to the return value of the function?

If yes, what about ZIP_FL_ENC_GUESS ?

ZIP_FL_ENC_GUESS (Default.) Guess the encoding of the archive comment in the ZIP archive and convert it to UTF-8, if necessary.

Does this mean if I use ZIP_FL_ENC_GUESS I can get a non-UTF8 comment?

Am I right to understand that ZIP_FL_ENC_STRICT is the only flag that guarantees an UTF8-encoded comment?

zip_open fails with error 18 when file path contains umlauts

Dear authors of libzip,

first of all I want to thank you for your great zip library.
It was easy to implement and is much better in usage and performance than wxZip.

I write you because I have found a small issue.
When I try to open zip files using the zip_open function I get an error in some cases.
The function returns error code 18 when I try to open a zip with a file path containing umlauts.

#define ZIP_ER_INVAL 18 /* N Invalid argument */

Can you please tell me what I can do, to fix this issue?
I cannot prevent my users from opening zips where the file path contains umlauts.

Yours Sincerely

configure error

[root@new_centos6 libzip-1.5.1]# ./configure
-bash: ./configure: 没有那个文件或目录
[root@new_centos6 libzip-1.5.1]# make
make: *** 没有指明目标并且找不到 makefile。 停止。
[root@new_centos6 libzip-1.5.1]#

erors building on OSX v 1.5.0

hello,
trying to build on OSX 10.12.6, errors out with the following.

can someone confirm this is a simple typo? that fragments s.b. nfragments?

ziptool_regress.c:162:6: error: use of undeclared identifier 'zip_buffer_fragment_t'
:info:build             zip_buffer_fragment_t *fragments;

ptool_regress.c:161:19: note: 'nfragments' declared here
:info:build             zip_uint64_t nfragments, i, left;
:info:build                          ^

ziptool_regress.c:165:37: error: use of undeclared identifier 'fragments'
:info:build             if ((fragments = malloc(sizeof(fragments[0]) * nfragments)) == NULL) {

ziptool_regress.c:172:8: error: use of undeclared identifier 'fragments'
:info:build                 if ((fragments[i].data = malloc(left)) == NULL) {

ziptool_regress.c:176:9: error: use of undeclared identifier 'fragments'
:info:build                         free(fragments[i].data);

Documentation is unclear on functions available in 0.1x versions

At present I am trying to update some legacy code in a project that uses libzip as it was around 0.10 to 0.13 (I think) - whilst nearly everywhere that our project is used now has a 1.x version available there are a couple of corner cases (including one part of the C.I. systems - based on Ubuntu Trusty Tahr) that only has the older version by default. Whilst I can accommodate such mature systems with #if defined(LIBZIP_VERSION_MAJOR) && (LIBZIP_VERSION_MAJOR >= 1) to use the appropriate function depending on what is available it is hard to determine exactly what functions I do have available in the older version cases.

Could the man pages be updated to indicate (perhaps within a HISTORY section) which version of the libzip library gained a (working form) of each function; perhaps also this should include the addition/modification/removal of flag options where appropriate?

large file support was lost in the transition to cmake

The old autoconf based versions of libzip used AC_SYS_LARGEFILE for largefile support on 32 Bit platforms. With the switch to cmake this feature was lost.
32 Bit systems may be rare these days in the x86 world, but 32 Bit ARM systems are very common.
Also, this is not just about file sizes: off_t is also used for inode numbers. As a result stat() will fail if the inode number exceeds 32 Bit. This is quite common with network file systems and causes seemingly random failures.
I'm no cmake expert but libarchive seems to have a reasonable check for this. It may be a good starting point.

make failed

Scanning dependencies of target ziptool
[ 93%] Building C object src/CMakeFiles/ziptool.dir/ziptool.c.obj
C:/msys/home/admin/libzip-git/src/ziptool.c: In function 'get_encryption_method':
C:/msys/home/admin/libzip-git/src/ziptool.c:620:9: error: 'ZIP_EM_AES_128' undeclared (first use in this function)
  return ZIP_EM_AES_128;
         ^
C:/msys/home/admin/libzip-git/src/ziptool.c:620:9: note: each undeclared identifier is reported only once for each function it appears in
C:/msys/home/admin/libzip-git/src/ziptool.c:622:9: error: 'ZIP_EM_AES_192' undeclared (first use in this function)
  return ZIP_EM_AES_192;
         ^
C:/msys/home/admin/libzip-git/src/ziptool.c:624:9: error: 'ZIP_EM_AES_256' undeclared (first use in this function)
  return ZIP_EM_AES_256;
         ^
make[2]: *** [src/CMakeFiles/ziptool.dir/build.make:64: src/CMakeFiles/ziptool.dir/ziptool.c.obj] Ошибка 1
make[1]: *** [CMakeFiles/Makefile2:470: src/CMakeFiles/ziptool.dir/all] Ошибка 2
make: *** [Makefile:141: all] Ошибка 2

Memory leak on simple test#2

I used sources libzip-1.4.0
The test_zip.cpp sources:

#include "zip.h"
#include <string.h>

int main()
{
const char* path = "./test.zip";
int errorp = 0;
zip_t *zip = zip_open(path, ZIP_CREATE | ZIP_TRUNCATE, &errorp);
const char *buffer = "some buffer";
zip_source_t *source = zip_source_buffer(zip, buffer, strlen(buffer), 0);
zip_file_add(zip, "buffer.entry", source, ZIP_FL_OVERWRITE | ZIP_FL_ENC_UTF_8);
zip_close(zip);
}

Compilation:
gcc test_zip.cpp -L. -lzip -lz -lbz2 -o test_zip

Run:
$ valgrind --leak-check=full --show-leak-kinds=all ./test_zip

Results:
$ valgrind --leak-check=full --show-leak-kinds=all ./test_zip
==18970== Memcheck, a memory error detector
==18970== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==18970== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==18970== Command: ./test_zip
==18970==
==18970==
==18970== HEAP SUMMARY:
==18970== in use at exit: 16 bytes in 1 blocks
==18970== total heap usage: 47 allocs, 46 frees, 414,608 bytes allocated
==18970==
==18970== 16 bytes in 1 blocks are definitely lost in loss record 1 of 1
==18970== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==18970== by 0x4C2CF1F: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==18970== by 0x40B911: buffer_grow_fragments (zip_source_buffer.c:397)
==18970== by 0x40BA91: buffer_new (zip_source_buffer.c:441)
==18970== by 0x40AF40: zip_source_buffer_fragment_create (zip_source_buffer.c:133)
==18970== by 0x40AEA0: zip_source_buffer_create (zip_source_buffer.c:106)
==18970== by 0x40AE34: zip_source_buffer (zip_source_buffer.c:89)
==18970== by 0x401628: main (in ./test_zip)
==18970==
==18970== LEAK SUMMARY:
==18970== definitely lost: 16 bytes in 1 blocks
==18970== indirectly lost: 0 bytes in 0 blocks
==18970== possibly lost: 0 bytes in 0 blocks
==18970== still reachable: 0 bytes in 0 blocks
==18970== suppressed: 0 bytes in 0 blocks
==18970==
==18970== For counts of detected and suppressed errors, rerun with: -v
==18970== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

If I'm doing anything wrong, please comment

Mercurial down at hg.nih.at possible to create pointer to this repo ?

Hi Not Invented Here,

seems like your mercurial is down at hg.nih.at the apache returns a 502 since yesterday.

I needed a special version of your libzip to build a project and was searching for an alternative source and found this repo by luck.

I don't know if you mainly develop on mercurial or git but it would be nice to have a pointer up at hg.nih.at to this repo so no time is wasted googling around :)

memory leak in _zip_source_window_new

The window context it reserves is never freed. Caught with valgrind. Adding ZIP_SOURCE_FREE to its zip_source_make_command_bitmap call seems to fix it.

Disable tests when cross compiling

The unconditional run of tests breaks cross compilation:

/bin/sh: 1: ziptool: not found
regress/CMakeFiles/testinput.dir/build.make:59: recipe for target 'regress/CMakeFiles/testinput' failed

This is because the target binary can't run on the host. In this particular case, running ziptool manually produces:

/lib/ld-musl-armhf.so.1: No such file or directory

It would be best to disable the tests automatically when cross comping. If not, an option to disable test with -DBUILD_TESTING=OFF would help.

How to extract non-English file name from zip file?

I failed to use libzippp to extract non-English file name from zip file. I tried to replace zip_open with windows_open in its source, but still doesn't work.
Environment: Win10 + VS2015 + zip archived by 7zip.

Pass options when adding as external project in CMakeLists.txt

Hi,

Not sure if this is a bug or a misinterpretation on my side. I'm using CMake to add libzip as an external project to my code.

In my CMakeLists.txt, among other things, I include libzip like that :

set(LIBZIP_BUILD_DIR ${CMAKE_CURRENT_BINARY_DIR}/external-lib-libzip)
ExternalProject_Add(
    project_libzip
    GIT_REPOSITORY https://github.com/nih-at/libzip/
    GIT_TAG rel-1-5-1
    PREFIX ${LIBZIP_BUILD_DIR}
    CMAKE_ARGS "-DBUILD_TOOLS=OFF" "-DCMAKE_INSTALL_PREFIX=${LIBZIP_BUILD_DIR}" "-DBUILD_REGRESS=OFF"  "-DBUILD_EXAMPLES=OFF" "-DBUILD_DOC=OFF"
)
add_library(libzip STATIC IMPORTED)

What I'm trying to achieve here is to build libzip from source, without the examples, docs, regress and tools (since I don't need them). To this avail, I'm passing the following options in CMAKE_ARGS : BUILD_DOC BUILD_EXAMPLES BUILD_REGRESS BUILD_TOOLS

When I run cmake and then make for my target, I get the following warning :

CMake Warning:
  Manually-specified variables were not used by the project:

    BUILD_DOC
    BUILD_EXAMPLES
    BUILD_REGRESS
    BUILD_TOOLS

And thus all the examples, etc, are built. The twist here is that the CMAKE_INSTALL_PREFIX is correctly taken into account (and no warning is issued for this option).

Is this a problem in this repo's CMakeLists.txt or is this me not understanding how all this should work ?

Thanks a lot for your time !
BR

pkg-config file does not use Libs/Libs.private correctly

The Libs: line should only contain the shared libraries required to use libzip (i.e. just libzip itself). Libs.private should contain libraries required for static linking, i.e. the dependencies.

In particular, -lbz2 should be under Libs.private and not under Libs – otherwise, it breaks linking in many cases.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.