summaryrefslogtreecommitdiff
path: root/refs
diff options
context:
space:
mode:
authorLibravatar René Scharfe <l.s.r@web.de>2017-04-16 18:55:46 +0200
committerLibravatar Junio C Hamano <gitster@pobox.com>2017-04-17 17:37:10 -0700
commitbe686f03e0f4c4f14f1d4ae9b1b35836168a0a4b (patch)
tree2c362df53918fca9ec5db1e3fdb535e2eccc9ea6 /refs
parentam: close stream on error, but not stdin (diff)
downloadtgif-be686f03e0f4c4f14f1d4ae9b1b35836168a0a4b.tar.xz
files_for_each_reflog_ent_reverse(): close stream and free strbuf on error
Exit the loop orderly through the cleanup code, instead of dashing out with logfp still open and sb leaking. Found with Cppcheck. Signed-off-by: Rene Scharfe <l.s.r@web.de> Reviewed-by: Jeff King <peff@peff.net> Reviewed-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'refs')
-rw-r--r--refs/files-backend.c20
1 files changed, 12 insertions, 8 deletions
diff --git a/refs/files-backend.c b/refs/files-backend.c
index c041d4ba21..10ba254941 100644
--- a/refs/files-backend.c
+++ b/refs/files-backend.c
@@ -3169,8 +3169,8 @@ static int files_for_each_reflog_ent_reverse(struct ref_store *ref_store,
/* Jump to the end */
if (fseek(logfp, 0, SEEK_END) < 0)
- return error("cannot seek back reflog for %s: %s",
- refname, strerror(errno));
+ ret = error("cannot seek back reflog for %s: %s",
+ refname, strerror(errno));
pos = ftell(logfp);
while (!ret && 0 < pos) {
int cnt;
@@ -3180,13 +3180,17 @@ static int files_for_each_reflog_ent_reverse(struct ref_store *ref_store,
/* Fill next block from the end */
cnt = (sizeof(buf) < pos) ? sizeof(buf) : pos;
- if (fseek(logfp, pos - cnt, SEEK_SET))
- return error("cannot seek back reflog for %s: %s",
- refname, strerror(errno));
+ if (fseek(logfp, pos - cnt, SEEK_SET)) {
+ ret = error("cannot seek back reflog for %s: %s",
+ refname, strerror(errno));
+ break;
+ }
nread = fread(buf, cnt, 1, logfp);
- if (nread != 1)
- return error("cannot read %d bytes from reflog for %s: %s",
- cnt, refname, strerror(errno));
+ if (nread != 1) {
+ ret = error("cannot read %d bytes from reflog for %s: %s",
+ cnt, refname, strerror(errno));
+ break;
+ }
pos -= cnt;
scanp = endp = buf + cnt;