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 <irogers@google.com> Cc: Adrian Hunter <adrian.hunter@intel.com> Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com> Cc: Chenyuan Mi <cymi20@fudan.edu.cn> Cc: Ingo Molnar <mingo@redhat.com> Cc: Jiri Olsa <jolsa@kernel.org> Cc: Mark Rutland <mark.rutland@arm.com> Cc: Namhyung Kim <namhyung@kernel.org> Cc: Peter Zijlstra <peterz@infradead.org> Link: https://lore.kernel.org/r/20231208000515.1693746-1-irogers@google.com Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
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)