387f7fe
diff -up mutt-1.5.21/imap/message.c.hdrcnt mutt-1.5.21/imap/message.c
387f7fe
--- mutt-1.5.21/imap/message.c.hdrcnt	2010-08-24 18:34:21.000000000 +0200
42dbf21
+++ mutt-1.5.21/imap/message.c	2011-06-13 15:44:08.268380854 +0200
387f7fe
@@ -65,7 +65,7 @@ int imap_read_headers (IMAP_DATA* idata,
387f7fe
   char *hdrreq = NULL;
387f7fe
   FILE *fp;
387f7fe
   char tempfile[_POSIX_PATH_MAX];
387f7fe
-  int msgno, idx;
8056a91
+  int msgno, idx = msgbegin - 1;
387f7fe
   IMAP_HEADER h;
387f7fe
   IMAP_STATUS* status;
387f7fe
   int rc, mfhrc, oldmsgcount;
387f7fe
@@ -185,7 +185,7 @@ int imap_read_headers (IMAP_DATA* idata,
387f7fe
           continue;
387f7fe
         }
387f7fe
 
387f7fe
-        idx = h.sid - 1;
387f7fe
+        idx++;
387f7fe
         ctx->hdrs[idx] = imap_hcache_get (idata, h.data->uid);
387f7fe
         if (ctx->hdrs[idx])
387f7fe
         {
42dbf21
@@ -211,6 +211,7 @@ int imap_read_headers (IMAP_DATA* idata,
42dbf21
           dprint (3, (debugfile, "bad cache entry at %d, giving up\n", h.sid - 1));
42dbf21
           imap_free_header_data((void**) (void*) &h.data);
42dbf21
           evalhc = 0;
42dbf21
+          idx--;
42dbf21
         }
42dbf21
       }
42dbf21
       while (rc != IMAP_CMD_OK && mfhrc == -1);
42dbf21
@@ -273,18 +274,20 @@ int imap_read_headers (IMAP_DATA* idata,
387f7fe
       {
387f7fe
         dprint (2, (debugfile, "msg_fetch_header: ignoring fetch response with no body\n"));
387f7fe
         mfhrc = -1;
387f7fe
+        msgend--;
387f7fe
         continue;
387f7fe
       }
387f7fe
 
387f7fe
       /* make sure we don't get remnants from older larger message headers */
387f7fe
       fputs ("\n\n", fp);
387f7fe
 
387f7fe
-      idx = h.sid - 1;
387f7fe
+      idx++;
387f7fe
       if (idx > msgend)
387f7fe
       {
387f7fe
         dprint (1, (debugfile, "imap_read_headers: skipping FETCH response for "
42dbf21
                     "unknown message number %d\n", h.sid));
42dbf21
         mfhrc = -1;
42dbf21
+        idx--;
42dbf21
         continue;
42dbf21
       }
42dbf21
       /* May receive FLAGS updates in a separate untagged response (#2935) */
42dbf21
@@ -292,6 +295,7 @@ int imap_read_headers (IMAP_DATA* idata,
42dbf21
       {
42dbf21
 	dprint (2, (debugfile, "imap_read_headers: message %d is not new\n",
42dbf21
 		    h.sid));
42dbf21
+        idx--;
42dbf21
 	continue;
42dbf21
       }
42dbf21