[PATCH 1/4] help.c::uniq: plug a leak
Signed-off-by: Tay Ray Chuan <rctay89@gmail.com>
---
help.c | 14 ++++++++++++--
1 file changed, 12 insertions(+), 2 deletions(-)
diff --git a/help.c b/help.c
index 69d483d..b64056d 100644
--- a/help.c
+++ b/help.c
@@ -38,14 +38,24 @@ static int cmdname_compare(const void *a_, const void *b_)
static void uniq(struct cmdnames *cmds)
{
- int i, j;
+ int i, j, c = 0;
if (!cmds->cnt)
return;
for (i = j = 1; i < cmds->cnt; i++)
- if (strcmp(cmds->names[i]->name, cmds->names[i-1]->name))
+ if (strcmp(cmds->names[i]->name, cmds->names[i-1]->name)) {
+
+ /* The i-1 entry was the cth duplicate
+ * Guarantees c=0
+ */
+ for (; c >= 1; c--)
+ free(cmds->names[i - c]);
+
cmds->names[j++] = cmds->names[i];
+ } else {
+ c++;
+ }
cmds->cnt = j;
}
--
1.7.10.1.611.g8a79d96