Skip to content
/ linux Public

Commit e9003b4

Browse files
srilakshmidjSasha Levin
authored andcommitted
libsubcmd: Fix null intersection case in exclude_cmds()
[ Upstream commit b6ee9b6 ] When there is no exclusion occurring from the cmds list - for example - cmds contains ["read-vdso32"] and excludes contains ["archive"] - the main loop completes with ci == cj == 0. In the original code the loop processing the remaining elements in the list was conditional: if (ci != cj) { ...} So we end up in the assertion loop since ci < cmds->cnt and we incorrectly try to assert the list elements to be NULL and fail with the following error help.c:104: exclude_cmds: Assertion `cmds->names[ci] == NULL' failed. Fix this by moving the if (ci != cj) check inside of a broader loop. If ci != cj, left shift the list elements, as before, and then unconditionally advance the ci and cj indicies which also covers the ci == cj case. Fixes: 1fdf938 ("perf tools: Fix use-after-free in help_unknown_cmd()") Reviewed-by: Guilherme Amadio <amadio@gentoo.org> Signed-off-by: Sri Jayaramappa <sjayaram@akamai.com> Tested-by: Guilherme Amadio <amadio@gentoo.org> Tested-by: Ian Rogers <irogers@google.com> Cc: Joshua Hunt <johunt@akamai.com> Cc: Namhyung Kim <namhyung@kernel.org> Cc: Peter Zijlstra <peterz@infradead.org> Link: https://lore.kernel.org/r/20251202213632.2873731-1-sjayaram@akamai.com Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 2edbcd5 commit e9003b4

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

tools/lib/subcmd/help.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,13 @@ void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
9797
ei++;
9898
}
9999
}
100-
if (ci != cj) {
101-
while (ci < cmds->cnt) {
102-
cmds->names[cj++] = cmds->names[ci];
103-
cmds->names[ci++] = NULL;
100+
while (ci < cmds->cnt) {
101+
if (ci != cj) {
102+
cmds->names[cj] = cmds->names[ci];
103+
cmds->names[ci] = NULL;
104104
}
105+
ci++;
106+
cj++;
105107
}
106108
for (ci = cj; ci < cmds->cnt; ci++)
107109
assert(cmds->names[ci] == NULL);

0 commit comments

Comments
 (0)