{"thread":{"id":"19984","subject":"[PATCH] git.c: avoid allocating one-too-many elements for new argv array","startedAt":"2009-06-30T20:24:13Z","lastAt":"2009-06-30T20:24:13Z","messageCount":1,"participants":["Brandon Casey"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"117251","messageId":"5TvnwnOe5CdWOiVI3-C6R28MSiRiTmlwLVDBsVsXh5WsVbT0-8FT803CQ2tY3PR_02aq3nSSHjc@cipher.nrlssc.navy.mil","threadId":"19984","inReplyTo":null,"subject":"[PATCH] git.c: avoid allocating one-too-many elements for new argv array","fromName":"Brandon Casey","fromEmail":"casey@nrlssc.navy.mil","sentAt":"2009-06-30T20:24:13Z","receivedAt":"2009-06-30T20:24:13Z","isPatch":true,"sender":{"key":"drafnel@gmail.com","avatar":"https://avatars.githubusercontent.com/u/921167?v=4"},"body":"From: Brandon Casey <drafnel@gmail.com>\n\nWhen creating a new argv array from a configured alias and the supplied\ncommand line arguments, the new argv was allocated with one element too\nmany.  Since the first element of the original argv array is skipped when\ncopying it to the new_argv, the number of elements that are allocated\nshould be reduced by one.  'count' is the number of elements that new_argv\ncontains, and *argcp is the number of elements in the original argv array.\nSo the total allocation (including the terminating NULL entry) for the\nnew_argv array should be:\n\n  count + (*argcp - 1) + 1\n\nAlso, the explicit assignment of the NULL terminating entry can be avoided\nby just copying it over from the original argv array.\n\nSigned-off-by: Brandon Casey <drafnel@gmail.com>\n---\n git.c |    3 +--\n 1 files changed, 1 insertions(+), 2 deletions(-)\n\ndiff --git a/git.c b/git.c\nindex 7d7f949..f4d53f4 100644\n--- a/git.c\n+++ b/git.c\n@@ -188,10 +188,9 @@ static int handle_alias(int *argcp, const char ***argv)\n \t\t\t\t  alias_command);\n \n \t\tnew_argv = xrealloc(new_argv, sizeof(char *) *\n-\t\t\t\t    (count + *argcp + 1));\n+\t\t\t\t    (count + *argcp));\n \t\t/* insert after command name */\n \t\tmemcpy(new_argv + count, *argv + 1, sizeof(char *) * *argcp);\n-\t\tnew_argv[count+*argcp] = NULL;\n \n \t\t*argv = new_argv;\n \t\t*argcp += count - 1;\n-- \n1.6.3.1.24.g152f4\n"}]}