Skip to content

Commit ebcb946

Browse files
committed
perf env: Do not return pointers to local variables
It is possible to return a pointer to a local variable when looking up the architecture name for the running system and no normalization is done on that value, i.e. we may end up returning the uts.machine local variable. While this doesn't happen on most arches, as normalization takes place, lets fix this by making that a static variable and optimize it a bit by not always running uname(), only the first time. Noticed in fedora rawhide running with: [perfbuilder@a5ff49d6e6e4 ~]$ gcc --version gcc (GCC) 10.0.1 20200216 (Red Hat 10.0.1-0.8) Reported-by: Jiri Olsa <[email protected]> Cc: Adrian Hunter <[email protected]> Cc: Namhyung Kim <[email protected]> Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent cff20b3 commit ebcb946

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

tools/perf/util/env.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,11 +343,11 @@ static const char *normalize_arch(char *arch)
343343

344344
const char *perf_env__arch(struct perf_env *env)
345345
{
346-
struct utsname uts;
347346
char *arch_name;
348347

349348
if (!env || !env->arch) { /* Assume local operation */
350-
if (uname(&uts) < 0)
349+
static struct utsname uts = { .machine[0] = '\0', };
350+
if (uts.machine[0] == '\0' && uname(&uts) < 0)
351351
return NULL;
352352
arch_name = uts.machine;
353353
} else

0 commit comments

Comments
 (0)