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

[PATCH 2/2] Fix a memory leak in "connect.c" and die if command too long.

From
Christian Couder <chriscool@tuxfamily.org>
Date
Sep 11, 2006, 05:04 UTC
Message-ID
<20060911070450.bf4cc778.chriscool@tuxfamily.org>
In-Reply-To
<20060911065922.93139e5d.chriscool@tuxfamily.org>

Use "add_to_string" instead of "sq_quote" and "snprintf", so that there is no memory allocation and no memory leak. Also check if the command is too long to fit into the buffer and die if this is the case, instead of truncating it to the buffer size.

Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
---
 connect.c |   17 ++++++++++++++---
 1 files changed, 14 insertions(+), 3 deletions(-)
diff --git a/connect.c b/connect.c
index 1c6429b..49251f9 100644
--- a/connect.c
+++ b/connect.c
@@ -599,12 +599,13 @@ static void git_proxy_connect(int fd[2],
 	close(pipefd[1][0]);
 }
 
+#define MAX_CMD_LEN 1024
+
 /*
  * Yeah, yeah, fixme. Need to pass in the heads etc.
  */
 int git_connect(int fd[2], char *url, const char *prog)
 {
-	char command[1024];
 	char *host, *path = url;
 	char *end;
 	int c;
@@ -697,8 +698,18 @@ int git_connect(int fd[2], char *url, co
 	if (pid < 0)
 		die("unable to fork");
 	if (!pid) {
-		snprintf(command, sizeof(command), "%s %s", prog,
-			 sq_quote(path));
+		char command[MAX_CMD_LEN];
+		char *posn = command;
+		int size = MAX_CMD_LEN;
+		int of = 0;
+
+		of |= add_to_string(&posn, &size, prog, 0);
+		of |= add_to_string(&posn, &size, " ", 0);
+		of |= add_to_string(&posn, &size, path, 1);
+
+		if (of)
+			die("command line too long");
+
 		dup2(pipefd[1][0], 0);
 		dup2(pipefd[0][1], 1);
 		close(pipefd[0][0]);
-- 
1.4.2.g430e
Previous: Christian CouderNext: Junio C Hamano
Message 5 of 6 in “Fix memory leak in "connect.c".”
  1. Fix memory leak in "connect.c".Christian Couder, Sep 7, 2006
  2. Junio C HamanoSep 7, 2006
  3. 0/2 Command preparation cleanup in "connect.c" (was: [PATCH] Fix memory leak in "connect.c".)Christian Couder, Sep 11, 2006
  4. 1/2 Move add_to_string to "quote.c" and make it extern.Christian Couder, Sep 11, 2006
  5. 2/2 Fix a memory leak in "connect.c" and die if command too long.Christian Couder, Sep 11, 2006
  6. Junio C HamanoSep 11, 2006

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.