Re: [PATCH 1/2] Don't leak file descriptors from unavailable pack files.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



"Shawn O. Pearce" <spearce@xxxxxxxxxxx> writes:

> Junio C Hamano <junkio@xxxxxxx> wrote:
>> I agree leaking fd is not nice, but I wonder if that should be
>> dealt with by the caller.
>> 
>> Originally it did not matter as open_packed_git() died, but
>> shouldn't it be closing the fd and marking p->pack_fd with -1, as
>> you made it return instead of die?
>
> Yea, I thought of that when I wrote that evil patch...
>
> But open_packed_git has 12 things that can cause it to return
> an error.  That's basically a rewrite of the function.  This was
> 4 lines added to the only caller which didn't die().

Well, you can let the compiler to do the job, like this,
perhaps, if you really want to be lazy.  There won't be any
overhead from the extra call level, as there is only one caller
of open_packed_git_1() which will be inlined ;-).

diff --git a/sha1_file.c b/sha1_file.c
index 2eff14a..45e410e 100644
--- a/sha1_file.c
+++ b/sha1_file.c
@@ -552,7 +552,11 @@ void unuse_pack(struct pack_window **w_cursor)
 	}
 }
 
-static int open_packed_git(struct packed_git *p)
+/*
+ * Do not call this directly as this leaks p->pack_fd on error return;
+ * call open_packed_git() instead.
+ */
+static int open_packed_git_1(struct packed_git *p)
 {
 	struct stat st;
 	struct pack_header hdr;
@@ -608,6 +612,17 @@ static int open_packed_git(struct packed_git *p)
 	return 0;
 }
 
+static int open_packed_git(struct packed_git *p)
+{
+	if (!open_packed_git_1(p))
+		return 0;
+	if (p->pack_fd != -1) {
+		close(p->pack_fd);
+		p->pack_fd = -1;
+	}
+	return -1;
+}
+
 static int in_window(struct pack_window *win, unsigned long offset)
 {
 	/* We must promise at least 20 bytes (one hash) after the

-
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]