[PATCH 21/25] perf evsel: Make perf_evsel__name() accept a NULL argument

From: Arnaldo Carvalho de Melo
Date: Fri Jun 21 2019 - 13:41:34 EST

From: Arnaldo Carvalho de Melo <acme@xxxxxxxxxx>

In which case it simply returns "unknown", like when it can't figure out
the evsel->name value.

This makes this code more robust and fixes a problem in 'perf trace'
where a NULL evsel was being passed to a routine that only used the
evsel for printing its name when a invalid syscall id was passed.

Reported-by: Leo Yan <leo.yan@xxxxxxxxxx>
Cc: Adrian Hunter <adrian.hunter@xxxxxxxxx>
Cc: Jiri Olsa <jolsa@xxxxxxxxxx>
Cc: Namhyung Kim <namhyung@xxxxxxxxxx>
Link: https://lkml.kernel.org/n/tip-f30ztaasku3z935cn3ak3h53@xxxxxxxxxxxxxx
Signed-off-by: Arnaldo Carvalho de Melo <acme@xxxxxxxxxx>
tools/perf/util/evsel.c | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c
index 0f506f10ecf0..04c4ed1573cb 100644
--- a/tools/perf/util/evsel.c
+++ b/tools/perf/util/evsel.c
@@ -589,6 +589,9 @@ const char *perf_evsel__name(struct perf_evsel *evsel)
char bf[128];

+ if (!evsel)
+ goto out_unknown;
if (evsel->name)
return evsel->name;

@@ -628,7 +631,10 @@ const char *perf_evsel__name(struct perf_evsel *evsel)

evsel->name = strdup(bf);

- return evsel->name ?: "unknown";
+ if (evsel->name)
+ return evsel->name;
+ return "unknown";

const char *perf_evsel__group_name(struct perf_evsel *evsel)