Avoid an fd leak in fopen_cloexec_for_read_only.

* xargs/xargs.c (fopen_cloexec_for_read_only): when fdopen fails,
close the file descriptor instead of leaking it.  Also, use GNU-style
brace positioning. Both problems were noticed by Paul Eggert.
This commit is contained in:
James Youngman 2015-12-23 23:23:05 +00:00
parent 9ebef301ac
commit d286cf67dd

View File

@ -367,9 +367,24 @@ smaller_of (size_t a, size_t b)
}
static FILE* fopen_cloexec_for_read_only (const char *file_name) {
static FILE* fopen_cloexec_for_read_only (const char *file_name)
{
int fd = open_cloexec (file_name, O_RDONLY);
return (fd < 0) ? NULL : fdopen (fd, "r");
if (fd < 0)
{
return NULL;
}
else
{
FILE *result = fdopen (fd, "r");
if (!result)
{
int saved_errno = errno;
close (fd);
errno = saved_errno;
return NULL;
}
}
}