systemd-path: fix memory leak reported by ASAN (#8874)
The leak can be reproduced by running systemd-path --suffix .tmp under valgrind or asan:
$ ./build/systemd-path --suffix .tmp search-binaries
/usr/local/bin/.tmp:/usr/bin/.tmp:/usr/local/sbin/.tmp:/usr/sbin/.tmp:/home/vagrant/.local/bin/.tmp:/home/vagrant/bin/.tmp
=================================================================
==19177==ERROR: LeakSanitizer: detected memory leaks
Direct leak of 56 byte(s) in 1 object(s) allocated from:
*0 0x7fd6adf72850 in malloc (/lib64/libasan.so.4+0xde850)
*1 0x7fd6ad2b93d2 in malloc_multiply ../src/basic/alloc-util.h:69
*2 0x7fd6ad2bafd2 in strv_split ../src/basic/strv.c:269
*3 0x7fd6ad42ba67 in search_from_environment ../src/libsystemd/sd-path/sd-path.c:409
*4 0x7fd6ad42bffe in get_search ../src/libsystemd/sd-path/sd-path.c:482
*5 0x7fd6ad42c55b in sd_path_search ../src/libsystemd/sd-path/sd-path.c:607
*6 0x7fd6ad42b3a2 in sd_path_home ../src/libsystemd/sd-path/sd-path.c:348
*7 0x55f59c65ebea in print_home ../src/path/path.c:97
*8 0x55f59c65f157 in main ../src/path/path.c:177
*9 0x7fd6abaea009 in __libc_start_main (/lib64/libc.so.6+0x21009)
Indirect leak of 68 byte(s) in 5 object(s) allocated from:
*0 0x7fd6adf72850 in malloc (/lib64/libasan.so.4+0xde850)
*1 0x7fd6abb5f689 in strndup (/lib64/libc.so.6+0x96689)
Indirect leak of 25 byte(s) in 1 object(s) allocated from:
*0 0x7fd6adf72850 in malloc (/lib64/libasan.so.4+0xde850)
*1 0x7fd6abb5f689 in strndup (/lib64/libc.so.6+0x96689)
*2 0x6c2e2f746e617266 (<unknown module>)
SUMMARY: AddressSanitizer: 149 byte(s) leaked in 7 allocation(s).
This commit is contained in:
committed by
Evgeny Vereshchagin
parent
2955e0d4dc
commit
a605e46f29
@@ -570,7 +570,8 @@ static int get_search(uint64_t type, char ***list) {
|
||||
}
|
||||
|
||||
_public_ int sd_path_search(uint64_t type, const char *suffix, char ***paths) {
|
||||
char **l, **i, **j, **n;
|
||||
char **i, **j;
|
||||
_cleanup_strv_free_ char **l = NULL, **n = NULL;
|
||||
int r;
|
||||
|
||||
assert_return(paths, -EINVAL);
|
||||
@@ -600,7 +601,7 @@ _public_ int sd_path_search(uint64_t type, const char *suffix, char ***paths) {
|
||||
l[0] = p;
|
||||
l[1] = NULL;
|
||||
|
||||
*paths = l;
|
||||
*paths = TAKE_PTR(l);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -609,15 +610,13 @@ _public_ int sd_path_search(uint64_t type, const char *suffix, char ***paths) {
|
||||
return r;
|
||||
|
||||
if (!suffix) {
|
||||
*paths = l;
|
||||
*paths = TAKE_PTR(l);
|
||||
return 0;
|
||||
}
|
||||
|
||||
n = new(char*, strv_length(l)+1);
|
||||
if (!n) {
|
||||
strv_free(l);
|
||||
if (!n)
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
j = n;
|
||||
STRV_FOREACH(i, l) {
|
||||
@@ -627,16 +626,13 @@ _public_ int sd_path_search(uint64_t type, const char *suffix, char ***paths) {
|
||||
else
|
||||
*j = strjoin(*i, "/", suffix);
|
||||
|
||||
if (!*j) {
|
||||
strv_free(l);
|
||||
strv_free(n);
|
||||
if (!*j)
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
j++;
|
||||
}
|
||||
|
||||
*j = NULL;
|
||||
*paths = n;
|
||||
*paths = TAKE_PTR(n);
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user