]> www.infradead.org Git - users/jedix/linux-maple.git/commitdiff
perf sched: Make sure it frees the usage string
authorNamhyung Kim <namhyung@kernel.org>
Thu, 3 Jul 2025 01:49:35 +0000 (18:49 -0700)
committerNamhyung Kim <namhyung@kernel.org>
Thu, 3 Jul 2025 18:27:58 +0000 (11:27 -0700)
The parse_options_subcommand() allocates the usage string based on the
given subcommands.  So it should reach the end of the function to free
the string to prevent memory leaks.

Fixes: 1a5efc9e13f357ab ("libsubcmd: Don't free the usage string")
Reviewed-by: Ian Rogers <irogers@google.com>
Tested-by: Ian Rogers <irogers@google.com>
Link: https://lore.kernel.org/r/20250703014942.1369397-2-namhyung@kernel.org
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
tools/perf/builtin-sched.c

index 26ece6e9bfd167b3bc9357cfe8048af9b1c372e5..b7bbfad0ed600eee18a6e51b55c7e6685938384e 100644 (file)
@@ -3902,9 +3902,9 @@ int cmd_sched(int argc, const char **argv)
         * Aliased to 'perf script' for now:
         */
        if (!strcmp(argv[0], "script")) {
-               return cmd_script(argc, argv);
+               ret = cmd_script(argc, argv);
        } else if (strlen(argv[0]) > 2 && strstarts("record", argv[0])) {
-               return __cmd_record(argc, argv);
+               ret = __cmd_record(argc, argv);
        } else if (strlen(argv[0]) > 2 && strstarts("latency", argv[0])) {
                sched.tp_handler = &lat_ops;
                if (argc > 1) {
@@ -3913,7 +3913,7 @@ int cmd_sched(int argc, const char **argv)
                                usage_with_options(latency_usage, latency_options);
                }
                setup_sorting(&sched, latency_options, latency_usage);
-               return perf_sched__lat(&sched);
+               ret = perf_sched__lat(&sched);
        } else if (!strcmp(argv[0], "map")) {
                if (argc) {
                        argc = parse_options(argc, argv, map_options, map_usage, 0);
@@ -3924,13 +3924,14 @@ int cmd_sched(int argc, const char **argv)
                                sched.map.task_names = strlist__new(sched.map.task_name, NULL);
                                if (sched.map.task_names == NULL) {
                                        fprintf(stderr, "Failed to parse task names\n");
-                                       return -1;
+                                       ret = -1;
+                                       goto out;
                                }
                        }
                }
                sched.tp_handler = &map_ops;
                setup_sorting(&sched, latency_options, latency_usage);
-               return perf_sched__map(&sched);
+               ret = perf_sched__map(&sched);
        } else if (strlen(argv[0]) > 2 && strstarts("replay", argv[0])) {
                sched.tp_handler = &replay_ops;
                if (argc) {
@@ -3938,7 +3939,7 @@ int cmd_sched(int argc, const char **argv)
                        if (argc)
                                usage_with_options(replay_usage, replay_options);
                }
-               return perf_sched__replay(&sched);
+               ret = perf_sched__replay(&sched);
        } else if (!strcmp(argv[0], "timehist")) {
                if (argc) {
                        argc = parse_options(argc, argv, timehist_options,
@@ -3954,19 +3955,19 @@ int cmd_sched(int argc, const char **argv)
                                parse_options_usage(NULL, timehist_options, "w", true);
                        if (sched.show_next)
                                parse_options_usage(NULL, timehist_options, "n", true);
-                       return -EINVAL;
+                       ret = -EINVAL;
+                       goto out;
                }
                ret = symbol__validate_sym_arguments();
-               if (ret)
-                       return ret;
-
-               return perf_sched__timehist(&sched);
+               if (!ret)
+                       ret = perf_sched__timehist(&sched);
        } else {
                usage_with_options(sched_usage, sched_options);
        }
 
+out:
        /* free usage string allocated by parse_options_subcommand */
        free((void *)sched_usage[0]);
 
-       return 0;
+       return ret;
 }