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>

René Scharfe committed Apr 16, 2017 at 18:55 UTC be686f03e0f4c4f14f1d4ae9b1b35836168a0a4b
1 file changed +12 -8
refs/files-backend.c
+12 -8
@@ -3169,8 +3169,8 @@ static int files_for_each_reflog_ent_reverse(struct ref_store *ref_store,
3169
3170 /* Jump to the end */
3171 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));
3174 pos = ftell(logfp);
3175 while (!ret && 0 < pos) {
3176 int cnt;
@@ -3180,13 +3180,17 @@ static int files_for_each_reflog_ent_reverse(struct ref_store *ref_store,
3180
3181 /* Fill next block from the end */
3182 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 + }
3188 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 + }
3194 pos -= cnt;
3195
3196 scanp = endp = buf + cnt;