Re: [PATCH v2 1/2] perf: Suggest inbuilt commands for unknown command
From: Ian Rogers
Date: Wed Mar 20 2024 - 11:26:34 EST
On Wed, Mar 20, 2024 at 8:20 AM Arnaldo Carvalho de Melo
<acme@xxxxxxxxxx> wrote:
>
> On Wed, Mar 20, 2024 at 08:12:56AM -0700, Ian Rogers wrote:
> > Ah, the change:
> >
> > - static int done_help;
> > + int done_help;
> >
> > created an uninitialized use. Compiler warning/sanitizers? Anyway,
> > done_help needs hoisting out of the loop and initializing to zero, or
> > being made static again (ugh).
>
> ⬢[acme@toolbox perf-tools-next]$ git diff
> diff --git a/tools/perf/perf.c b/tools/perf/perf.c
> index c719e6ccd9e27778..f9532b20e87cbf05 100644
> --- a/tools/perf/perf.c
> +++ b/tools/perf/perf.c
> @@ -459,7 +459,7 @@ static int libperf_print(enum libperf_print_level level,
>
> int main(int argc, const char **argv)
> {
> - int err;
> + int err, done_help = 0;
> const char *cmd;
> char sbuf[STRERR_BUFSIZE];
>
> @@ -558,8 +558,6 @@ int main(int argc, const char **argv)
> pthread__block_sigwinch();
>
> while (1) {
> - int done_help;
> -
> run_argv(&argc, &argv);
>
> if (errno != ENOENT)
> ⬢[acme@toolbox perf-tools-next]$ perf raccord
> Fatal: Out of memory, realloc failed
> ⬢[acme@toolbox perf-tools-next]$
>
> Then:
>
> diff --git a/tools/perf/perf.c b/tools/perf/perf.c
> index c719e6ccd9e27778..54f62aa6612bc290 100644
> --- a/tools/perf/perf.c
> +++ b/tools/perf/perf.c
> @@ -558,7 +558,7 @@ int main(int argc, const char **argv)
> pthread__block_sigwinch();
>
> while (1) {
> - int done_help;
> + static int done_help;
>
> run_argv(&argc, &argv);
>
> ⬢[acme@toolbox perf-tools-next]$ perf raccord
> Fatal: Out of memory, realloc failed
> ⬢[acme@toolbox perf-tools-next]$
Similar issue on main_cmds, caught by memory sanitizer. I think the
following fixes it:
```
diff --git a/tools/perf/perf.c b/tools/perf/perf.c
index c719e6ccd9e2..bd3f80b5bb46 100644
--- a/tools/perf/perf.c
+++ b/tools/perf/perf.c
@@ -459,7 +459,7 @@ static int libperf_print(enum libperf_print_level level,
int main(int argc, const char **argv)
{
- int err;
+ int err, done_help = 0;
const char *cmd;
char sbuf[STRERR_BUFSIZE];
@@ -558,15 +558,13 @@ int main(int argc, const char **argv)
pthread__block_sigwinch();
while (1) {
- int done_help;
-
run_argv(&argc, &argv);
if (errno != ENOENT)
break;
if (!done_help) {
- struct cmdnames main_cmds;
+ struct cmdnames main_cmds = {};
for (unsigned int i = 0; i < ARRAY_SIZE(commands); i++) {
add_cmdname(&main_cmds,
```
Thanks,
Ian
> - Arnaldo