Thread (40 messages) 40 messages, 5 authors, 7h ago

Re: [PATCH 02/13] loose: avoid closing invalid fd on error path

From: Patrick Steinhardt <hidden>
Date: 2026-07-01 07:56:28

On Wed, Jul 01, 2026 at 07:04:20AM +0000, Johannes Schindelin via GitGitGadget wrote:
quoted hunk ↗ jump to hunk
diff --git a/loose.c b/loose.c
index 47b7f5ec38..2c6db45245 100644
--- a/loose.c
+++ b/loose.c
@@ -202,7 +202,8 @@ static int write_one_object(struct odb_source_loose *loose,
 	return 0;
 errout:
 	error_errno(_("failed to write loose object index %s"), path.buf);
-	close(fd);
+	if (fd >= 0)
+		close(fd);
 	rollback_lock_file(&lock);
 	strbuf_release(&buf);
 	strbuf_release(&path);
Makes sense. At the time we hit the first `goto errout` we have already
assigned `fd = open(...)`, so we know it should be either negative or a
positive file descriptor.

There's also a second call to `close(fd)`, but if that call is
successful then we would not use the `errout` path. If it fails we may
try to close the file descriptor a second time, but that's probably a
non-issue.

Patrick
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help