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

[PATCH 2/6] run_column_filter: use argv_array

From
Jeff King <peff@peff.net>
Date
May 15, 2014, 08:33 UTC
Message-ID
<20140515083340.GB26866@sigill.intra.peff.net>
In-Reply-To
<20140515082943.GA26473@sigill.intra.peff.net>

We currently set up the argv array by hand in a fixed-size stack-local array. Using an argv array is more readable, as it handles buffer allocation us (not to mention makes it obvious we do not overflow the array).

However, there's a more subtle benefit, too. We leave the function having run start_command (with the child_process in a static global), and then later run finish_command from another function. That means when we run finish_command, neither column_process.argv nor the memory it points to is valid any longer.

Most of the time finish_command does not bother looking at argv, but it may if it encounters an error (e.g., waitpid failure or signal death). This is unusual, which is why nobody has noticed. But by using run-command's built-in argv_array, the memory ownership is handled for us automatically.

Signed-off-by: Jeff King <peff@peff.net>
---
 column.c | 43 +++++++++++++------------------------------
 1 file changed, 13 insertions(+), 30 deletions(-)
diff --git a/column.c b/column.c
index 8d1ce88..1a468de 100644
--- a/column.c
+++ b/column.c
@@ -370,46 +370,29 @@ static struct child_process column_process;
 
 int run_column_filter(int colopts, const struct column_options *opts)
 {
-	const char *av[10];
-	int ret, ac = 0;
-	struct strbuf sb_colopt  = STRBUF_INIT;
-	struct strbuf sb_width   = STRBUF_INIT;
-	struct strbuf sb_padding = STRBUF_INIT;
+	struct argv_array *argv;
 
 	if (fd_out != -1)
 		return -1;
 
-	av[ac++] = "column";
-	strbuf_addf(&sb_colopt, "--raw-mode=%d", colopts);
-	av[ac++] = sb_colopt.buf;
-	if (opts && opts->width) {
-		strbuf_addf(&sb_width, "--width=%d", opts->width);
-		av[ac++] = sb_width.buf;
-	}
-	if (opts && opts->indent) {
-		av[ac++] = "--indent";
-		av[ac++] = opts->indent;
-	}
-	if (opts && opts->padding) {
-		strbuf_addf(&sb_padding, "--padding=%d", opts->padding);
-		av[ac++] = sb_padding.buf;
-	}
-	av[ac] = NULL;
+	memset(&column_process, 0, sizeof(column_process));
+	argv = &column_process.args;
+
+	argv_array_push(argv, "column");
+	argv_array_pushf(argv, "--raw-mode=%d", colopts);
+	if (opts && opts->width)
+		argv_array_pushf(argv, "--width=%d", opts->width);
+	if (opts && opts->indent)
+		argv_array_pushf(argv, "--indent=%s", opts->indent);
+	if (opts && opts->padding)
+		argv_array_pushf(argv, "--padding=%d", opts->padding);
 
 	fflush(stdout);
-	memset(&column_process, 0, sizeof(column_process));
 	column_process.in = -1;
 	column_process.out = dup(1);
 	column_process.git_cmd = 1;
-	column_process.argv = av;
-
-	ret = start_command(&column_process);
-
-	strbuf_release(&sb_colopt);
-	strbuf_release(&sb_width);
-	strbuf_release(&sb_padding);
 
-	if (ret)
+	if (start_command(&column_process))
 		return -2;
 
 	fd_out = dup(1);
-- 
2.0.0.rc1.436.g03cb729
Previous: Jeff KingNext: Jeff King
Message 3 of 9 in “build argv_array into run-command”
  1. 0/6 build argv_array into run-commandJeff King, May 15, 2014
  2. 1/6 run-command: store an optional argv_arrayJeff King, May 15, 2014
  3. 2/6 run_column_filter: use argv_arrayJeff King, May 15, 2014
  4. 3/6 git_connect: use argv_arrayJeff King, May 15, 2014
  5. 4/6 get_helper: use run-command's internal argv_arrayJeff King, May 15, 2014
  6. 5/6 get_exporter: use argv_arrayJeff King, May 15, 2014
  7. 6/6 get_importer: use run-command's internal argv_arrayJeff King, May 15, 2014
  8. 7/6 argv-array: drop "detach" codeJeff King, May 15, 2014
  9. Junio C HamanoMay 15, 2014

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.