[PATCH 5.19 157/365] perf parse-events: Fix segfault when event parser gets an error

From: Greg Kroah-Hartman
Date: Tue Aug 23 2022 - 04:40:21 EST


From: Adrian Hunter <adrian.hunter@xxxxxxxxx>

commit 2e828582b81f5bc76a4fe8e7812df259ab208302 upstream.

parse_events() is often called with parse_events_error set to NULL.
Make parse_events_error__handle() not segfault in that case.

A subsequent patch changes to avoid passing NULL in the first place.

Fixes: 43eb05d066795bdf ("perf tests: Support 'Track with sched_switch' test for hybrid")
Signed-off-by: Adrian Hunter <adrian.hunter@xxxxxxxxx>
Cc: Ian Rogers <irogers@xxxxxxxxxx>
Cc: Jin Yao <yao.jin@xxxxxxxxxxxxxxx>
Cc: Jiri Olsa <jolsa@xxxxxxxxxx>
Cc: Namhyung Kim <namhyung@xxxxxxxxxx>
Link: https://lore.kernel.org/r/20220809080702.6921-2-adrian.hunter@xxxxxxxxx
Signed-off-by: Arnaldo Carvalho de Melo <acme@xxxxxxxxxx>
Signed-off-by: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx>
---
tools/perf/util/parse-events.c | 14 +++++++++++---
1 file changed, 11 insertions(+), 3 deletions(-)

--- a/tools/perf/util/parse-events.c
+++ b/tools/perf/util/parse-events.c
@@ -2391,9 +2391,12 @@ void parse_events_error__exit(struct par
void parse_events_error__handle(struct parse_events_error *err, int idx,
char *str, char *help)
{
- if (WARN(!str, "WARNING: failed to provide error string\n")) {
- free(help);
- return;
+ if (WARN(!str, "WARNING: failed to provide error string\n"))
+ goto out_free;
+ if (!err) {
+ /* Assume caller does not want message printed */
+ pr_debug("event syntax error: %s\n", str);
+ goto out_free;
}
switch (err->num_errors) {
case 0:
@@ -2419,6 +2422,11 @@ void parse_events_error__handle(struct p
break;
}
err->num_errors++;
+ return;
+
+out_free:
+ free(str);
+ free(help);
}

#define MAX_WIDTH 1000