]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
kresc: fix coverity warning 155938
authorVladimír Čunát <vladimir.cunat@nic.cz>
Mon, 6 Feb 2017 10:41:19 +0000 (11:41 +0100)
committerVladimír Čunát <vladimir.cunat@nic.cz>
Mon, 6 Feb 2017 17:44:28 +0000 (18:44 +0100)
The FD leak happened only when we exit the process, but still...

daemon/kresc.c

index ecac2d1a0a4bec95346edbd1f685a95a953e87b1..72a6d172942831e381a9c21bfab9e80b947e4bf1 100644 (file)
@@ -28,6 +28,7 @@
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <sys/un.h>
+#include <unistd.h>
 
 #define HISTORY_FILE "kresc_history"
 #define PROGRAM_NAME "kresc"
@@ -275,23 +276,28 @@ static int init_tty(const char *path)
        size_t plen = strlen(path);
        if (plen + 1 > sizeof(addr.sun_path)) {
                fprintf(stderr, "Path too long\n");
+               close(fd);
                return 1;
        }
        memcpy(addr.sun_path, path, plen + 1);
        if (connect(fd, (const struct sockaddr *)&addr, sizeof(addr))) {
                perror("While connecting to daemon");
+               close(fd);
                return 1;
        }
 
        g_tty = fdopen(fd, "r+");
        if (!g_tty) {
                perror("While opening TTY");
+               close(fd);
                return 1;
        }
        // Switch to binary mode and consume the text "> ".
        if (fprintf(g_tty, "__binary\n") < 0 || !fread(&addr, 2, 1, g_tty)
            || fflush(g_tty)) {
                perror("While initializing TTY");
+               fclose(g_tty);
+               g_tty = NULL;
                return 1;
        }