Skip to content
/ linux Public
forked from torvalds/linux

Commit

Permalink
libsubcmd: Fix memory leak in uniq()
Browse files Browse the repository at this point in the history
[ Upstream commit ad30469 ]

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>
Signed-off-by: Sasha Levin <sashal@kernel.org>
  • Loading branch information
captain5050 authored and gregkh committed Feb 3, 2024
1 parent a17bcb3 commit 5d31c86
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions tools/lib/subcmd/help.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,21 @@ void uniq(struct cmdnames *cmds)
if (!cmds->cnt)
return;

for (i = j = 1; i < cmds->cnt; i++)
if (strcmp(cmds->names[i]->name, cmds->names[i-1]->name))
cmds->names[j++] = cmds->names[i];

for (i = 1; i < cmds->cnt; i++) {
if (!strcmp(cmds->names[i]->name, cmds->names[i-1]->name))
zfree(&cmds->names[i - 1]);
}
for (i = 0, j = 0; i < cmds->cnt; i++) {
if (cmds->names[i]) {
if (i == j)
j++;
else
cmds->names[j++] = cmds->names[i];
}
}
cmds->cnt = j;
while (j < i)
cmds->names[j++] = NULL;
}

void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
Expand Down

0 comments on commit 5d31c86

Please sign in to comment.