gpt4 book ai didi

c - libmagic 内存泄漏?

转载 作者:行者123 更新时间:2023-11-30 15:02:31 25 4
gpt4 key购买 nike

尝试一些 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报告来自库的memleak:

 ==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

根据那里的讨论,应该在最新版本中修复它。

关于c - libmagic 内存泄漏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41013977/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com