Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

lib subcmd: Avoid memory leak in exclude_cmds

exclude_cmds will shorten the cmds names array, before doing so free the
removed entry.

Signed-off-by: Ian Rogers <irogers@google.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: André Almeida <andrealmeid@igalia.com>
Cc: Darren Hart <dvhart@infradead.org>
Cc: Davidlohr Bueso <dave@stgolabs.net>
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>
Cc: Thomas Gleixner <tglx@linutronix.de>
Link: https://lore.kernel.org/r/20230611233610.953456-1-irogers@google.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Ian Rogers and committed by
Arnaldo Carvalho de Melo
657a3efe d927ef50

+6 -2
+6 -2
tools/lib/subcmd/help.c
··· 66 66 while (ci < cmds->cnt && ei < excludes->cnt) { 67 67 cmp = strcmp(cmds->names[ci]->name, excludes->names[ei]->name); 68 68 if (cmp < 0) { 69 + zfree(&cmds->names[cj]); 69 70 cmds->names[cj++] = cmds->names[ci++]; 70 71 } else if (cmp == 0) { 71 72 ci++; ··· 76 75 } 77 76 } 78 77 79 - while (ci < cmds->cnt) 78 + while (ci < cmds->cnt) { 79 + zfree(&cmds->names[cj]); 80 80 cmds->names[cj++] = cmds->names[ci++]; 81 - 81 + } 82 + for (ci = cj; ci < cmds->cnt; ci++) 83 + zfree(&cmds->names[ci]); 82 84 cmds->cnt = cj; 83 85 } 84 86