# [PATCH 1/2] refs.c: release file descriptor on error return

3 messages from 2009-07-16 to 2009-07-16. Participants: Brandon Casey.
Thread: https://gitlist.dev/t/20137

## Brandon Casey, 2009-07-16 21:25

Subject: [PATCH 0/2] Two miscellaneous patches found while debugging 'next'
Message-ID: <W0GiRm9n1NEfjVB8xBotanSRY89hxjLPu3Wksn_EsZusMJjkHNIRf8JuQmUh43ny21xTKBn1Li8@cipher.nrlssc.navy.mil>
URL: https://gitlist.dev/e/W0GiRm9n1NEfjVB8xBotanSRY89hxjLPu3Wksn_EsZusMJjkHNIRf8JuQmUh43ny21xTKBn1Li8%40cipher.nrlssc.navy.mil

```
From: Brandon Casey <drafnel@gmail.com>

Noticed these two issues while debugging a segfault on next.
Patch for 'next' to follow.

-brandon


Brandon Casey (2):
  refs.c: release file descriptor on error return
  sha1_name.c: avoid unnecessary strbuf_release

 refs.c      |    4 +++-
 sha1_name.c |    2 --
 2 files changed, 3 insertions(+), 3 deletions(-)

```

## Brandon Casey, 2009-07-16 21:25

Subject: [PATCH 1/2] refs.c: release file descriptor on error return
Message-ID: <W0GiRm9n1NEfjVB8xBotapRpIoR0jr6qkttRpkkGMPpe-bfqtvLM7YBvkMsGo7-vheQxRPBUAEU@cipher.nrlssc.navy.mil>
URL: https://gitlist.dev/e/W0GiRm9n1NEfjVB8xBotapRpIoR0jr6qkttRpkkGMPpe-bfqtvLM7YBvkMsGo7-vheQxRPBUAEU%40cipher.nrlssc.navy.mil
In-Reply-To: <W0GiRm9n1NEfjVB8xBotanSRY89hxjLPu3Wksn_EsZusMJjkHNIRf8JuQmUh43ny21xTKBn1Li8@cipher.nrlssc.navy.mil>

```
From: Brandon Casey <drafnel@gmail.com>


Signed-off-by: Brandon Casey <drafnel@gmail.com>
---
 refs.c |    4 +++-
 1 files changed, 3 insertions(+), 1 deletions(-)

diff --git a/refs.c b/refs.c
index dffe395..e15880f 100644
--- a/refs.c
+++ b/refs.c
@@ -1525,8 +1525,10 @@ int for_each_recent_reflog_ent(const char *ref, each_reflog_ent_fn fn, long ofs,
 		if (fstat(fileno(logfp), &statbuf) ||
 		    statbuf.st_size < ofs ||
 		    fseek(logfp, -ofs, SEEK_END) ||
-		    fgets(buf, sizeof(buf), logfp))
+		    fgets(buf, sizeof(buf), logfp)) {
+			fclose(logfp);
 			return -1;
+		}
 	}
 
 	while (fgets(buf, sizeof(buf), logfp)) {
-- 
1.6.3.1.24.g152f4

```

## Brandon Casey, 2009-07-16 21:25

Subject: [PATCH 2/2] sha1_name.c: avoid unnecessary strbuf_release
Message-ID: <W0GiRm9n1NEfjVB8xBotan7WQ5BVS-0Yy_3hikrJN4b57ORvs0sreBFZFVMQljOVkzTQg_oO16A@cipher.nrlssc.navy.mil>
URL: https://gitlist.dev/e/W0GiRm9n1NEfjVB8xBotan7WQ5BVS-0Yy_3hikrJN4b57ORvs0sreBFZFVMQljOVkzTQg_oO16A%40cipher.nrlssc.navy.mil
In-Reply-To: <W0GiRm9n1NEfjVB8xBotapRpIoR0jr6qkttRpkkGMPpe-bfqtvLM7YBvkMsGo7-vheQxRPBUAEU@cipher.nrlssc.navy.mil>

```
From: Brandon Casey <drafnel@gmail.com>

When we fall back to a standard for_each_reflog_ent() after failing to find
the nth branch switch (or if we had a short reflog) with the call to
for_each_recent_reflog_ent(), we do not need to free the memory allocated
for our strbuf's since a strbuf_reset() will be performed in
grab_nth_branch_switch() before assigning to the entry.

Plus, the strbuf_release() negates the non-zero hint we initially gave to
strbuf_init() just above these lines.

Signed-off-by: Brandon Casey <drafnel@gmail.com>
---
 sha1_name.c |    2 --
 1 files changed, 0 insertions(+), 2 deletions(-)

diff --git a/sha1_name.c b/sha1_name.c
index 904bcd9..44bb62d 100644
--- a/sha1_name.c
+++ b/sha1_name.c
@@ -777,8 +777,6 @@ int interpret_branch_name(const char *name, struct strbuf *buf)
 	for_each_recent_reflog_ent("HEAD", grab_nth_branch_switch, 40960, &cb);
 	if (cb.cnt < nth) {
 		cb.cnt = 0;
-		for (i = 0; i < nth; i++)
-			strbuf_release(&cb.buf[i]);
 		for_each_reflog_ent("HEAD", grab_nth_branch_switch, &cb);
 	}
 	if (cb.cnt < nth)
-- 
1.6.3.1.24.g152f4

```
