[PATCH 2/4] help.c::exclude_cmds: plug a leak
- From
Tay Ray Chuan <rctay89@gmail.com>
- Date
- May 6, 2012, 06:55 UTC
- Message-ID
- <1336287330-7215-3-git-send-email-rctay89@gmail.com>
- In-Reply-To
- <1336287330-7215-2-git-send-email-rctay89@gmail.com>
Create a fresh cmdnames to hold the entries we want to keep, such that we free the excluded entries in cmds only.
Signed-off-by: Tay Ray Chuan <rctay89@gmail.com>
---
A solution that does not require a fresh cmdnames escapes me. --- help.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-)
diff --git a/help.c b/help.c index b64056d..705f152 100644 --- a/help.c +++ b/help.c @@ -65,21 +65,29 @@ void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes) int ci, cj, ei; int cmp; + struct cmdnames excluded; + memset(&excluded, 0, sizeof(excluded)); + ALLOC_GROW(excluded.names, cmds->cnt, excluded.alloc); + ci = cj = ei = 0; while (ci < cmds->cnt && ei < excludes->cnt) { cmp = strcmp(cmds->names[ci]->name, excludes->names[ei]->name); - if (cmp < 0) - cmds->names[cj++] = cmds->names[ci++]; - else if (cmp == 0) + if (cmp < 0) { + excluded.names[cj] = cmds->names[ci]; + cmds->names[ci] = NULL; + ci++, cj++; + } else if (cmp == 0) ci++, ei++; else if (cmp > 0) ei++; } - while (ci < cmds->cnt) - cmds->names[cj++] = cmds->names[ci++]; - + clean_cmdnames(cmds); + cmds->alloc = excluded.alloc; cmds->cnt = cj; + cmds->names = excluded.names; + while (cj--) + cmds->names[cj] = excluded.names[cj]; } static void pretty_print_string_list(struct cmdnames *cmds,
-- 1.7.10.1.611.g8a79d96