git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH] git.c: avoid allocating one-too-many elements for new argv array
@ 2009-06-30 20:24 Brandon Casey
  0 siblings, 0 replies; only message in thread
From: Brandon Casey @ 2009-06-30 20:24 UTC (permalink / raw
  To: gitster; +Cc: git, Brandon Casey

From: Brandon Casey <drafnel@gmail.com>

When creating a new argv array from a configured alias and the supplied
command line arguments, the new argv was allocated with one element too
many.  Since the first element of the original argv array is skipped when
copying it to the new_argv, the number of elements that are allocated
should be reduced by one.  'count' is the number of elements that new_argv
contains, and *argcp is the number of elements in the original argv array.
So the total allocation (including the terminating NULL entry) for the
new_argv array should be:

  count + (*argcp - 1) + 1

Also, the explicit assignment of the NULL terminating entry can be avoided
by just copying it over from the original argv array.

Signed-off-by: Brandon Casey <drafnel@gmail.com>
---
 git.c |    3 +--
 1 files changed, 1 insertions(+), 2 deletions(-)

diff --git a/git.c b/git.c
index 7d7f949..f4d53f4 100644
--- a/git.c
+++ b/git.c
@@ -188,10 +188,9 @@ static int handle_alias(int *argcp, const char ***argv)
 				  alias_command);
 
 		new_argv = xrealloc(new_argv, sizeof(char *) *
-				    (count + *argcp + 1));
+				    (count + *argcp));
 		/* insert after command name */
 		memcpy(new_argv + count, *argv + 1, sizeof(char *) * *argcp);
-		new_argv[count+*argcp] = NULL;
 
 		*argv = new_argv;
 		*argcp += count - 1;
-- 
1.6.3.1.24.g152f4

^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2009-06-30 20:25 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-06-30 20:24 [PATCH] git.c: avoid allocating one-too-many elements for new argv array Brandon Casey

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

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 read-only IMAP folder(s) and NNTP newsgroup(s).