From dca084e703ce54c500e4af7ddc1e0c2b65d4d403 Mon Sep 17 00:00:00 2001 From: Nicholas Marriott Date: Wed, 10 Jun 2015 15:39:23 +0100 Subject: [PATCH] Don't leak dotfd if fchdir fails. From ettl dot martin78 at gmail dot com. --- compat/openat.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/compat/openat.c b/compat/openat.c index 5cd9e551..6b04eedc 100644 --- a/compat/openat.c +++ b/compat/openat.c @@ -40,8 +40,12 @@ openat(int fd, const char *path, int flags, ...) dotfd = open(".", O_RDONLY); if (dotfd == -1) return (-1); - if (fchdir(fd) != 0) + if (fchdir(fd) != 0) { + saved_errno = errno; + close(dotfd); + errno = saved_errno; return (-1); + } } retval = open(path, flags, mode);