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

Re: minor problems in git.c

From
Junio C Hamano <junkio@cox.net>
Date
Dec 2, 2005, 01:07 UTC
Message-ID
<7v1x0wb936.fsf@assigned-by-dhcp.cox.net>
In-Reply-To
<81b0412b0512010602l63ecev1ba03fb90d06e071@mail.gmail.com>
Alex Riesen <raa.lkml@gmail.com> writes:
>> Shouldn't you check the return value of snprintf
>
> Probably. For the case where length of a git-command-name +
> --exec-prefix together are longer than PATH_MAX.
Combined, something like this.
-- >8 --
Subject: git wrapper: more careful argument stuffing
From: Alex Riesen <raa.lkml@gmail.com>
Date: Thu, 1 Dec 2005 13:48:35 +0100
 - Use stderr for error output
 - Build git_command more careful
 - ENOENT is good enough for check of failed exec to show usage, no
   access() check needed
[jc: Originally from Alex Riesen with inputs from Sven
 Verdoolaege mixed in.]
Signed-off-by: Junio C Hamano <junkio@cox.net>
---
 git.c |   19 ++++++++++++-------
 1 files changed, 12 insertions(+), 7 deletions(-)
6e3f1bf88fdce10ba5c0274e017667d21bb68359
diff --git a/git.c b/git.c
index 0b10b6e..878c359 100644
--- a/git.c
+++ b/git.c
@@ -283,16 +283,21 @@ int main(int argc, char **argv, char **e
 	len = strlen(git_command);
 	prepend_to_path(git_command, len);
 
-	strncat(&git_command[len], "/git-", sizeof(git_command) - len);
-	len += 5;
-	strncat(&git_command[len], argv[i], sizeof(git_command) - len);
-
-	if (access(git_command, X_OK))
-		usage(exec_path, "'%s' is not a git-command", argv[i]);
+	len += snprintf(git_command + len, sizeof(git_command) - len,
+			"/git-%s", argv[i]);
+	if (sizeof(git_command) <= len) {
+		fprintf(stderr, "git: command name given is too long (%d)\n", len);
+		exit(1);
+	}
 
 	/* execve() can only ever return if it fails */
 	execve(git_command, &argv[i], envp);
-	printf("Failed to run command '%s': %s\n", git_command, strerror(errno));
+
+	if (errno == ENOENT)
+		usage(exec_path, "'%s' is not a git-command", argv[i]);
+
+	fprintf(stderr, "Failed to run command '%s': %s\n",
+		git_command, strerror(errno));
 
 	return 1;
 }
-- 
0.99.9.GIT
Previous: Alex RiesenNext: Alex Riesen
Message 5 of 6 in “minor problems in git.c”
  1. Robert WatsonDec 1, 2005
  2. Alex RiesenDec 1, 2005
  3. Sven VerdoolaegeDec 1, 2005
  4. Alex RiesenDec 1, 2005
  5. Junio C HamanoDec 2, 2005
  6. Alex RiesenDec 2, 2005

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.