summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames Youngman <jay@gnu.org>2015-12-23 23:23:05 +0000
committerJames Youngman <jay@gnu.org>2015-12-23 23:23:05 +0000
commitd286cf67ddbad8f5827310f759f862c3fdfe550e (patch)
tree7699548cce75683dfe96e569434a53af28220b49
parent9ebef301acff81f980bc3d14547c56755b2d4b4f (diff)
downloadfindutils-d286cf67ddbad8f5827310f759f862c3fdfe550e.tar.gz
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.
-rw-r--r--xargs/xargs.c19
1 files changed, 17 insertions, 2 deletions
diff --git a/xargs/xargs.c b/xargs/xargs.c
index bf130e05..5f3e7601 100644
--- a/xargs/xargs.c
+++ b/xargs/xargs.c
@@ -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;
+ }
+ }
}