diff options
author | Lennart Poettering <lennart@poettering.net> | 2011-01-05 16:17:26 +0100 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2011-01-05 16:17:26 +0100 |
commit | c4731d1135d54609e33df1569fefbb0c96824896 (patch) | |
tree | b1060bbb37477225a8f3da63d5936d8992739b24 /src/util.c | |
parent | 022707d96113accf6898b0a59be3a3acde2c6832 (diff) |
util: don't pass invalid fd to fdopendir() on error to avoid corruption of errno
Diffstat (limited to 'src/util.c')
-rw-r--r-- | src/util.c | 13 |
1 files changed, 12 insertions, 1 deletions
diff --git a/src/util.c b/src/util.c index 21afdceb8c..aa1f19ed69 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) { |