perf stat: Avoid metric-only segv
authorIan Rogers <irogers@google.com>
Fri, 9 Feb 2024 20:49:46 +0000 (12:49 -0800)
committerNamhyung Kim <namhyung@kernel.org>
Tue, 13 Feb 2024 21:48:09 +0000 (13:48 -0800)
Cycles is recognized as part of a hard coded metric in stat-shadow.c,
it may call print_metric_only with a NULL fmt string leading to a
segfault. Handle the NULL fmt explicitly.

Fixes: 088519f318be ("perf stat: Move the display functions to stat-display.c")
Signed-off-by: Ian Rogers <irogers@google.com>
Reviewed-by: Kan Liang <kan.liang@linux.intel.com>
Cc: K Prateek Nayak <kprateek.nayak@amd.com>
Cc: James Clark <james.clark@arm.com>
Cc: Kaige Ye <ye@kaige.org>
Cc: John Garry <john.g.garry@oracle.com>
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Link: https://lore.kernel.org/r/20240209204947.3873294-4-irogers@google.com
tools/perf/util/stat-display.c

index 4dfe7d9517a9691a275693eec48649655faf4329..ae37395f90c0b80e315cfdea999e31ba4702b4cc 100644 (file)
@@ -571,7 +571,7 @@ static void print_metric_only(struct perf_stat_config *config,
        if (color)
                mlen += strlen(color) + sizeof(PERF_COLOR_RESET) - 1;
 
-       color_snprintf(str, sizeof(str), color ?: "", fmt, val);
+       color_snprintf(str, sizeof(str), color ?: "", fmt ?: "", val);
        fprintf(out, "%*s ", mlen, str);
        os->first = false;
 }