Skip to content

Commit be686f0

Browse files
rscharfegitster
authored andcommitted
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 <[email protected]> Reviewed-by: Jeff King <[email protected]> Reviewed-by: Michael Haggerty <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ac8ce18 commit be686f0

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

refs/files-backend.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3169,8 +3169,8 @@ static int files_for_each_reflog_ent_reverse(struct ref_store *ref_store,
31693169

31703170
/* Jump to the end */
31713171
if (fseek(logfp, 0, SEEK_END) < 0)
3172-
return error("cannot seek back reflog for %s: %s",
3173-
refname, strerror(errno));
3172+
ret = error("cannot seek back reflog for %s: %s",
3173+
refname, strerror(errno));
31743174
pos = ftell(logfp);
31753175
while (!ret && 0 < pos) {
31763176
int cnt;
@@ -3180,13 +3180,17 @@ static int files_for_each_reflog_ent_reverse(struct ref_store *ref_store,
31803180

31813181
/* Fill next block from the end */
31823182
cnt = (sizeof(buf) < pos) ? sizeof(buf) : pos;
3183-
if (fseek(logfp, pos - cnt, SEEK_SET))
3184-
return error("cannot seek back reflog for %s: %s",
3185-
refname, strerror(errno));
3183+
if (fseek(logfp, pos - cnt, SEEK_SET)) {
3184+
ret = error("cannot seek back reflog for %s: %s",
3185+
refname, strerror(errno));
3186+
break;
3187+
}
31863188
nread = fread(buf, cnt, 1, logfp);
3187-
if (nread != 1)
3188-
return error("cannot read %d bytes from reflog for %s: %s",
3189-
cnt, refname, strerror(errno));
3189+
if (nread != 1) {
3190+
ret = error("cannot read %d bytes from reflog for %s: %s",
3191+
cnt, refname, strerror(errno));
3192+
break;
3193+
}
31903194
pos -= cnt;
31913195

31923196
scanp = endp = buf + cnt;

0 commit comments

Comments
 (0)