使用Valgrind检查时,Libzip示例包含未初始化的值

Sam*_*gan 7 c++ zip valgrind

我一直在使用libzip来处理zip文件,并且我的代码基于rodrigo对这个问题的答案中找到的例子.这是他的代码,供快速参考:

#include <zip.h>

int main()
{
    //Open the ZIP archive
    int err = 0;
    zip *z = zip_open("foo.zip", 0, &err);

    //Search for the 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 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);
}
Run Code Online (Sandbox Code Playgroud)

我将随后从Valgrind得到的错误追溯到这个代码 - 当使用'zip_fopen()`打开压缩的"file.txt"时,它会抱怨未初始化的值.

==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)
Run Code Online (Sandbox Code Playgroud)

我无法在此代码中看到未初始化值的来源.任何人都可以追踪这个,或者libzip本身的错误是什么?我应该切换到另一个zip库 - 例如,Minizip吗?

编辑:71字节是file.txt的内容,最后读取delete[] contents;标记将消除.

(我已经对最初的答案留下了评论,以引起对这个问题的关注,但我没有必要的代表.)

Joa*_*son 6

你让我看:)

是的,这是zlib中的一个错误(由libzip使用),因为内存的分配和使用都在inflateInit2_同一个调用中.你的代码甚至没有机会进入那段记忆.

我可以使用zlib 1.2.3重复这个问题,但它在1.2.7中不再出现了.我没有1.2.3的代码可用,但如果你正在查看它,我会检查初始化state以及它在内部的使用方式inflateReset2.

编辑:追踪问题,我下载了Ubuntu的zlib源代码包(1.2.3.4)和违规行;

if (state->wbits != windowBits && state->window != Z_NULL) {
Run Code Online (Sandbox Code Playgroud)

wbits在此之前未初始化,并将导致警告.奇怪的是原始的zlib 1.2.3或1.2.4都没有这个问题,它似乎对Ubuntu来说是独一无二的.1.2.3甚至没有inflateReset2功能,而1.2.4没有权利;

if (state->window != Z_NULL && state->wbits != (unsigned)windowBits) {
Run Code Online (Sandbox Code Playgroud)

由于window先前已初始化为Z_NULL,因此wbits不会发生未初始化的读取.