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

[PATCH 2/4] avoid double close of descriptors handed to run_command

From
Jeff King <peff@peff.net>
Date
Jun 24, 2014, 09:45 UTC
Message-ID
<20140624094545.GB14514@sigill.intra.peff.net>
In-Reply-To
<20140624094217.GA14216@sigill.intra.peff.net>

When a file descriptor is given to run_command via the "in", "out", or "err" parameters, run_command takes ownership. The descriptor will be closed in the parent process whether the process is spawned successfully or not, and closing it again is wrong.

In practice this has not caused problems, because we usually close() right after start_command returns, meaning no other code has opened a descriptor in the meantime. So we just get EBADF and ignore it (rather than accidentally closing somebody else's descriptor!).

Signed-off-by: Jeff King <peff@peff.net>
---
I noticed the one in replace, and grepped around for other instances. I
think this is all of them. The only possible cases I didn't investigate
carefully were in send-pack/receive-pack. The logic there is quite
complicated, and I remember looking at and fixing close() issues there
not too long ago (e.g., 49ecfa1), so I assumed it was OK.
 builtin/replace.c | 2 --
 daemon.c          | 1 -
 2 files changed, 3 deletions(-)
diff --git a/builtin/replace.c b/builtin/replace.c
index 8507835..eb1d2ec 100644
--- a/builtin/replace.c
+++ b/builtin/replace.c
@@ -207,8 +207,6 @@ static void export_object(const unsigned char *sha1, const char *filename)
 
 	if (run_command(&cmd))
 		die("cat-file reported failure");
-
-	close(fd);
 }
 
 /*
diff --git a/daemon.c b/daemon.c
index f9c63e9..e1e424d 100644
--- a/daemon.c
+++ b/daemon.c
@@ -775,7 +775,6 @@ static void handle(int incoming, struct sockaddr *addr, socklen_t addrlen)
 		logerror("unable to fork");
 	else
 		add_child(&cld, addr, addrlen);
-	close(incoming);
 }
 
 static void child_handler(int signo)
-- 
2.0.0.566.gfe3e6b2
Previous: Jeff KingNext: Jeff King
Message 3 of 9 in “git replace --edit --raw”
  1. 0/4 git replace --edit --rawJeff King, Jun 24, 2014
  2. 1/4 replace: replace spaces with tabs in indentationJeff King, Jun 24, 2014
  3. 2/4 avoid double close of descriptors handed to run_commandJeff King, Jun 24, 2014
  4. 3/4 replace: use argv_array in export_objectJeff King, Jun 24, 2014
  5. 4/4 replace: add a --raw mode for --editJeff King, Jun 24, 2014
  6. Eric SunshineJun 25, 2014
  7. Jeff KingJun 25, 2014
  8. Junio C HamanoJun 25, 2014
  9. Jeff KingJun 26, 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.