From 4e801463c7d2b26de747d3684cf75b722b0594af Mon Sep 17 00:00:00 2001 From: "Juan F. Codagnone" Date: Wed, 24 Jan 2018 13:56:47 -0300 Subject: [PATCH] mailinfo: avoid segfault when can't open files If or files can't be opened, then mailinfo() returns an error before it even initializes mi->p_hdr_data or mi->s_hdr_data. When cmd_mailinfo() then calls clear_mailinfo(), we dereference the NULL pointers trying to free their contents. Signed-off-by: Juan F. Codagnone Reviewed-by: Jeff King Signed-off-by: Junio C Hamano --- mailinfo.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/mailinfo.c b/mailinfo.c index a489d9d0fb..36ec927b9e 100644 --- a/mailinfo.c +++ b/mailinfo.c @@ -1149,11 +1149,13 @@ void clear_mailinfo(struct mailinfo *mi) strbuf_release(&mi->inbody_header_accum); free(mi->message_id); - for (i = 0; mi->p_hdr_data[i]; i++) - strbuf_release(mi->p_hdr_data[i]); + if (mi->p_hdr_data) + for (i = 0; mi->p_hdr_data[i]; i++) + strbuf_release(mi->p_hdr_data[i]); free(mi->p_hdr_data); - for (i = 0; mi->s_hdr_data[i]; i++) - strbuf_release(mi->s_hdr_data[i]); + if (mi->s_hdr_data) + for (i = 0; mi->s_hdr_data[i]; i++) + strbuf_release(mi->s_hdr_data[i]); free(mi->s_hdr_data); while (mi->content < mi->content_top) {