c++ - Libzip example contains uninitialised values when checked with Valgrind -


i've been using libzip handle zip files, , have based code on example found in rodrigo's answer this question. here's code, quick reference:

#include <zip.h>  int main() {     //open zip archive     int err = 0;     zip *z = zip_open("foo.zip", 0, &err);      //search file of given name     const char *name = "file.txt";     struct zip_stat st;     zip_stat_init(&st);     zip_stat(z, name, 0, &st);      //alloc memory uncompressed contents     char *contents = new char[st.size];      //read compressed file     zip_file *f = zip_fopen(z, "file.txt", 0);     zip_fread(f, contents, st.size);     zip_fclose(f);      //and close archive     zip_close(z); } 

i traced errors subsequently got valgrind code- complains of uninitialised value when opening zipped "file.txt" using 'zip_fopen()`.

==29256== conditional jump or move depends on uninitialised value(s) ==29256==    @ 0x5b4b290: inflatereset2 (in /usr/lib/libz.so.1.2.3.4) ==29256==    0x5b4b37f: inflateinit2_ (in /usr/lib/libz.so.1.2.3.4) ==29256==    0x4e2eb8c: zip_fopen_index (in /usr/lib/libzip.so.1.0.0) ==29256==    0x400c32: main (main.cpp:24) ==29256==  uninitialised value created heap allocation ==29256==    @ 0x4c244e8: malloc (vg_replace_malloc.c:236) ==29256==    0x5b4b35b: inflateinit2_ (in /usr/lib/libz.so.1.2.3.4) ==29256==    0x4e2eb8c: zip_fopen_index (in /usr/lib/libzip.so.1.0.0) ==29256==    0x400c32: main (main.cpp:24) ==29256== ==29256== ==29256== heap summary: ==29256==     in use @ exit: 71 bytes in 1 blocks ==29256==   total heap usage: 26 allocs, 25 frees, 85,851 bytes allocated ==29256== ==29256== 71 bytes in 1 blocks lost in loss record 1 of 1 ==29256==    @ 0x4c24a72: operator new[](unsigned long) (vg_replace_malloc.c:305) ==29256==    0x400bee: main (main.cpp:19) 

i can't see uninitialised value coming in code. can trace this, or fault lie libzip itself? should switch zip library- example, minizip?

edit: 71 bytes contents of file.txt read in- delete[] contents; tagged on end eliminate that.

(i'd have left comment on original answer draw attention issue, not have requisite rep.)

you made me :)

yes, it's error inside zlib (used libzip), since both allocation , use of memory inside inflateinit2_ on same call. code doesn't have chance memory.

i can repeat problem using zlib 1.2.3, it's not showing anymore in 1.2.7. didn't have code 1.2.3 available, if you're looking @ it, i'd check initialization of state , how it's used inside inflatereset2.

edit: tracked down problem, downloaded ubuntu's source package zlib (1.2.3.4) , offending line is;

if (state->wbits != windowbits && state->window != z_null) { 

wbits not initialized previous this, , cause warning. odd thing neither original zlib 1.2.3 or 1.2.4 have problem, seems unique ubuntu. 1.2.3 doesn't have function inflatereset2, , 1.2.4 has right;

if (state->window != z_null && state->wbits != (unsigned)windowbits) { 

since window initialized z_null, uninitialized wbits read won't happen.


Comments

Popular posts from this blog

c# - must be a non-abstract type with a public parameterless constructor in redis -

c# - ReportViewer control - axd url -

ssl - no cipher suites in common on Liberty Websphere -