From d3922661bac7afb6cd7a05494b9cde526d4d3229 Mon Sep 17 00:00:00 2001 From: Peter Hutterer Date: Tue, 15 Oct 2024 11:52:12 +1000 Subject: [PATCH] util: add xclose(fd) to close-and-reset an fd This prevents accidentally leaving the fd set after closing. And it includes the -1 check so we don't need this everywhere ourselves (not that we use it right now but valgrind likes to complain about this). Part-of: --- src/util-files.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/util-files.h b/src/util-files.h index 15c5ab0d..e980d099 100644 --- a/src/util-files.h +++ b/src/util-files.h @@ -27,6 +27,7 @@ #include #include +#include #include #include "util-strings.h" @@ -52,3 +53,12 @@ mkdir_p(const char *dir) return (rc == -1 && errno != EEXIST) ? -errno : 0; } + +static inline void +xclose(int *fd) +{ + if (*fd != -1) { + close(*fd); + *fd = -1; + } +} -- 2.34.1