Skip to content

Commit ad30469

Browse files
captain5050acmel
authored andcommitted
libsubcmd: Fix memory leak in uniq()
uniq() will write one command name over another causing the overwritten string to be leaked. Fix by doing a pass that removes duplicates and a second that removes the holes. Signed-off-by: Ian Rogers <[email protected]> Cc: Adrian Hunter <[email protected]> Cc: Alexander Shishkin <[email protected]> Cc: Chenyuan Mi <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: Mark Rutland <[email protected]> Cc: Namhyung Kim <[email protected]> Cc: Peter Zijlstra <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent 6af6d22 commit ad30469

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

tools/lib/subcmd/help.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,21 @@ void uniq(struct cmdnames *cmds)
5252
if (!cmds->cnt)
5353
return;
5454

55-
for (i = j = 1; i < cmds->cnt; i++)
56-
if (strcmp(cmds->names[i]->name, cmds->names[i-1]->name))
57-
cmds->names[j++] = cmds->names[i];
58-
55+
for (i = 1; i < cmds->cnt; i++) {
56+
if (!strcmp(cmds->names[i]->name, cmds->names[i-1]->name))
57+
zfree(&cmds->names[i - 1]);
58+
}
59+
for (i = 0, j = 0; i < cmds->cnt; i++) {
60+
if (cmds->names[i]) {
61+
if (i == j)
62+
j++;
63+
else
64+
cmds->names[j++] = cmds->names[i];
65+
}
66+
}
5967
cmds->cnt = j;
68+
while (j < i)
69+
cmds->names[j++] = NULL;
6070
}
6171

6272
void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)

0 commit comments

Comments
 (0)