Trynig some test code for 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;
}
The code is pretty much ripped from here:
http://vivithemage.co.uk/blog/?p=105
The code runs ok, but using valgrind --leak-check=full ./libmagic /path/to/some/image/file
a memleak from the library is reported:
==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
Do I need something else than magic_close()
to wrap up, or is there some weakness in the library?
magic.h says MAGIC_VERSION 524, I'm using gcc 5.4.0