Re: [PATCH 1/4] help.c::uniq: plug a leak
- From
Tay Ray Chuan <rctay89@gmail.com>
- Date
- May 6, 2012, 15:54 UTC
- Message-ID
- <CALUzUxqtKGd9REqwyZLVnr4zcd20GmSREeNL7tDpA8kYaTtWBg@mail.gmail.com>
- In-Reply-To
- <20120506081213.GA27878@sigill.intra.peff.net>
On Sun, May 6, 2012 at 4:12 PM, Jeff King <peff@peff.net> wrote:
> So this shorter patch should be sufficient (though I didn't actually > test it):
Tested and works fine.
Show 19 quoted lines
> diff --git a/help.c b/help.c
> index 69d483d..d3868b3 100644
> --- a/help.c
> +++ b/help.c
> @@ -43,9 +43,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;
> }Not only is this better than mine in terms of readability, it is better than the original code.
-- Cheers, Ray Chuan