From: Jeff King <peff@peff.net>
To: Tay Ray Chuan <rctay89@gmail.com>
Cc: Git Mailing List <git@vger.kernel.org>,
Junio C Hamano <gitster@pobox.com>
Subject: Re: [PATCH 1/4] help.c::uniq: plug a leak
Date: Sun, 6 May 2012 04:12:14 -0400 [thread overview]
Message-ID: <20120506081213.GA27878@sigill.intra.peff.net> (raw)
In-Reply-To: <1336287330-7215-2-git-send-email-rctay89@gmail.com>
On Sun, May 06, 2012 at 02:55:27PM +0800, Tay Ray Chuan wrote:
> 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;
> }
Freeing the strings at the end of each run of duplicates is confusing to
read. And your implementation is buggy: if there are duplicates at the
very end of the list, you would never free them (you would need to check
'c' again at the end of the loop).
I think you avoided freeing as you go because that invalidates the i-1
element that we use in the comparison. However, we can observe that the
j-1 element can serve the same purpose, as 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).
So this shorter patch should be sufficient (though I didn't actually
test it):
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;
}
next prev parent reply other threads:[~2012-05-06 8:13 UTC|newest]
Thread overview: 37+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-05-06 6:55 [PATCH 0/4] allow recovery from command name typos Tay Ray Chuan
2012-05-06 6:55 ` [PATCH 1/4] help.c::uniq: plug a leak Tay Ray Chuan
2012-05-06 6:55 ` [PATCH 2/4] help.c::exclude_cmds: " Tay Ray Chuan
2012-05-06 6:55 ` [PATCH 3/4] help.c: plug a leak when help.autocorrect is set Tay Ray Chuan
2012-05-06 6:55 ` [PATCH 4/4] allow recovery from command name typos Tay Ray Chuan
2012-05-06 8:21 ` Jeff King
2012-05-06 16:07 ` Tay Ray Chuan
2012-05-07 9:43 ` Thomas Rast
2012-05-07 15:49 ` Tay Ray Chuan
2012-05-07 17:41 ` Junio C Hamano
2012-05-09 15:06 ` Tay Ray Chuan
2012-05-09 17:03 ` Junio C Hamano
[not found] ` <CAOBOgRaDEgAqXWmdC6hrudkL5OwzeMffbj2RtKMxf2TsYWzotA@mail.gmail.com>
2012-05-06 16:04 ` Tay Ray Chuan
2012-05-06 8:12 ` Jeff King [this message]
2012-05-06 15:54 ` [PATCH 1/4] help.c::uniq: plug a leak Tay Ray Chuan
2012-05-07 7:30 ` Jeff King
2012-07-25 16:16 ` [PATCH v2 0/4] allow recovery from command name typos Tay Ray Chuan
2012-07-25 16:16 ` [PATCH v2 1/4] help.c::uniq: plug a leak Tay Ray Chuan
2012-07-25 16:16 ` [PATCH v2 2/4] help.c::exclude_cmds: realloc() before copy, " Tay Ray Chuan
2012-07-25 16:16 ` [PATCH v2 3/4] help.c: plug leaks with(out) help.autocorrect Tay Ray Chuan
2012-07-25 16:16 ` [PATCH v2 4/4] allow recovery from command name typos Tay Ray Chuan
2012-07-25 17:57 ` Junio C Hamano
2012-07-26 17:08 ` Tay Ray Chuan
2012-07-26 17:26 ` Jeff King
2012-07-26 17:59 ` Junio C Hamano
2012-07-26 18:37 ` Jeff King
2012-07-26 17:53 ` Junio C Hamano
2012-07-25 17:47 ` [PATCH v2 3/4] help.c: plug leaks with(out) help.autocorrect Junio C Hamano
2012-07-25 17:39 ` [PATCH v2 2/4] help.c::exclude_cmds: realloc() before copy, plug a leak Junio C Hamano
2012-08-05 18:45 ` [PATCH v3 0/2] allow recovery from command name typos Tay Ray Chuan
2012-08-05 18:45 ` [PATCH v3 1/2] add interface for /dev/tty interaction Tay Ray Chuan
2012-08-05 18:45 ` [PATCH v3 2/2] allow recovery from command name typos Tay Ray Chuan
2012-08-06 0:50 ` Junio C Hamano
2012-08-05 20:11 ` [PATCH v3 1/2] add interface for /dev/tty interaction Junio C Hamano
2012-08-06 19:45 ` Jeff King
2012-08-06 19:56 ` Jeff King
2012-08-06 20:01 ` Junio C Hamano
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20120506081213.GA27878@sigill.intra.peff.net \
--to=peff@peff.net \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=rctay89@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).