From: Lennart Poettering Date: Wed, 5 Jan 2011 15:17:26 +0000 (+0100) Subject: util: don't pass invalid fd to fdopendir() on error to avoid corruption of errno X-Git-Tag: v16~14 X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c4731d1135d54609e33df1569fefbb0c96824896;p=systemd util: don't pass invalid fd to fdopendir() on error to avoid corruption of errno --- diff --git a/src/util.c b/src/util.c index 21afdceb..aa1f19ed 100644 --- a/src/util.c +++ b/src/util.c @@ -3436,7 +3436,18 @@ bool null_or_empty(struct stat *st) { } DIR *xopendirat(int fd, const char *name, int flags) { - return fdopendir(openat(fd, name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|flags)); + int nfd; + DIR *d; + + if ((nfd = openat(fd, name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|flags)) < 0) + return NULL; + + if (!(d = fdopendir(nfd))) { + close_nointr_nofail(nfd); + return NULL; + } + + return d; } int signal_from_string_try_harder(const char *s) {