]> git.ipfire.org Git - thirdparty/valgrind.git/commitdiff
Fix -Wdiscarded-qualifiers warnings in launcher-linux.c
authorMark Wielaard <mark@klomp.org>
Thu, 30 May 2019 01:40:56 +0000 (03:40 +0200)
committerMark Wielaard <mark@klomp.org>
Thu, 30 May 2019 01:40:56 +0000 (03:40 +0200)
commit f15bee "Fix memory leak in launcher-linux.c" introduced some
warnings about passing const pointers to free.

    warning: passing argument 1 of ‘free’ discards ‘const’ qualifier from
    pointer target type [-Wdiscarded-qualifiers]

This was because that code was a little too "smart". The compiler cannot
know that we really only call free () when the pointer was dynamically
allocated. Simplify the code a little to just always allocate a new
string in find_client and always free that string in select_platform.

coregrind/launcher-linux.c

index 208996e6c35bf3067834fca34b5db9f0a7afad6f..e014d220300a55ea2fa2204497e71d2c57a0fa11 100644 (file)
@@ -89,7 +89,7 @@ static void barf ( const char *format, ... )
 }
 
 /* Search the path for the client program */
-static const char *find_client(const char *clientname)
+static char *find_client(const char *clientname)
 {
    char *fullname;
    const char *path = getenv("PATH");
@@ -97,7 +97,7 @@ static const char *find_client(const char *clientname)
 
    assert(clientname != NULL);
 
-   if (path == NULL) return clientname;
+   if (path == NULL) return strdup(clientname);
 
    /* Make the size of the FULLNAME buffer large enough. */
    unsigned need = strlen(path) + strlen("/") + strlen(clientname) + 1;
@@ -128,7 +128,7 @@ static const char *find_client(const char *clientname)
    }
    free(fullname);
 
-   return clientname;
+   return strdup(clientname);
 }
 
 /* Examine the client and work out which platform it is for */
@@ -142,20 +142,21 @@ static const char *select_platform(const char *clientname)
    } header;
    ssize_t n_bytes;
    const char *platform = NULL;
-   const char *client = clientname;
+   char *client;
 
    VG_(debugLog)(2, "launcher", "selecting platform for '%s'\n", clientname);
 
    if (strchr(clientname, '/') == NULL)
       client = find_client(clientname);
+   else
+      client = strdup(clientname);
 
-   if (client != clientname)
+   if (strcmp (client, clientname) != 0)
       VG_(debugLog)(2, "launcher", "selecting platform for '%s'\n", client);
 
    if ((fd = open(clientname, O_RDONLY)) < 0) {
      return_null:
-      if (client != clientname)
-         free (client);
+      free (client);
       return NULL;
    }
    //   barf("open(%s): %s", clientname, strerror(errno));
@@ -326,8 +327,7 @@ static const char *select_platform(const char *clientname)
    VG_(debugLog)(2, "launcher", "selected platform '%s'\n",
                  platform ? platform : "unknown");
 
-   if (client != clientname)
-      free (client);
+   free (client);
 
    return platform;
 }