Subject: [PATCH 06/25] perf map: Fix potential NULL pointer dereference found by smatch tool

From: Arnaldo Carvalho de Melo <[email-masked]>
Date: Tue, 9 Jul 2019 15:31:07 -0300

From: Leo Yan <[email-masked]>

Based on the following report from Smatch, fix the potential NULL
pointer dereference check.

  tools/perf/util/map.c:479
  map__fprintf_srccode() error: we previously assumed 'state' could be
  null (see line 466)

  tools/perf/util/map.c
  465         /* Avoid redundant printing */
  466         if (state &&
  467             state->srcfile &&
  468             !strcmp(state->srcfile, srcfile) &&
  469             state->line == line) {
  470                 free(srcfile);
  471                 return 0;
  472         }
  473
  474         srccode = find_sourceline(srcfile, line, &len);
  475         if (!srccode)
  476                 goto out_free_line;
  477
  478         ret = fprintf(fp, "|%-8d %.*s", line, len, srccode);
  479         state->srcfile = srcfile;
              ^^^^^^^
  480         state->line = line;
              ^^^^^^^

This patch validates 'state' pointer before access its elements.

Signed-off-by: Leo Yan <[email-masked]>
Acked-by: Jiri Olsa <[email-masked]>
Cc: Adrian Hunter <[email-masked]>
Cc: Alexander Shishkin <[email-masked]>
Cc: Alexey Budankov <[email-masked]>
Cc: Alexios Zavras <[email-masked]>
Cc: Andi Kleen <[email-masked]>
Cc: Changbin Du <[email-masked]>
Cc: David S. Miller <[email-masked]>
Cc: Davidlohr Bueso <[email-masked]>
Cc: Eric Saint-Etienne <[email-masked]>
Cc: Jin Yao <[email-masked]>
Cc: Konstantin Khlebnikov <[email-masked]>
Cc: Mathieu Poirier <[email-masked]>
Cc: Namhyung Kim <[email-masked]>
Cc: Peter Zijlstra <[email-masked]>
Cc: Rasmus Villemoes <[email-masked]>
Cc: Song Liu <[email-masked]>
Cc: Suzuki Poulouse <[email-masked]>
Cc: Thomas Gleixner <[email-masked]>
Cc: Thomas Richter <[email-masked]>
Cc: [email-masked]
Fixes: dd2e18e9ac20 ("perf tools: Support 'srccode' output")
Link: http://lkml.kernel.org/r/[email-masked]
Signed-off-by: Arnaldo Carvalho de Melo <[email-masked]>
---
 tools/perf/util/map.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/tools/perf/util/map.c b/tools/perf/util/map.c
index 6fce983c6115..5f87975d2562 100644
--- a/tools/perf/util/map.c
+++ b/tools/perf/util/map.c
@@ -476,8 +476,11 @@ int map__fprintf_srccode(struct map *map, u64 addr,
 		goto out_free_line;
 
 	ret = fprintf(fp, "|%-8d %.*s", line, len, srccode);
-	state->srcfile = srcfile;
-	state->line = line;
+
+	if (state) {
+		state->srcfile = srcfile;
+		state->line = line;
+	}
 	return ret;
 
 out_free_line:
-- 
2.21.0

_______________________________________________
linux-arm-kernel mailing list
[email-masked]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel