From 4ea4db3b51fe9a060a0dd30cc61373c244efe673 Mon Sep 17 00:00:00 2001 From: Richard Russon Date: Wed, 19 Jun 2019 15:08:37 +0100 Subject: [PATCH] add more Context checks Check for the condition where the Context exists, but `Context->mailbox` is NULL. Fixes: #1747 --- index.c | 83 ++++++++++++++++++++++++++++++++++--------------------- monitor.c | 4 +-- 2 files changed, 53 insertions(+), 34 deletions(-) diff --git a/index.c b/index.c index 7c25461da..96e39f330 100644 --- a/index.c +++ b/index.c @@ -245,7 +245,7 @@ static void collapse_all(struct Menu *menu, int toggle) struct MuttThread *thread = NULL, *top = NULL; int final; - if (!Context || (Context->mailbox->msg_count == 0)) + if (!Context || !Context->mailbox || (Context->mailbox->msg_count == 0)) return; /* Figure out what the current message would be after folding / unfolding, @@ -303,6 +303,9 @@ static void collapse_all(struct Menu *menu, int toggle) */ static int ci_next_undeleted(int msgno) { + if (!Context || !Context->mailbox) + return -1; + for (int i = msgno + 1; i < Context->mailbox->vcount; i++) if (!Context->mailbox->emails[Context->mailbox->v2r[i]]->deleted) return i; @@ -317,6 +320,9 @@ static int ci_next_undeleted(int msgno) */ static int ci_previous_undeleted(int msgno) { + if (!Context || !Context->mailbox) + return -1; + for (int i = msgno - 1; i >= 0; i--) if (!Context->mailbox->emails[Context->mailbox->v2r[i]]->deleted) return i; @@ -332,7 +338,7 @@ static int ci_previous_undeleted(int msgno) */ static int ci_first_message(void) { - if (!Context || (Context->mailbox->msg_count == 0)) + if (!Context || !Context->mailbox || (Context->mailbox->msg_count == 0)) return 0; int old = -1; @@ -405,6 +411,9 @@ static int mx_toggle_write(struct Mailbox *m) */ static void resort_index(struct Menu *menu) { + if (!Context || !Context->mailbox) + return; + struct Email *e = CUR_EMAIL; menu->current = -1; @@ -640,10 +649,10 @@ static int main_change_folder(struct Menu *menu, int op, struct Mailbox *m, } /* keepalive failure in mutt_enter_fname may kill connection. #3028 */ - if (Context && (mutt_buffer_is_empty(Context->mailbox->pathbuf))) + if (Context && Context->mailbox && (mutt_buffer_is_empty(Context->mailbox->pathbuf))) ctx_free(&Context); - if (Context) + if (Context && Context->mailbox) { char *new_last_folder = NULL; #ifdef USE_INOTIFY @@ -738,7 +747,8 @@ static int main_change_folder(struct Menu *menu, int op, struct Mailbox *m, */ void index_make_entry(char *buf, size_t buflen, struct Menu *menu, int line) { - if (!Context || !menu || (line < 0) || (line >= Context->mailbox->email_max)) + if (!Context || !Context->mailbox || !menu || (line < 0) || + (line >= Context->mailbox->email_max)) return; struct Email *e = Context->mailbox->emails[Context->mailbox->v2r[line]]; @@ -811,7 +821,7 @@ void index_make_entry(char *buf, size_t buflen, struct Menu *menu, int line) */ int index_color(int line) { - if (!Context || (line < 0)) + if (!Context || !Context->mailbox || (line < 0)) return 0; struct Email *e = Context->mailbox->emails[Context->mailbox->v2r[line]]; @@ -977,7 +987,8 @@ static void index_custom_redraw(struct Menu *menu) menu_redraw_sidebar(menu); #endif - if (Context && Context->mailbox->emails && !(menu->current >= Context->mailbox->vcount)) + if (Context && Context->mailbox && Context->mailbox->emails && + !(menu->current >= Context->mailbox->vcount)) { menu_check_recenter(menu); @@ -1038,12 +1049,13 @@ int mutt_index_menu(void) menu->menu_make_entry = index_make_entry; menu->menu_color = index_color; menu->current = ci_first_message(); - menu->help = mutt_compile_help(helpstr, sizeof(helpstr), MENU_MAIN, + menu->help = mutt_compile_help( + helpstr, sizeof(helpstr), MENU_MAIN, #ifdef USE_NNTP - (Context && (Context->mailbox->magic == MUTT_NNTP)) ? - IndexNewsHelp : + (Context && Context->mailbox && (Context->mailbox->magic == MUTT_NNTP)) ? + IndexNewsHelp : #endif - IndexHelp); + IndexHelp); menu->menu_custom_redraw = index_custom_redraw; mutt_menu_push_current(menu); mutt_window_reflow(); @@ -1073,15 +1085,15 @@ int mutt_index_menu(void) /* check if we need to resort the index because just about * any 'op' below could do mutt_enter_command(), either here or * from any new menu launched, and change $sort/$sort_aux */ - if (OptNeedResort && Context && (Context->mailbox->msg_count != 0) && - (menu->current >= 0)) + if (OptNeedResort && Context && Context->mailbox && + (Context->mailbox->msg_count != 0) && (menu->current >= 0)) resort_index(menu); - menu->max = Context ? Context->mailbox->vcount : 0; - oldcount = Context ? Context->mailbox->msg_count : 0; + menu->max = (Context && Context->mailbox) ? Context->mailbox->vcount : 0; + oldcount = (Context && Context->mailbox) ? Context->mailbox->msg_count : 0; - if (OptRedrawTree && Context && (Context->mailbox->msg_count != 0) && - ((C_Sort & SORT_MASK) == SORT_THREADS)) + if (OptRedrawTree && Context && Context->mailbox && + (Context->mailbox->msg_count != 0) && ((C_Sort & SORT_MASK) == SORT_THREADS)) { mutt_draw_tree(Context); menu->redraw |= REDRAW_STATUS; @@ -1091,7 +1103,7 @@ int mutt_index_menu(void) if (Context) Context->menu = menu; - if (Context && !attach_msg) + if (Context && Context->mailbox && !attach_msg) { int check; /* check for new mail in the mailbox. If nonzero, then something has @@ -1713,7 +1725,7 @@ int mutt_index_menu(void) { int check; - oldcount = Context ? Context->mailbox->msg_count : 0; + oldcount = (Context && Context->mailbox) ? Context->mailbox->msg_count : 0; mutt_startup_shutdown_hook(MUTT_SHUTDOWN_HOOK); notify_send(NeoMutt->notify, NT_GLOBAL, NT_GLOBAL_SHUTDOWN, 0); @@ -1753,7 +1765,7 @@ int mutt_index_menu(void) case OP_SORT_REVERSE: if (mutt_select_sort((op == OP_SORT_REVERSE)) == 0) { - if (Context && (Context->mailbox->msg_count != 0)) + if (Context && Context->mailbox && (Context->mailbox->msg_count != 0)) { resort_index(menu); OptSearchInvalid = true; @@ -1848,12 +1860,12 @@ int mutt_index_menu(void) #ifdef USE_IMAP case OP_MAIN_IMAP_FETCH: - if (Context && (Context->mailbox->magic == MUTT_IMAP)) + if (Context && Context->mailbox && (Context->mailbox->magic == MUTT_IMAP)) imap_check_mailbox(Context->mailbox, true); break; case OP_MAIN_IMAP_LOGOUT_ALL: - if (Context && (Context->mailbox->magic == MUTT_IMAP)) + if (Context && Context->mailbox && (Context->mailbox->magic == MUTT_IMAP)) { int check = mx_mbox_close(&Context); if (check != 0) @@ -1873,7 +1885,7 @@ int mutt_index_menu(void) #endif case OP_MAIN_SYNC_FOLDER: - if (Context && (Context->mailbox->msg_count == 0)) + if (!Context || !Context->mailbox || (Context->mailbox->msg_count == 0)) break; if (!prereq(Context, menu, CHECK_IN_MAILBOX | CHECK_MSGCOUNT | CHECK_READONLY)) @@ -2018,7 +2030,9 @@ int mutt_index_menu(void) case OP_MAIN_MODIFY_TAGS: case OP_MAIN_MODIFY_TAGS_THEN_HIDE: { - if (!Context || !mx_tags_is_supported(Context->mailbox)) + if (!Context || !Context->mailbox) + break; + if (!mx_tags_is_supported(Context->mailbox)) { mutt_message(_("Folder doesn't support tagging, aborting")); break; @@ -2225,7 +2239,7 @@ int mutt_index_menu(void) cp = _("Open mailbox"); if ((op == OP_MAIN_NEXT_UNREAD_MAILBOX) && Context && - !mutt_buffer_is_empty(Context->mailbox->pathbuf)) + Context->mailbox && !mutt_buffer_is_empty(Context->mailbox->pathbuf)) { mutt_buffer_strcpy(folderbuf, mutt_b2s(Context->mailbox->pathbuf)); mutt_buffer_pretty_mailbox(folderbuf); @@ -2250,7 +2264,7 @@ int mutt_index_menu(void) #endif else { - if (C_ChangeFolderNext && Context && + if (C_ChangeFolderNext && Context && Context->mailbox && mutt_buffer_is_empty(Context->mailbox->pathbuf)) { mutt_buffer_strcpy(folderbuf, mutt_b2s(Context->mailbox->pathbuf)); @@ -2306,9 +2320,11 @@ int mutt_index_menu(void) &oldcount, &index_hint); #ifdef USE_NNTP /* mutt_mailbox_check() must be done with mail-reader mode! */ - menu->help = mutt_compile_help( - helpstr, sizeof(helpstr), MENU_MAIN, - (Context && (Context->mailbox->magic == MUTT_NNTP)) ? IndexNewsHelp : IndexHelp); + menu->help = mutt_compile_help(helpstr, sizeof(helpstr), MENU_MAIN, + (Context && Context->mailbox && + (Context->mailbox->magic == MUTT_NNTP)) ? + IndexNewsHelp : + IndexHelp); #endif mutt_buffer_expand_path(folderbuf); #ifdef USE_SIDEBAR @@ -2990,7 +3006,10 @@ int mutt_index_menu(void) } case OP_CREATE_ALIAS: - mutt_alias_create(Context && Context->mailbox->vcount ? CUR_EMAIL->env : NULL, NULL); + mutt_alias_create(Context && Context->mailbox && Context->mailbox->vcount ? + CUR_EMAIL->env : + NULL, + NULL); menu->redraw |= REDRAW_CURRENT; break; @@ -3430,7 +3449,7 @@ int mutt_index_menu(void) /* fallthrough */ case OP_POST: - if (!prereq(Context, menu, CHECK_ATTACH)) + if (!prereq(Context, menu, CHECK_IN_MAILBOX | CHECK_ATTACH)) break; if ((op != OP_FOLLOWUP) || !CUR_EMAIL->env->followup_to || (mutt_str_strcasecmp(CUR_EMAIL->env->followup_to, "poster") != 0) || @@ -3683,7 +3702,7 @@ int mutt_reply_observer(struct NotifyCallback *nc) if (mutt_str_strcmp(ec->name, "reply_regex") != 0) return 0; - if (!Context) + if (!Context || !Context->mailbox) return 0; regmatch_t pmatch[1]; diff --git a/monitor.c b/monitor.c index 62e3559d4..11e874d09 100644 --- a/monitor.c +++ b/monitor.c @@ -320,7 +320,7 @@ static enum ResolveResult monitor_resolve(struct MonitorInfo *info, struct Mailb info->magic = m->magic; info->path = m->realpath; } - else if (Context) + else if (Context && Context->mailbox) { info->magic = Context->mailbox->magic; info->path = Context->mailbox->realpath; @@ -525,7 +525,7 @@ int mutt_monitor_remove(struct Mailbox *m) goto cleanup; } - if (Context) + if (Context && Context->mailbox) { if (m) { -- 2.49.0