试一下libmagic的一些测试代码:

 #include <magic.h>
 #include <stdio.h>

 int main(int argc, char **argv) {

    magic_t cookie;

    cookie = magic_open(MAGIC_MIME_TYPE);

    if (cookie == NULL) {
        perror(magic_error(cookie));
        return 1;
    }

    if (magic_load(cookie, NULL) == -1) {
        perror(magic_error(cookie));
        return 1;
    }

    const char *string = magic_file(cookie, *(argv+1));

    if (string == NULL) {
        perror(magic_error(cookie));
        return 1;
    }

    printf("%s\n", string);

    magic_close(cookie);
    return 0;
 }


代码从这里几乎撕开了:

http://vivithemage.co.uk/blog/?p=105

该代码运行正常,但是报告了使用valgrind --leak-check=full ./libmagic /path/to/some/image/file库中的内存泄漏:

 ==6153== HEAP SUMMARY:
 ==6153==     in use at exit: 48 bytes in 3 blocks
 ==6153==   total heap usage: 36 allocs, 33 frees, 990,559 bytes allocated
 ==6153==
 ==6153== 48 bytes in 1 blocks are definitely lost in loss record 2 of 2
 ==6153==    at 0x4C2FB55: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
 ==6153==    by 0x4E43D3D: ??? (in /usr/lib/x86_64-linux-gnu/libmagic.so.1.0.0)
 ==6153==    by 0x4E447ED: ??? (in /usr/lib/x86_64-linux-gnu/libmagic.so.1.0.0)
 ==6153==    by 0x4008A8: main (libmagic.c:15)
 ==6153==
 ==6153== LEAK SUMMARY:
 ==6153==    definitely lost: 48 bytes in 1 blocks
 ==6153==    indirectly lost: 0 bytes in 2 blocks
 ==6153==      possibly lost: 0 bytes in 0 blocks
 ==6153==    still reachable: 0 bytes in 0 blocks
 ==6153==         suppressed: 0 bytes in 0 blocks


我是否需要除magic_close()以外的其他东西来打包,还是库中存在一些弱点?

magic.h说MAGIC_VERSION 524,我使用的是gcc 5.4.0

最佳答案

我今天刚遇到同一问题,似乎有一个缺陷报告:

https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=840754

根据讨论,应该将其修复为最新版本。

08-16 04:18