git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: Tay Ray Chuan <rctay89@gmail.com>
Cc: "Git Mailing List" <git@vger.kernel.org>,
	"Jeff King" <peff@peff.net>, Thomas Rast <trast@student.ethz.ch>
Subject: Re: [PATCH v2 2/4] help.c::exclude_cmds: realloc() before copy, plug a leak
Date: Wed, 25 Jul 2012 10:39:37 -0700	[thread overview]
Message-ID: <7v394fd9k6.fsf@alter.siamese.dyndns.org> (raw)
In-Reply-To: <1343232982-10540-3-git-send-email-rctay89@gmail.com> (Tay Ray Chuan's message of "Thu, 26 Jul 2012 00:16:20 +0800")

Tay Ray Chuan <rctay89@gmail.com> writes:

> Copying with structural assignment may not take into account that the
> LHS struct has sufficient memory, especially since the cmdname->name
> member is nonfixed in size. Be unambiguous about it by realloc()'ing it
> to be of sufficient size.

If the original code were

	*(cmd->names[cj++]) = *(cmd->names[ci++]);

there may be a structural assignment involved, but

	cmds->names[dst] = cmd->names[src]

just copies the pointer that points at a struct cmdname that records
the src command name to another slot of cmds->names[] array, whose
elements are pointers, no?  What's there to realloc?

> @@ -58,20 +69,25 @@ void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
>  {
>  	int ci, cj, ei;
>  	int cmp;
> +	int last_cj;
>  
>  	ci = cj = ei = 0;
>  	while (ci < cmds->cnt && ei < excludes->cnt) {
>  		cmp = strcmp(cmds->names[ci]->name, excludes->names[ei]->name);
>  		if (cmp < 0)
> -			cmds->names[cj++] = cmds->names[ci++];
> +			copy_cmdname(&cmds->names[cj++], cmds->names[ci++]);
>  		else if (cmp == 0)
>  			ci++, ei++;
>  		else if (cmp > 0)
>  			ei++;
>  	}
> +	last_cj = cj;
>  
>  	while (ci < cmds->cnt)
> -		cmds->names[cj++] = cmds->names[ci++];
> +		copy_cmdname(&cmds->names[cj++], cmds->names[ci++]);
> +
> +	while (last_cj < cmds->cnt)
> +		free(cmds->names[last_cj++]);
>  
>  	cmds->cnt = cj;
>  }

We shifted cmds->names[] array to skip entries that appear in
excludes.  If original cmds->names[] had "0", "1", "2", "3", ...
and excludes had "0" and "1", cmds->names[] would contain "2", "3",
"2", "3"; the first two are copied over "0" and "1" that are
excluded, and the latter two are leftover beyond last_cj.  The
corresponding names share the same structure (cmds->names[] is an
array of pointers).  Doesn't freeing cmds->names[2] free the
structure that is used by both cmds->names[0] and cmds->names[2]?

Confused.

The function drops cmds->names[ci] when it appears in excludes, so
you may want to free it when it happens, though.

 help.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/help.c b/help.c
index 6991492..cae389b 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(cmd->names[ci++]);
+		} else if (cmp > 0)
 			ei++;
 	}
 

  parent reply	other threads:[~2012-07-25 17:39 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   ` [PATCH 1/4] help.c::uniq: plug a leak Jeff King
2012-05-06 15:54     ` 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       ` Junio C Hamano [this message]
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=7v394fd9k6.fsf@alter.siamese.dyndns.org \
    --to=gitster@pobox.com \
    --cc=git@vger.kernel.org \
    --cc=peff@peff.net \
    --cc=rctay89@gmail.com \
    --cc=trast@student.ethz.ch \
    /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).