From: Arnaldo Carvalho de Melo <a...@redhat.com>

[ Upstream commit 7fcdccd4237724931d9773d1e3039bfe053a6f52 ]

When applying a patch by Ian I incorrectly converted to zfree() an
expression that involved testing some other struct member, not the one
being freed, which lead to bugs reproduceable by:

  $ perf stat -e i/bs,tsc,L2/o sleep 1
  WARNING: multiple event parsing errors
  Segmentation fault (core dumped)
  $

Fix it by restoring the test for pos->free_str before freeing
pos->val.str, but continue using zfree(&pos->val.str) to set that member
to NULL after freeing it.

Reported-by: Ian Rogers <irog...@google.com>
Fixes: e8dfb81838b1 ("perf parse-events: Fix memory leaks found on 
parse_events")
Cc: Adrian Hunter <adrian.hun...@intel.com>
Cc: Alexander Shishkin <alexander.shish...@linux.intel.com>
Cc: Andi Kleen <a...@linux.intel.com>
Cc: clang-built-li...@googlegroups.com
Cc: Jiri Olsa <jo...@kernel.org>
Cc: Leo Yan <leo....@linaro.org>
Cc: Mark Rutland <mark.rutl...@arm.com>
Cc: Namhyung Kim <namhy...@kernel.org>
Cc: Peter Zijlstra <pet...@infradead.org>
Cc: Stephane Eranian <eran...@google.com>
Signed-off-by: Arnaldo Carvalho de Melo <a...@redhat.com>
Signed-off-by: Sasha Levin <sas...@kernel.org>
---
 tools/perf/util/parse-events.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c
index 2733cdfdf04c6..ba973bdfaa657 100644
--- a/tools/perf/util/parse-events.c
+++ b/tools/perf/util/parse-events.c
@@ -1258,7 +1258,8 @@ static int __parse_events_add_pmu(struct 
parse_events_state *parse_state,
 
                list_for_each_entry_safe(pos, tmp, &config_terms, list) {
                        list_del_init(&pos->list);
-                       zfree(&pos->val.str);
+                       if (pos->free_str)
+                               zfree(&pos->val.str);
                        free(pos);
                }
                return -EINVAL;
-- 
2.25.1

Reply via email to