Check for NULL mx_ops in mx.c
authorKevin McCarthy <kevin@8t8.us>
Wed, 21 Sep 2016 20:51:01 +0000 (22:51 +0200)
committerKevin McCarthy <kevin@8t8.us>
Wed, 21 Sep 2016 20:51:01 +0000 (22:51 +0200)
Eike Rathke reported this happening when in an IMAP index view the
underlying connection was terminated, ctx->mx_ops was NULL and thus
accessing ctx->mx_ops->check segfaulted.

Thanks also to Eike Rathke for the initial patch, for which I expanded
the checks to other functions.

mx.c

diff --git a/mx.c b/mx.c
index fbe82e4ca38d0cae230569603110a2b323fbd36a..8f1b397e51e3e5fea44a70a831bd49be8d8ae252 100644 (file)
--- a/mx.c
+++ b/mx.c
@@ -1241,7 +1241,7 @@ MESSAGE *mx_open_new_message (CONTEXT *dest, HEADER *hdr, int flags)
   ADDRESS *p = NULL;
   MESSAGE *msg;
 
-  if (!dest->mx_ops->open_new_msg)
+  if (!dest->mx_ops || !dest->mx_ops->open_new_msg)
   {
       dprint (1, (debugfile, "mx_open_new_message(): function unimplemented for mailbox type %d.\n",
               dest->magic));
@@ -1293,7 +1293,7 @@ MESSAGE *mx_open_new_message (CONTEXT *dest, HEADER *hdr, int flags)
 /* check for new mail */
 int mx_check_mailbox (CONTEXT *ctx, int *index_hint)
 {
-  if (!ctx)
+  if (!ctx || !ctx->mx_ops)
   {
     dprint (1, (debugfile, "mx_check_mailbox: null or invalid context.\n"));
     return -1;
@@ -1307,7 +1307,7 @@ MESSAGE *mx_open_message (CONTEXT *ctx, int msgno)
 {
   MESSAGE *msg;
 
-  if (!ctx->mx_ops->open_msg)
+  if (!ctx->mx_ops || !ctx->mx_ops->open_msg)
   {
     dprint (1, (debugfile, "mx_open_message(): function not implemented for mailbox type %d.\n", ctx->magic));
     return NULL;
@@ -1324,7 +1324,7 @@ MESSAGE *mx_open_message (CONTEXT *ctx, int msgno)
 
 int mx_commit_message (MESSAGE *msg, CONTEXT *ctx)
 {
-  if (!ctx->mx_ops->commit_msg)
+  if (!ctx->mx_ops || !ctx->mx_ops->commit_msg)
     return -1;
 
   if (!(msg->write && ctx->append))
@@ -1342,7 +1342,7 @@ int mx_close_message (CONTEXT *ctx, MESSAGE **msg)
 {
   int r = 0;
 
-  if (ctx->mx_ops->close_msg)
+  if (ctx->mx_ops && ctx->mx_ops->close_msg)
     r = ctx->mx_ops->close_msg (ctx, *msg);
 
   if ((*msg)->path)