]> git.ipfire.org Git - thirdparty/kernel/stable.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)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Fri, 15 Aug 2025 10:13:52 +0000 (12:13 +0200)
[ Upstream commit 10d9b89203765fb776512742c13af8dd92821842 ]

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>
Signed-off-by: Sasha Levin <sashal@kernel.org>
tools/perf/builtin-sched.c

index 5981cc51abc88b7a81cb2b5a1330e1c118f9b5a9..864fcb76e7582cd0279150033b76a1bed2635d6c 100644 (file)
@@ -3891,9 +3891,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) {
@@ -3902,7 +3902,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);
@@ -3913,13 +3913,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) {
@@ -3927,7 +3928,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,
@@ -3943,19 +3944,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;
 }