From 4a15758f2ef97970694012cfd6da7c8449bc68c2 Mon Sep 17 00:00:00 2001 From: Jeff King Date: Thu, 26 Jul 2012 00:16:19 +0800 Subject: help.c::uniq: plug a leak We observe that the j-1 element can serve the same purpose as the i-1 element that we use in the strcmp(); it is either: 1. Exactly i-1, when the loop begins (and until we see a duplicate). 2. The same pointer that was stored at i-1 (if it was not a duplicate, and we just copied it into place). 3. A pointer to an equivalent string (i.e., we rejected i-1 _because_ it was identical to j-1). Signed-off-by: Jeff King Signed-off-by: Tay Ray Chuan Signed-off-by: Junio C Hamano --- help.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'help.c') diff --git a/help.c b/help.c index 662349dd56..699149201e 100644 --- a/help.c +++ b/help.c @@ -44,9 +44,12 @@ static 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)) + for (i = j = 1; i < cmds->cnt; i++) { + if (!strcmp(cmds->names[i]->name, cmds->names[j-1]->name)) + free(cmds->names[i]); + else cmds->names[j++] = cmds->names[i]; + } cmds->cnt = j; } -- cgit v1.2.3 From 6a17f583f4db98a867d84ca95bbbc4de3cd0feaa Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Wed, 25 Jul 2012 11:01:12 -0700 Subject: help.c::exclude_cmds(): plug a leak Command name removed from the list of commands via the exclusion were overwritten and lost without being freed. Signed-off-by: Junio C Hamano --- help.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'help.c') diff --git a/help.c b/help.c index 699149201e..2a42ec6d1f 100644 --- a/help.c +++ b/help.c @@ -64,9 +64,10 @@ void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes) cmp = strcmp(cmds->names[ci]->name, excludes->names[ei]->name); if (cmp < 0) cmds->names[cj++] = cmds->names[ci++]; - else if (cmp == 0) - ci++, ei++; - else if (cmp > 0) + else if (cmp == 0) { + ei++; + free(cmds->names[ci++]); + } else if (cmp > 0) ei++; } -- cgit v1.2.3