From: Mark Wielaard Date: Thu, 30 May 2019 01:40:56 +0000 (+0200) Subject: Fix -Wdiscarded-qualifiers warnings in launcher-linux.c X-Git-Tag: VALGRIND_3_16_0~269 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1364da0fde0d68c84f3450b192588da97d50760b;p=thirdparty%2Fvalgrind.git Fix -Wdiscarded-qualifiers warnings in launcher-linux.c 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. --- diff --git a/coregrind/launcher-linux.c b/coregrind/launcher-linux.c index 208996e6c3..e014d22030 100644 --- a/coregrind/launcher-linux.c +++ b/coregrind/launcher-linux.c @@ -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; }