Skip to contentSkip to conversation

perf_counter tools: Fix memory leaks of add_cmdname()

PATCH In review Sohaib Mohamed 2 participants 2 messages Nov 19, 2021 – Nov 25, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 perf_counter tools: Fix memory leaks of add_cmdname()

ASan reports some memory leaks when running:

$ perf xyz

This patch adds the missing clean_cmdnames()

Fixes 078006012401 ("perf_counter tools: add in basic glue from Git")
---
 tools/lib/subcmd/help.c | 1 +
 1 file changed, 1 insertion(+)
tools/lib/subcmd/help.c +1 -0
index bf02d62a3b2b..4a433f4d19a1 100644
@@ -187,6 +187,7 @@ static void list_commands_in_dir(struct cmdnames *cmds,
 
 		add_cmdname(cmds, de->d_name + prefix_len, entlen);
 	}
+	clean_cmdnames(cmds);
 	closedir(dir);
 	free(buf);
 }
Signature · 1 line
2.25.1
#1 lore raw

Em Fri, Nov 19, 2021 at 10:46:36AM +0200, Sohaib Mohamed escreveu:

Quoted 18 lines

ASan reports some memory leaks when running:

$ perf xyz

This patch adds the missing clean_cmdnames()

Fixes 078006012401 ("perf_counter tools: add in basic glue from Git")
---
 tools/lib/subcmd/help.c | 1 +
 1 file changed, 1 insertion(+)
tools/lib/subcmd/help.c +0 -0
index bf02d62a3b2b..4a433f4d19a1 100644

tools/lib/subcmd/help.c static void list_commands_in_dir(struct cmdnames *cmds, · line 190

Quoted 4 lines
tools/lib/subcmd/help.c +1 -0
@@ -187,6 +187,7 @@ static void list_commands_in_dir(struct cmdnames *cmds,
 
 		add_cmdname(cmds, de->d_name + prefix_len, entlen);
 	}
+	clean_cmdnames(cmds);

Have you tested this?

Look at this usage:

void load_command_list(const char *prefix,
struct cmdnames *main_cmds,
struct cmdnames *other_cmds)

{
        const char *env_path = getenv("PATH");
        char *exec_path = get_argv_exec_path();

        if (exec_path) {
                list_commands_in_dir(main_cmds, exec_path, prefix);
                qsort(main_cmds->names, main_cmds->cnt,
                      sizeof(*main_cmds->names), cmdname_compare);
                uniq(main_cmds);
        }

If you call clean_cmdnames() in list_commands_in_dir() that call to qsort will be with main_cmds emptied, no?

- Arnaldo

Quoted 5 lines
tools/lib/subcmd/help.c +0 -0
 	closedir(dir);
 	free(buf);
 }
Signature · 1 line
2.25.1
Signature · 1 line
- Arnaldo