Skip to content

Commit

Permalink
help.c::uniq: plug a leak
Browse files Browse the repository at this point in the history
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 <peff@peff.net>
Signed-off-by: Tay Ray Chuan <rctay89@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Jeff King authored and Junio C Hamano committed Jul 25, 2012
1 parent 476109f commit 4a15758
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions help.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 4a15758

Please sign in to comment.