From e08d3a7932528cf62c9a4335a639e7e638639a1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Sur=C3=BD?= Date: Thu, 15 Aug 2024 09:23:31 +0200 Subject: [PATCH] Check the result of dirfd() before calling unlinkat() Instead of directly using the result of dirfd() in the unlinkat() call, check whether the returned file descriptor is actually valid. That doesn't really change the logic as the unlinkat() would fail with invalid descriptor anyway, but this is cleaner and will report the right error returned directly by dirfd() instead of EBADF from unlinkat(). (cherry picked from commit 59f4fdebc07441a5c7419488990f4b82c6a22eeb) --- lib/isc/log.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/isc/log.c b/lib/isc/log.c index 523fd2d6bb..b18edaa7fe 100644 --- a/lib/isc/log.c +++ b/lib/isc/log.c @@ -1076,8 +1076,10 @@ greatest_version(isc_logfile_t *file, int versions, int *greatestp) { * Remove any backup files that exceed versions. */ if (*digit_end == '\0' && version >= versions) { - int n = unlinkat(dirfd(dir.handle), - dir.entry.name, 0); + int n = dirfd(dir.handle); + if (n >= 0) { + n = unlinkat(n, dir.entry.name, 0); + } if (n < 0) { result = isc_errno_toresult(errno); if (result != ISC_R_SUCCESS && @@ -1223,8 +1225,10 @@ remove_old_tsversions(isc_logfile_t *file, int versions) { * Remove any backup files that exceed versions. */ if (*digit_end == '\0' && version < last) { - int n = unlinkat(dirfd(dir.handle), - dir.entry.name, 0); + int n = dirfd(dir.handle); + if (n >= 0) { + n = unlinkat(n, dir.entry.name, 0); + } if (n < 0) { result = isc_errno_toresult(errno); if (result != ISC_R_SUCCESS &&