[v1,3/4] perf stat: Avoid metric-only segv

Message ID 20240209204947.3873294-4-irogers@google.com
State New
Headers
Series Fixes/improvements to metric output |

Commit Message

Ian Rogers Feb. 9, 2024, 8:49 p.m. UTC
  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>
---
Note, the fixes tag is to a refactor that moved the function. The bug
existed before this.
---
 tools/perf/util/stat-display.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
  

Comments

Namhyung Kim Feb. 13, 2024, 9:45 p.m. UTC | #1
On Fri, Feb 9, 2024 at 12:50 PM Ian Rogers <irogers@google.com> wrote:
>
> 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>
> ---
> Note, the fixes tag is to a refactor that moved the function. The bug
> existed before this.

Yeah I noticed that.

> ---
>  tools/perf/util/stat-display.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/tools/perf/util/stat-display.c b/tools/perf/util/stat-display.c
> index 8c61f8627ebc..b7d00a538d70 100644
> --- a/tools/perf/util/stat-display.c
> +++ b/tools/perf/util/stat-display.c
> @@ -560,7 +560,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);

I was thinking about fixing the callers to pass valid format strings
but it seems they don't care about the output string anyway so
I think it's fine.

Thanks,
Namhyung


>         fprintf(out, "%*s ", mlen, str);
>         os->first = false;
>  }
> --
> 2.43.0.687.g38aa6559b0-goog
>
  

Patch

diff --git a/tools/perf/util/stat-display.c b/tools/perf/util/stat-display.c
index 8c61f8627ebc..b7d00a538d70 100644
--- a/tools/perf/util/stat-display.c
+++ b/tools/perf/util/stat-display.c
@@ -560,7 +560,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;
 }