Note that there are some explanatory texts on larger screens.

plurals
  1. POLibzip example contains uninitialised values when checked with Valgrind
    primarykey
    data
    text
    <p>I've been using libzip to handle zip files, and have based my code on the example found in rodrigo's answer to <a href="https://stackoverflow.com/questions/10440113/simple-way-to-unzip-a-zip-file-using-zlib">this question</a>. Here's his code, for quick reference:</p> <pre class="lang-cpp prettyprint-override"><code>#include &lt;zip.h&gt; int main() { //Open the ZIP archive int err = 0; zip *z = zip_open("foo.zip", 0, &amp;err); //Search for the file of given name const char *name = "file.txt"; struct zip_stat st; zip_stat_init(&amp;st); zip_stat(z, name, 0, &amp;st); //Alloc memory for its uncompressed contents char *contents = new char[st.size]; //Read the compressed file zip_file *f = zip_fopen(z, "file.txt", 0); zip_fread(f, contents, st.size); zip_fclose(f); //And close the archive zip_close(z); } </code></pre> <p>I traced the errors I subsequently got from Valgrind back to this code- it complains of an uninitialised value when opening the zipped "file.txt" using 'zip_fopen()`. </p> <pre><code>==29256== Conditional jump or move depends on uninitialised value(s) ==29256== at 0x5B4B290: inflateReset2 (in /usr/lib/libz.so.1.2.3.4) ==29256== by 0x5B4B37F: inflateInit2_ (in /usr/lib/libz.so.1.2.3.4) ==29256== by 0x4E2EB8C: zip_fopen_index (in /usr/lib/libzip.so.1.0.0) ==29256== by 0x400C32: main (main.cpp:24) ==29256== Uninitialised value was created by a heap allocation ==29256== at 0x4C244E8: malloc (vg_replace_malloc.c:236) ==29256== by 0x5B4B35B: inflateInit2_ (in /usr/lib/libz.so.1.2.3.4) ==29256== by 0x4E2EB8C: zip_fopen_index (in /usr/lib/libzip.so.1.0.0) ==29256== by 0x400C32: main (main.cpp:24) ==29256== ==29256== ==29256== HEAP SUMMARY: ==29256== in use at 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 are definitely lost in loss record 1 of 1 ==29256== at 0x4C24A72: operator new[](unsigned long) (vg_replace_malloc.c:305) ==29256== by 0x400BEE: main (main.cpp:19) </code></pre> <p>I can't see where the uninitialised value is coming from in this code. Can anyone trace this, or does the fault lie with libzip itself? Should I switch to another zip library- for example, Minizip?</p> <p>EDIT: The 71 bytes are the contents of file.txt which was read in- <code>delete[] contents;</code> tagged on the end will eliminate that.</p> <p>(I'd have left a comment on the original answer to draw attention to the issue, but I do not have the requisite rep.)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload