From: Kaige Ye Date: Fri, 4 Aug 2023 02:09:08 +0000 (+0800) Subject: perf stat-display: Check if snprintf()'s fmt argument is NULL X-Git-Tag: v6.6.17~3940^2~63 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=58a8d2edd57b35307d81ec6c304697707e6067c1;p=platform%2Fkernel%2Flinux-rpi.git perf stat-display: Check if snprintf()'s fmt argument is NULL It is undefined behavior to pass NULL as snprintf()'s fmt argument. Here is an example to trigger the problem: $ perf stat --metric-only -x, -e instructions -- sleep 1 insn per cycle, Segmentation fault (core dumped) With this patch: $ perf stat --metric-only -x, -e instructions -- sleep 1 insn per cycle, , Reviewed-by: Ian Rogers Signed-off-by: Kaige Ye Cc: Adrian Hunter Cc: Alexander Shishkin Cc: Ingo Molnar Cc: Jiri Olsa Cc: Mark Rutland Cc: Namhyung Kim Cc: Peter Zijlstra Link: https://lore.kernel.org/r/01CA7674B690CA24+20230804020907.144562-2-ye@kaige.org Signed-off-by: Arnaldo Carvalho de Melo --- diff --git a/tools/perf/util/stat-display.c b/tools/perf/util/stat-display.c index d45d5dc..afe6db8 100644 --- a/tools/perf/util/stat-display.c +++ b/tools/perf/util/stat-display.c @@ -578,7 +578,7 @@ static void print_metric_only_csv(struct perf_stat_config *config __maybe_unused if (!valid_only_metric(unit)) return; unit = fixunit(tbuf, os->evsel, unit); - snprintf(buf, sizeof buf, fmt, val); + snprintf(buf, sizeof(buf), fmt ?: "", val); ends = vals = skip_spaces(buf); while (isdigit(*ends) || *ends == '.') ends++; @@ -600,7 +600,7 @@ static void print_metric_only_json(struct perf_stat_config *config __maybe_unuse if (!valid_only_metric(unit)) return; unit = fixunit(tbuf, os->evsel, unit); - snprintf(buf, sizeof(buf), fmt, val); + snprintf(buf, sizeof(buf), fmt ?: "", val); ends = vals = skip_spaces(buf); while (isdigit(*ends) || *ends == '.') ends++;