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

[PATCH 2/2] git: ensure 0/1/2 are open in main()

From
Thomas Rast <trast@inf.ethz.ch>
Date
Jul 16, 2013, 09:27 UTC
Message-ID
<65e2cdb28e76de8f669f3d58556fa9b8da944c5b.1373966758.git.trast@inf.ethz.ch>
In-Reply-To
<1fe4ff3df32aa1836ed3672e749f582047745bc9.1373966758.git.trast@inf.ethz.ch>

Not having an open FD in the 0--2 range can lead to strange results, for example, a subsequent open() may return 2 (stderr) and then a die() would clobber this file.

git-daemon and git-shell already guarded against this, but apparently users also manage to trip over it in other git commands. So we call sanitize_stdfds() during main git startup.

Since these FDs are inherited, this covers all use of 'git foo ...', and all internal C commands when called directly. It does not fix shell/perl commands called directly.

Signed-off-by: Thomas Rast <trast@inf.ethz.ch>
---
 git.c | 7 +++++++
 1 file changed, 7 insertions(+)
diff --git a/git.c b/git.c
index 4359086..6104d5e 100644
--- a/git.c
+++ b/git.c
@@ -525,6 +525,13 @@ int main(int argc, char **av)
 	if (!cmd)
 		cmd = "git-help";
 
+	/*
+	 * Always open file descriptors 0/1/2 to avoid clobbering files
+	 * in die().  It also avoids messing up when the pipes are dup'ed
+	 * onto stdin/stdout/stderr in the child processes we spawn.
+	 */
+	sanitize_stdfds();
+
 	git_setup_gettext();
 
 	/*
-- 
1.8.3.2.998.g1d087bc
Previous: Thomas Rast
Message 2 of 2 in “daemon/shell: refactor redirection of 0/1/2 from /dev/null”
  1. 1/2 daemon/shell: refactor redirection of 0/1/2 from /dev/nullThomas Rast, Jul 16, 2013
  2. 2/2 git: ensure 0/1/2 are open in main()Thomas Rast, Jul 16, 2013

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.