lib/fileutils: close fd if fdopen is failed

Signed-off-by: Masatake YAMATO <yamato@redhat.com>
This commit is contained in:
Masatake YAMATO 2021-05-06 13:39:30 +09:00 committed by Karel Zak
parent f6c1168925
commit c8d931ebe2
1 changed files with 6 additions and 1 deletions

View 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