From cf05da96a33c44d3ba9fd01e75a4fff514ae9c3a Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Mon, 17 Apr 2017 13:42:44 +0200 Subject: [PATCH] get_mail_commit_oid(): avoid resource leak When we fail to read, or parse, the file, we still want to close the file descriptor and release the strbuf. Reported via Coverity. Signed-off-by: Johannes Schindelin --- builtin/am.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/builtin/am.c b/builtin/am.c index 31fb60578f..a9c6d7088e 100644 --- a/builtin/am.c +++ b/builtin/am.c @@ -1355,15 +1355,16 @@ static int get_mail_commit_oid(struct object_id *commit_id, const char *mail) struct strbuf sb = STRBUF_INIT; FILE *fp = xfopen(mail, "r"); const char *x; + int ret = 0; if (strbuf_getline_lf(&sb, fp)) - return -1; + ret = -1; - if (!skip_prefix(sb.buf, "From ", &x)) - return -1; + if (!ret && !skip_prefix(sb.buf, "From ", &x)) + ret = -1; - if (get_oid_hex(x, commit_id) < 0) - return -1; + if (!ret && get_oid_hex(x, commit_id) < 0) + ret = -1; strbuf_release(&sb); fclose(fp);