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 <peff@peff.net> Signed-off-by: Tay Ray Chuan <rctay89@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Jul 26, 2012 at 00:16 UTC 4a15758f2ef97970694012cfd6da7c8449bc68c2
1 file changed +5 -2
help.c
+5 -2
@@ -44,9 +44,12 @@ static void uniq(struct cmdnames *cmds)
44 if (!cmds->cnt)
45 return;
46
47 - for (i = j = 1; i < cmds->cnt; i++)
48 - if (strcmp(cmds->names[i]->name, cmds->names[i-1]->name))
47 + for (i = j = 1; i < cmds->cnt; i++) {
48 + if (!strcmp(cmds->names[i]->name, cmds->names[j-1]->name))
49 + free(cmds->names[i]);
50 + else
51 cmds->names[j++] = cmds->names[i];
52 + }
53
54 cmds->cnt = j;
55 }