zebra: check dirfd() result

`dirfd()` can theoretically return an error.  Call it once and check the
result.

clang-SA: technically correct™.  Ain't that the best kind of correct?

Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
This commit is contained in:
David Lamparter 2024-10-16 12:54:45 +02:00
parent 67b0a457ed
commit e6cb1a90f2

View file

@ -378,19 +378,28 @@ void zebra_ns_notify_parse(void)
{ {
struct dirent *dent; struct dirent *dent;
DIR *srcdir = opendir(NS_RUN_DIR); DIR *srcdir = opendir(NS_RUN_DIR);
int srcdirfd;
if (srcdir == NULL) { if (srcdir == NULL) {
flog_err_sys(EC_LIB_SYSTEM_CALL, flog_err_sys(EC_LIB_SYSTEM_CALL,
"NS parsing init: failed to parse %s", NS_RUN_DIR); "NS parsing init: failed to parse %s", NS_RUN_DIR);
return; return;
} }
srcdirfd = dirfd(srcdir);
if (srcdirfd < 0) {
closedir(srcdir);
flog_err_sys(EC_LIB_SYSTEM_CALL, "NS parsing init: failed to parse %s", NS_RUN_DIR);
return;
}
while ((dent = readdir(srcdir)) != NULL) { while ((dent = readdir(srcdir)) != NULL) {
struct stat st; struct stat st;
if (strcmp(dent->d_name, ".") == 0 if (strcmp(dent->d_name, ".") == 0
|| strcmp(dent->d_name, "..") == 0) || strcmp(dent->d_name, "..") == 0)
continue; continue;
if (fstatat(dirfd(srcdir), dent->d_name, &st, 0) < 0) { if (fstatat(srcdirfd, dent->d_name, &st, 0) < 0) {
flog_err_sys( flog_err_sys(
EC_LIB_SYSTEM_CALL, EC_LIB_SYSTEM_CALL,
"NS parsing init: failed to parse entry %s", "NS parsing init: failed to parse entry %s",