git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] git.c: avoid allocating one-too-many elements for new argv array

From
Brandon Casey <casey@nrlssc.navy.mil>
Date
Jun 30, 2009, 20:24 UTC
Message-ID
<5TvnwnOe5CdWOiVI3-C6R28MSiRiTmlwLVDBsVsXh5WsVbT0-8FT803CQ2tY3PR_02aq3nSSHjc@cipher.nrlssc.navy.mil>
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
Message 1 of 1 in “git.c: avoid allocating one-too-many elements for new argv array”
  1. git.c: avoid allocating one-too-many elements for new argv arrayBrandon Casey, Jun 30, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.