]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Fix leaked file descriptor
authorAndreas Huber <andreas.huber@deltaww.com>
Thu, 7 Dec 2017 13:35:43 +0000 (14:35 +0100)
committerJoel Rosdahl <joel@rosdahl.net>
Thu, 11 Jan 2018 20:02:33 +0000 (21:02 +0100)
ccache.c
ccache.h
util.c

index 19df19502dd59ac2d38ac986d4edfd134ee89d87..fcd0f215476a387e3dbf254642228b9ea6a684f5 100644 (file)
--- a/ccache.c
+++ b/ccache.c
@@ -3137,6 +3137,7 @@ static void
 setup_uncached_err(void)
 {
        int uncached_fd = dup(2);
+       set_cloexec_flag(uncached_fd);
        if (uncached_fd == -1) {
                cc_log("dup(2) failed: %s", strerror(errno));
                failed();
index 413da7f3218817bc2630e1291132ef6ff5a56f57..2a0a095e61fbb5dda3f6f06b725c7c80ad7382f5 100644 (file)
--- a/ccache.h
+++ b/ccache.h
@@ -184,6 +184,7 @@ char *x_readlink(const char *path);
 bool read_file(const char *path, size_t size_hint, char **data, size_t *size);
 char *read_text_file(const char *path, size_t size_hint);
 char *subst_env_in_string(const char *str, char **errmsg);
+void set_cloexec_flag (int fd);
 
 // ----------------------------------------------------------------------------
 // stats.c
diff --git a/util.c b/util.c
index 8b4783a0d09ff7a3910b0d544dadb343de5fbb82..9d3bba8c8d46712ea0faef8a61ee8080c8060a33 100644 (file)
--- a/util.c
+++ b/util.c
@@ -51,10 +51,7 @@ init_log(void)
        if (logfile) {
 #ifndef _WIN32
                int fd = fileno(logfile);
-               int flags = fcntl(fd, F_GETFD, 0);
-               if (flags >= 0) {
-                       fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
-               }
+                set_cloexec_flag(fd);
 #endif
                return true;
        } else {
@@ -1187,6 +1184,7 @@ create_tmp_fd(char **fname)
                fatal("Failed to create temporary file for %s: %s",
                      *fname, strerror(errno));
        }
+       set_cloexec_flag(fd);
 
 #ifndef _WIN32
        fchmod(fd, 0666 & ~get_umask());
@@ -1662,3 +1660,15 @@ subst_env_in_string(const char *str, char **errmsg)
        reformat(&result, "%s%.*s", result, (int)(q - p), p);
        return result;
 }
+
+void
+set_cloexec_flag (int fd)
+{
+#ifndef _WIN32
+       int flags = fcntl(fd, F_GETFD, 0);
+       if (flags >= 0) {
+               fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
+       }
+#endif
+}
+