# [PATCH] copy_fd: close ifd on error

2 messages from 2005-12-27 to 2005-12-27. Participants: Sam Ravnborg, Junio C Hamano.
Thread: https://gitlist.dev/t/2937

## Sam Ravnborg, 2005-12-27 08:19

Subject: [PATCH] copy_fd: close ifd on error
Message-ID: <20051227081905.GA9946@mars.ravnborg.org>
URL: https://gitlist.dev/e/20051227081905.GA9946%40mars.ravnborg.org

```
In copy_fd when write fails we ought to close input file descriptor.

Signed-off-by: Sam Ravnborg <sam@ravnborg.org>
---

Found while looking at the source for inspiration for something else.
I did not see any error related to this.

	Sam

diff --git a/copy.c b/copy.c
index 7100eed..08a3d38 100644
--- a/copy.c
+++ b/copy.c
@@ -22,11 +22,14 @@ int copy_fd(int ifd, int ofd)
 				buf += written;
 				len -= written;
 			}
-			else if (!written)
+			else if (!written) {
+				close(ifd);
 				return error("copy-fd: write returned 0");
-			else
+			} else {
+				close(ifd);
 				return error("copy-fd: write returned %s",
 					     strerror(errno));
+			}
 		}
 	}
 	close(ifd);

```

## Junio C Hamano, 2005-12-27 09:02

Subject: Re: [PATCH] copy_fd: close ifd on error
Message-ID: <7vpsnic3pq.fsf@assigned-by-dhcp.cox.net>
URL: https://gitlist.dev/e/7vpsnic3pq.fsf%40assigned-by-dhcp.cox.net
In-Reply-To: <20051227081905.GA9946@mars.ravnborg.org>

```
Sam Ravnborg <sam@ravnborg.org> writes:

> In copy_fd when write fails we ought to close input file descriptor.
>
> Signed-off-by: Sam Ravnborg <sam@ravnborg.org>
> ---
>
> Found while looking at the source for inspiration for something else.
> I did not see any error related to this.

Thanks.  I think the only place this matters is git-local-fetch
which nobody uses these days where it can leak handful ifd.
All other users immediately exit upon seeing failure return from
the function.

Of course this would matter much when we do libification.

```
