$OpenBSD: patch-imap_util_c,v 1.2 2010/08/09 09:54:15 dcoppa Exp $

add hcache debug, re ticket #3296

fix bug in imap_keepalive() which erroneously free Context when the 
IMAP connection is shut down by the server.

--- imap/util.c.orig	Wed Jun 10 06:17:20 2009
+++ imap/util.c	Mon Aug  9 11:37:57 2010
@@ -129,6 +129,8 @@ HEADER* imap_hcache_get (IMAP_DATA* idata, unsigned in
   {
     if (*uv == idata->uid_validity)
       h = mutt_hcache_restore ((unsigned char*)uv, NULL);
+    else
+      dprint (3, (debugfile, "hcache uidvalidity mismatch: %u", *uv));
     FREE (&uv);
   }
 
@@ -731,6 +733,8 @@ void imap_keepalive (void)
   {
     if (conn->account.type == M_ACCT_TYPE_IMAP)
     {
+      int need_free = 0;
+
       idata = (IMAP_DATA*) conn->data;
 
       if (idata->state >= IMAP_AUTHENTICATED
@@ -742,9 +746,17 @@ void imap_keepalive (void)
 	{
 	  ctx = safe_calloc (1, sizeof (CONTEXT));
 	  ctx->data = idata;
+          /* imap_close_mailbox will set ctx->iadata->ctx to NULL, so we can't
+           * rely on the value of iadata->ctx to determine if this placeholder
+           * context needs to be freed.
+           */
+          need_free = 1;
 	}
+        /* if the imap connection closes during this call, ctx may be invalid
+         * after this point, and thus should not be read.
+         */
 	imap_check_mailbox (ctx, NULL, 1);
-	if (!idata->ctx)
+	if (need_free)
 	  FREE (&ctx);
       }
     }
