]> git.ipfire.org Git - thirdparty/valgrind.git/commitdiff
Fix memory leak in launcher-linux.c
authorMark Wielaard <mark@klomp.org>
Sat, 25 May 2019 14:31:02 +0000 (16:31 +0200)
committerMark Wielaard <mark@klomp.org>
Sat, 25 May 2019 14:31:02 +0000 (16:31 +0200)
When the clientname argument is not a full absolute path we reconstruct
the full path using $PATH. This reconstructed full path would be leaked.
This is small and insignificant. But valgrind should give a good example.
So free the path again when we are done and allocated the memory.
Also don't print the path twice in the debug output if we didn't need to
construct a new different path for it.

coregrind/launcher-linux.c

index 105d023a545ba5d3c7b2e80fcb5ba97b24292ad9..954a3fd8c7389445fb9d14ef78f8c6dbf2dc6446 100644 (file)
@@ -144,28 +144,34 @@ static const char *select_platform(const char *clientname)
    } header;
    ssize_t n_bytes;
    const char *platform = NULL;
+   const char *client = clientname;
 
    VG_(debugLog)(2, "launcher", "selecting platform for '%s'\n", clientname);
 
    if (strchr(clientname, '/') == NULL)
-      clientname = find_client(clientname);
+      client = find_client(clientname);
 
-   VG_(debugLog)(2, "launcher", "selecting platform for '%s'\n", clientname);
+   if (client != clientname)
+      VG_(debugLog)(2, "launcher", "selecting platform for '%s'\n", client);
 
-   if ((fd = open(clientname, O_RDONLY)) < 0)
+   if ((fd = open(clientname, O_RDONLY)) < 0) {
+     return_null:
+      if (client != clientname)
+         free (client);
       return NULL;
+   }
    //   barf("open(%s): %s", clientname, strerror(errno));
 
-   VG_(debugLog)(2, "launcher", "opened '%s'\n", clientname);
+   VG_(debugLog)(2, "launcher", "opened '%s'\n", client);
 
    n_bytes = read(fd, header.c, sizeof(header));
    close(fd);
    if (n_bytes < 2) {
-      return NULL;
+      goto return_null;
    }
 
    VG_(debugLog)(2, "launcher", "read %ld bytes from '%s'\n",
-                    (long int)n_bytes, clientname);
+                    (long int)n_bytes, client);
 
    if (header.c[0] == '#' && header.c[1] == '!') {
       int i = 2;
@@ -322,6 +328,9 @@ static const char *select_platform(const char *clientname)
    VG_(debugLog)(2, "launcher", "selected platform '%s'\n",
                  platform ? platform : "unknown");
 
+   if (client != clientname)
+      free (client);
+
    return platform;
 }