Skip to content

Commit

Permalink
git.c: avoid allocating one-too-many elements for new argv array
Browse files Browse the repository at this point in the history
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>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Brandon Casey authored and Junio C Hamano committed Jun 30, 2009
1 parent b8f2626 commit 6167c13
Showing 1 changed file with 1 addition and 2 deletions.
3 changes: 1 addition & 2 deletions git.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 6167c13

Please sign in to comment.