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: <https://gitlab.freedesktop.org/libinput/libinput/-/merge_requests/1067>
This commit is contained in:
Peter Hutterer 2024-10-15 11:52:12 +10:00 committed by Marge Bot
parent a61c876412
commit d3922661ba

View file

@ -27,6 +27,7 @@
#include <errno.h>
#include <libgen.h>
#include <unistd.h>
#include <sys/stat.h>
#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;
}
}