util: don't pass invalid fd to fdopendir() on error to avoid corruption of errno

This commit is contained in:
Lennart Poettering 2011-01-05 16:17:26 +01:00
parent 022707d961
commit c4731d1135
1 changed files with 12 additions and 1 deletions

View File

@ -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) {