libsubcmd: Don't free the usage string
authorAditya Gupta <adityag@linux.ibm.com>
Wed, 4 Sep 2024 06:18:30 +0000 (11:48 +0530)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Wed, 4 Sep 2024 12:54:24 +0000 (09:54 -0300)
Currently, commands which depend on 'parse_options_subcommand()' don't
show the usage string, and instead show '(null)'

    $ ./perf sched
Usage: (null)

    -D, --dump-raw-trace  dump raw trace in ASCII
    -f, --force           don't complain, do it
    -i, --input <file>    input file name
    -v, --verbose         be more verbose (show symbol address, etc)

'parse_options_subcommand()' is generally expected to initialise the usage
string, with information in the passed 'subcommands[]' array

This behaviour was changed in:

  230a7a71f92212e7 ("libsubcmd: Fix parse-options memory leak")

Where the generated usage string is deallocated, and usage[0] string is
reassigned as NULL.

As discussed in [1], free the allocated usage string in the main
function itself, and don't reset usage string to NULL in
parse_options_subcommand

With this change, the behaviour is restored.

    $ ./perf sched
        Usage: perf sched [<options>] {record|latency|map|replay|script|timehist}

           -D, --dump-raw-trace  dump raw trace in ASCII
           -f, --force           don't complain, do it
           -i, --input <file>    input file name
           -v, --verbose         be more verbose (show symbol address, etc)

[1]: https://lore.kernel.org/linux-perf-users/htq5vhx6piet4nuq2mmhk7fs2bhfykv52dbppwxmo3s7du2odf@styd27tioc6e/

Fixes: 230a7a71f92212e7 ("libsubcmd: Fix parse-options memory leak")
Suggested-by: Namhyung Kim <namhyung@kernel.org>
Signed-off-by: Aditya Gupta <adityag@linux.ibm.com>
Acked-by: Namhyung Kim <namhyung@kernel.org>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Athira Rajeev <atrajeev@linux.vnet.ibm.com>
Cc: Disha Goel <disgoel@linux.vnet.ibm.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kajol Jain <kjain@linux.ibm.com>
Cc: Madhavan Srinivasan <maddy@linux.ibm.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Link: https://lore.kernel.org/r/20240904061836.55873-2-adityag@linux.ibm.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/lib/subcmd/parse-options.c
tools/perf/builtin-kmem.c
tools/perf/builtin-kvm.c
tools/perf/builtin-kwork.c
tools/perf/builtin-lock.c
tools/perf/builtin-mem.c
tools/perf/builtin-sched.c

index 4b60ec03b0bb995a67a5ab5a286627b4d8c4a96d..eb896d30545b6391f0cffd9dd04c194f0379bd28 100644 (file)
@@ -633,10 +633,11 @@ int parse_options_subcommand(int argc, const char **argv, const struct option *o
                        const char *const subcommands[], const char *usagestr[], int flags)
 {
        struct parse_opt_ctx_t ctx;
-       char *buf = NULL;
 
        /* build usage string if it's not provided */
        if (subcommands && !usagestr[0]) {
+               char *buf = NULL;
+
                astrcatf(&buf, "%s %s [<options>] {", subcmd_config.exec_name, argv[0]);
 
                for (int i = 0; subcommands[i]; i++) {
@@ -678,10 +679,7 @@ int parse_options_subcommand(int argc, const char **argv, const struct option *o
                        astrcatf(&error_buf, "unknown switch `%c'", *ctx.opt);
                usage_with_options(usagestr, options);
        }
-       if (buf) {
-               usagestr[0] = NULL;
-               free(buf);
-       }
+
        return parse_options_end(&ctx);
 }
 
index b3cbac40b8c727722d66e608133f335817cf0c29..a756147e2eec7a3820e1953db346fafa8fe687ba 100644 (file)
@@ -2057,6 +2057,8 @@ int cmd_kmem(int argc, const char **argv)
 
 out_delete:
        perf_session__delete(session);
+       /* free usage string allocated by parse_options_subcommand */
+       free((void *)kmem_usage[0]);
 
        return ret;
 }
index 692267b1b7e8311f3d8587c332a9d9a1442c16fe..55ea17c5ff02acf7d17e8ffd5e968e84c50afb1d 100644 (file)
@@ -2184,5 +2184,8 @@ int cmd_kvm(int argc, const char **argv)
        else
                usage_with_options(kvm_usage, kvm_options);
 
+       /* free usage string allocated by parse_options_subcommand */
+       free((void *)kvm_usage[0]);
+
        return 0;
 }
index 6a4281b8fd1071c438bf5e4b0de52e1efb5840e0..c1daf82c9b923be817a410f9ceffd3e44cc9a77d 100644 (file)
@@ -2519,5 +2519,8 @@ int cmd_kwork(int argc, const char **argv)
        } else
                usage_with_options(kwork_usage, kwork_options);
 
+       /* free usage string allocated by parse_options_subcommand */
+       free((void *)kwork_usage[0]);
+
        return 0;
 }
index 2c216427e929e2c31b1d3c962b69228b62cd164c..062e2b56a2ab570ead5e9da4bba99e29b18d3fde 100644 (file)
@@ -2712,6 +2712,9 @@ int cmd_lock(int argc, const char **argv)
                usage_with_options(lock_usage, lock_options);
        }
 
+       /* free usage string allocated by parse_options_subcommand */
+       free((void *)lock_usage[0]);
+
        zfree(&lockhash_table);
        return rc;
 }
index 18e5f9a0ddc2213931b00f0436b4c5d039951459..bb38bb5a1c26477a4fd248267e2318c9b814cad9 100644 (file)
@@ -546,5 +546,8 @@ int cmd_mem(int argc, const char **argv)
        else
                usage_with_options(mem_usage, mem_options);
 
+       /* free usage string allocated by parse_options_subcommand */
+       free((void *)mem_usage[0]);
+
        return 0;
 }
index 0a7b2b2acd5697926bb1039316892c1415af7c29..5981cc51abc88b7a81cb2b5a1330e1c118f9b5a9 100644 (file)
@@ -3954,5 +3954,8 @@ int cmd_sched(int argc, const char **argv)
                usage_with_options(sched_usage, sched_options);
        }
 
+       /* free usage string allocated by parse_options_subcommand */
+       free((void *)sched_usage[0]);
+
        return 0;
 }