libselinux: free empty scandir(3) result

In case scandir(3) finds no entries still free the returned result to
avoid leaking it.

Also do not override errno in case of a failure.

Reported.by: Cppcheck

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
Acked-by: James Carter <jwcart2@gmail.com>
This commit is contained in:
Christian Göttsche 2024-04-29 18:38:59 +02:00 committed by James Carter
parent 9ef1a83563
commit c8b1f59282
1 changed files with 5 additions and 1 deletions

View File

@ -53,7 +53,11 @@ int security_get_boolean_names(char ***names, int *len)
snprintf(path, sizeof path, "%s%s", selinux_mnt, SELINUX_BOOL_DIR); snprintf(path, sizeof path, "%s%s", selinux_mnt, SELINUX_BOOL_DIR);
*len = scandir(path, &namelist, &filename_select, alphasort); *len = scandir(path, &namelist, &filename_select, alphasort);
if (*len <= 0) { if (*len < 0) {
return -1;
}
if (*len == 0) {
free(namelist);
errno = ENOENT; errno = ENOENT;
return -1; return -1;
} }