Commit 58a8d2ed authored by Kaige Ye's avatar Kaige Ye Committed by Arnaldo Carvalho de Melo

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: default avatarIan Rogers <irogers@google.com>
Signed-off-by: default avatarKaige Ye <ye@kaige.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: https://lore.kernel.org/r/01CA7674B690CA24+20230804020907.144562-2-ye@kaige.orgSigned-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
parent 7d964231
...@@ -578,7 +578,7 @@ static void print_metric_only_csv(struct perf_stat_config *config __maybe_unused ...@@ -578,7 +578,7 @@ static void print_metric_only_csv(struct perf_stat_config *config __maybe_unused
if (!valid_only_metric(unit)) if (!valid_only_metric(unit))
return; return;
unit = fixunit(tbuf, os->evsel, unit); unit = fixunit(tbuf, os->evsel, unit);
snprintf(buf, sizeof buf, fmt, val); snprintf(buf, sizeof(buf), fmt ?: "", val);
ends = vals = skip_spaces(buf); ends = vals = skip_spaces(buf);
while (isdigit(*ends) || *ends == '.') while (isdigit(*ends) || *ends == '.')
ends++; ends++;
...@@ -600,7 +600,7 @@ static void print_metric_only_json(struct perf_stat_config *config __maybe_unuse ...@@ -600,7 +600,7 @@ static void print_metric_only_json(struct perf_stat_config *config __maybe_unuse
if (!valid_only_metric(unit)) if (!valid_only_metric(unit))
return; return;
unit = fixunit(tbuf, os->evsel, unit); unit = fixunit(tbuf, os->evsel, unit);
snprintf(buf, sizeof(buf), fmt, val); snprintf(buf, sizeof(buf), fmt ?: "", val);
ends = vals = skip_spaces(buf); ends = vals = skip_spaces(buf);
while (isdigit(*ends) || *ends == '.') while (isdigit(*ends) || *ends == '.')
ends++; ends++;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment