# [PATCH] For the sake of correctness, fix file descriptor leak.

3 messages from 2007-11-22 to 2007-11-25. Participants: André Goddard Rosa, Junio C Hamano.
Thread: https://gitlist.dev/t/10968

## André Goddard Rosa, 2007-11-22 00:59

Subject: [PATCH] For the sake of correctness, fix file descriptor leak.
Message-ID: <b8bf37780711211659v4fbd5936t29d0a0a2ff84f4b@mail.gmail.com>
URL: https://gitlist.dev/e/b8bf37780711211659v4fbd5936t29d0a0a2ff84f4b%40mail.gmail.com

```
Hi, all!

    Please cc: me as I'm not subscribed. I'm sending the patch inline
only for review, probably it is mangled.
    Please use the attached patch if you agree with it. Sorry about
sending it attached.

>From 9fba346aca7470633ee46848013051248493896c Mon Sep 17 00:00:00 2001
From: Andre Goddard Rosa <andre.goddard@gmail.com>
Date: Tue, 27 Nov 2007 10:16:22 -0200
Subject: [PATCH] For the sake of correctness, fix file descriptor leak.

Signed-off-by: Andre Goddard Rosa <andre.goddard@gmail.com>
---
 builtin-rerere.c |    4 +++-
 1 files changed, 3 insertions(+), 1 deletions(-)

diff --git a/builtin-rerere.c b/builtin-rerere.c
index 7449323..31766be 100644
--- a/builtin-rerere.c
+++ b/builtin-rerere.c
@@ -275,8 +275,10 @@ static int copy_file(const char *src, const char *dest)

 	if (!(in = fopen(src, "r")))
 		return error("Could not open %s", src);
-	if (!(out = fopen(dest, "w")))
+	if (!(out = fopen(dest, "w"))) {
+		fclose(in);
 		return error("Could not open %s", dest);
+	}
 	while ((count = fread(buffer, 1, sizeof(buffer), in)))
 		fwrite(buffer, 1, count, out);
 	fclose(in);
-- 
1.5.3.6.861.gd794-dirty


From 9fba346aca7470633ee46848013051248493896c Mon Sep 17 00:00:00 2001
From: Andre Goddard Rosa <andre.goddard@gmail.com>
Date: Tue, 27 Nov 2007 10:16:22 -0200
Subject: [PATCH] For the sake of correctness, fix file descriptor leak.

Signed-off-by: Andre Goddard Rosa <andre.goddard@gmail.com>
---
 builtin-rerere.c |    4 +++-
 1 files changed, 3 insertions(+), 1 deletions(-)

diff --git a/builtin-rerere.c b/builtin-rerere.c
index 7449323..31766be 100644
--- a/builtin-rerere.c
+++ b/builtin-rerere.c
@@ -275,8 +275,10 @@ static int copy_file(const char *src, const char *dest)
 
 	if (!(in = fopen(src, "r")))
 		return error("Could not open %s", src);
-	if (!(out = fopen(dest, "w")))
+	if (!(out = fopen(dest, "w"))) {
+		fclose(in);
 		return error("Could not open %s", dest);
+	}
 	while ((count = fread(buffer, 1, sizeof(buffer), in)))
 		fwrite(buffer, 1, count, out);
 	fclose(in);
-- 
1.5.3.6.861.gd794-dirty


```

## André Goddard Rosa, 2007-11-25 21:37

Subject: [Resend Trivial PATCH] For the sake of correctness, fix file descriptor leak.
Message-ID: <b8bf37780711251337q41e02304q8fd2654b1e83201@mail.gmail.com>
URL: https://gitlist.dev/e/b8bf37780711251337q41e02304q8fd2654b1e83201%40mail.gmail.com
In-Reply-To: <b8bf37780711211659v4fbd5936t29d0a0a2ff84f4b@mail.gmail.com>

```
Hi, all!

    Plug a file descriptor leak.

From 9fba346aca7470633ee46848013051248493896c Mon Sep 17 00:00:00 2001
From: Andre Goddard Rosa <andre.goddard@gmail.com>
Date: Tue, 27 Nov 2007 10:16:22 -0200
Subject: [PATCH] For the sake of correctness, fix file descriptor leak.

Signed-off-by: Andre Goddard Rosa <andre.goddard@gmail.com>
---
 builtin-rerere.c |    4 +++-
 1 files changed, 3 insertions(+), 1 deletions(-)

diff --git a/builtin-rerere.c b/builtin-rerere.c
index 7449323..31766be 100644
--- a/builtin-rerere.c
+++ b/builtin-rerere.c
@@ -275,8 +275,10 @@ static int copy_file(const char *src, const char *dest)

        if (!(in = fopen(src, "r")))
                return error("Could not open %s", src);
-       if (!(out = fopen(dest, "w")))
+       if (!(out = fopen(dest, "w"))) {
+               fclose(in);
                return error("Could not open %s", dest);
+       }
        while ((count = fread(buffer, 1, sizeof(buffer), in)))
                fwrite(buffer, 1, count, out);
        fclose(in);
--
1.5.3.6.861.gd794-dirty



-- 
[]s,
André Goddard


From 9fba346aca7470633ee46848013051248493896c Mon Sep 17 00:00:00 2001
From: Andre Goddard Rosa <andre.goddard@gmail.com>
Date: Tue, 27 Nov 2007 10:16:22 -0200
Subject: [PATCH] For the sake of correctness, fix file descriptor leak.

Signed-off-by: Andre Goddard Rosa <andre.goddard@gmail.com>
---
 builtin-rerere.c |    4 +++-
 1 files changed, 3 insertions(+), 1 deletions(-)

diff --git a/builtin-rerere.c b/builtin-rerere.c
index 7449323..31766be 100644
--- a/builtin-rerere.c
+++ b/builtin-rerere.c
@@ -275,8 +275,10 @@ static int copy_file(const char *src, const char *dest)
 
 	if (!(in = fopen(src, "r")))
 		return error("Could not open %s", src);
-	if (!(out = fopen(dest, "w")))
+	if (!(out = fopen(dest, "w"))) {
+		fclose(in);
 		return error("Could not open %s", dest);
+	}
 	while ((count = fread(buffer, 1, sizeof(buffer), in)))
 		fwrite(buffer, 1, count, out);
 	fclose(in);
-- 
1.5.3.6.861.gd794-dirty


```

## Junio C Hamano, 2007-11-25 22:29

Subject: Re: [Resend Trivial PATCH] For the sake of correctness, fix file descriptor leak.
Message-ID: <7vbq9ij6x8.fsf@gitster.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vbq9ij6x8.fsf%40gitster.siamese.dyndns.org
In-Reply-To: <b8bf37780711251337q41e02304q8fd2654b1e83201@mail.gmail.com>

```
The codepath you are touching will exit immediately after an error
return; I do not feel much urgency to it, although I do not think the
patch would _hurt_.

Please follow the established convention for patch submission (see
patches on the list from other people as examples).

```
