lib:replace: Do not leak the file pointer in rep_getprogname()
authorAndreas Schneider <asn@samba.org>
Mon, 12 Nov 2018 14:31:09 +0000 (15:31 +0100)
committerAndreas Schneider <asn@cryptomilk.org>
Tue, 13 Nov 2018 06:37:25 +0000 (07:37 +0100)
And return NULL on error.

Signed-off-by: Andreas Schneider <asn@samba.org>
Reviewed-by: Ralph Boehme <slow@samba.org>
lib/replace/replace.c

index e38df98..9b1df3d 100644 (file)
@@ -996,6 +996,7 @@ const char *rep_getprogname(void)
        pid_t pid;
        size_t nread;
        int len;
+       int rc;
 
        if (rep_progname[0] != '\0') {
                return rep_progname;
@@ -1003,12 +1004,12 @@ const char *rep_getprogname(void)
 
        len = snprintf(rep_progname, sizeof(rep_progname), "%s", "<unknown>");
        if (len <= 0) {
-               return "<unknown>";
+               return NULL;
        }
 
        pid = getpid();
        if (pid <= 1 || pid == (pid_t)-1) {
-               return rep_progname;
+               return NULL;
        }
 
        len = snprintf(cmdline,
@@ -1016,17 +1017,23 @@ const char *rep_getprogname(void)
                       "/proc/%u/cmdline",
                       (unsigned int)pid);
        if (len <= 0 || len == sizeof(cmdline)) {
-               return rep_progname;
+               return NULL;
        }
 
        fp = fopen(cmdline, "r");
        if (fp == NULL) {
-               return rep_progname;
+               return NULL;
        }
 
        nread = fread(cmdline, 1, sizeof(cmdline) - 1, fp);
+
+       rc = fclose(fp);
+       if (rc != 0) {
+               return NULL;
+       }
+
        if (nread == 0) {
-               return rep_progname;
+               return NULL;
        }
 
        cmdline[nread] = '\0';