libarchive / libarchive

Multi-format archive and compression library

Home Page:http://www.libarchive.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

memory leak

dududuguo opened this issue · comments

The command in Wsl2 for Ubuntu22.04 is :
$ ./bsdcat --version
bsdcat 3.7.4 - libarchive 3.7.4 zlib/1.2.11 libzstd/1.4.8

I found the archive_version_details() has return const char *.
the leak path is: in libarchive/archive_string.c:316 : "p = (char *)realloc(as->s, new_length);"
#0 at libarchive/archive_string.c:316
#1 libarchive/archive_string.c:205
#2 libarchive/archive_string.c:351
#3 libarchive/archive_string.c:383
#4 libarchive/archive_version_details.c:91
#5 cat/bsdcat.c:68
#6 cat/bsdcat.c:138"

So may we can fix it the be:
version(void)
{
char *p = archive_version_details();
printf("bsdcat %s - %s \n",
BSDCAT_VERSION_STRING,
p);
free(p);
exit(0);
}
$ valgrind ./bsdcat --version;
==81973== Memcheck, a memory error detector
==81973== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==81973== Using Valgrind-3.18.1 and LibVEX; rerun with -h for copyright info
==81973== Command: ./bsdcat --version
==81973==
bsdcat 3.7.4 - libarchive 3.7.4 zlib/1.2.11 libzstd/1.4.8
==81973==
==81973== HEAP SUMMARY:
==81973== in use at exit: 0 bytes in 0 blocks
==81973== total heap usage: 4 allocs, 4 frees, 73,824 bytes allocated
==81973==
==81973== All heap blocks were freed -- no leaks are possible
==81973==
==81973== For lists of detected and suppressed errors, rerun with: -s
==81973== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

Thank you!

The archive_version_details function lazily initializes a static variable. That data is owned by the archive_version_details function and should not be released by anyone outside of that function. There is in fact no need to ever release that memory.