Don't leak file descriptors from unavailable pack files.
If open_packed_git failed it may have been because the packfile actually exists and is readable, but some sort of verification did not pass. In this case open_packed_git left pack_fd filled in, as the file descriptor is valid. We don't want to leak the file descriptor, nor do we want to allow someone in the future to use this packed_git. Signed-off-by: Shawn O. Pearce <spearce@spearce.org> Signed-off-by: Junio C Hamano <junkio@cox.net>
Shawn O. Pearce committed
Feb 2, 2007 at 03:00 UTC
3cf8b462d2dbe78233bba5c0765ecaa2c6b6cd99
1 file changed
+16
-1
sha1_file.c
+16
-1
@@ -552,7 +552,11 @@ void unuse_pack(struct pack_window **w_cursor)
552
}
553
}
554
555
-static int open_packed_git(struct packed_git *p)
555
+/*
556
+ * Do not call this directly as this leaks p->pack_fd on error return;
557
+ * call open_packed_git() instead.
558
+ */
559
+static int open_packed_git_1(struct packed_git *p)
560
{
561
struct stat st;
562
struct pack_header hdr;
@@ -608,6 +612,17 @@ static int open_packed_git(struct packed_git *p)
612
return 0;
613
}
614
615
+static int open_packed_git(struct packed_git *p)
616
+{
617
+ if (!open_packed_git_1(p))
618
+ return 0;
619
+ if (p->pack_fd != -1) {
620
+ close(p->pack_fd);
621
+ p->pack_fd = -1;
622
+ }
623
+ return -1;
624
+}
625
+
626
static int in_window(struct pack_window *win, unsigned long offset)
627
{
628
/* We must promise at least 20 bytes (one hash) after the