]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib/fileutils: close fd if fdopen is failed
authorMasatake YAMATO <yamato@redhat.com>
Thu, 6 May 2021 04:39:30 +0000 (13:39 +0900)
committerKarel Zak <kzak@redhat.com>
Thu, 6 May 2021 10:20:14 +0000 (12:20 +0200)
Signed-off-by: Masatake YAMATO <yamato@redhat.com>
include/fileutils.h

index 10fe46a25db012e00577f9b9f5e305f31db2c953..ad54b952cb4834bdb3a008cb96125dff9e42c2de 100644 (file)
@@ -34,10 +34,15 @@ static inline FILE *fopen_at(int dir, const char *filename,
                              int flags, const char *mode)
 {
        int fd = openat(dir, filename, flags);
+       FILE *ret;
+
        if (fd < 0)
                return NULL;
 
-       return fdopen(fd, mode);
+       ret = fdopen(fd, mode);
+       if (!ret)
+               close(fd);
+       return ret;
 }
 #endif