diff -u -p -Nr --exclude CVS mailutils-20020409.orig/imap4d/fetch.c mailutils-20020409/imap4d/fetch.c
--- mailutils-20020409.orig/imap4d/fetch.c	2001-12-16 16:59:13.000000000 +0100
+++ mailutils-20020409/imap4d/fetch.c	2005-05-27 07:57:36.000000000 +0200
@@ -138,7 +138,7 @@ int
 imap4d_fetch0 (char *arg, int isuid, char *resp, size_t resplen)
 {
   struct fetch_command *fcmd = NULL;
-  int rc = RESP_NO;
+  int rc = RESP_OK;
   char *sp = NULL;
   char *msgset;
   size_t *set = NULL;
@@ -161,7 +161,8 @@ imap4d_fetch0 (char *arg, int isuid, cha
       return RESP_BAD;
     }
 
-  for (i = 0; i < n; i++)
+  snprintf (resp, resplen, "Completed");
+  for (i = 0; i < n && rc == RESP_OK; i++)
     {
       char item[32];
       char *items = strdup (sp);
@@ -209,12 +210,18 @@ imap4d_fetch0 (char *arg, int isuid, cha
 		}
 	    }
 	  util_send (")\r\n");
+	  free (p);
+	}
+      else
+	{
+	  snprintf (resp, resplen,
+		    "Bogus message set: message number out of range");
+	  rc = RESP_BAD;
+	  break;
 	}
-      free (p);
     }
   free (set);
-  snprintf (resp, resplen, "Completed");
-  return RESP_OK;
+  return rc;
 }
 
 /* ALL:
@@ -242,8 +249,7 @@ fetch_full (struct fetch_command *comman
   fetch_all (command, arg);
   util_send (" ");
   c_body.msg = command->msg;
-  fetch_body (&c_body, arg);
-  return RESP_OK;
+  return fetch_body (&c_body, arg);
 }
 
 /* FAST:
@@ -386,8 +392,7 @@ fetch_rfc822_header (struct fetch_comman
   char *p = buffer;
   (void)arg; /* No arguments.  */
   strcpy (buffer, ".PEEK[HEADER]");
-  fetch_body (command, &p);
-  return RESP_OK;
+  return fetch_body (command, &p);
 }
 
 /* RFC822.TEXT:
@@ -400,8 +405,7 @@ fetch_rfc822_text (struct fetch_command 
   char *p = buffer;
   (void)arg; /* No arguments.  */
   strcpy (buffer, "[TEXT]");
-  fetch_body (command, &p);
-  return RESP_OK;
+  return fetch_body (command, &p);
 }
 
 /* The [RFC-822] size of the message.  */
@@ -933,7 +937,8 @@ fetch_operation (message_t msg, char **a
   unsigned long end = ULONG_MAX; /* No limit. */
   char *section; /* Hold the section number string.  */
   char *partial = strchr (*arg, '<');
-
+  int rc;
+  
   /* Check for section specific offset.  */
   if (partial)
     {
@@ -987,13 +992,15 @@ fetch_operation (message_t msg, char **a
   else
     section = calloc (1, 1);
 
+  rc = RESP_OK;
+  
   /* Choose the right fetch attribute.  */
   if (*section == '\0' && **arg == ']')
     {
       if (!silent)
 	util_send ("[]");
       (*arg)++;
-      fetch_message (msg, start, end);
+      rc = fetch_message (msg, start, end);
     }
   else if (strncasecmp (*arg, "HEADER]", 7) == 0)
     {
@@ -1010,7 +1017,7 @@ fetch_operation (message_t msg, char **a
 	    util_send ("[%s", *arg);
 	}
       (*arg) += 7;
-      fetch_header (msg, start, end);
+      rc = fetch_header (msg, start, end);
     }
   else if (strncasecmp (*arg, "MIME]", 5) == 0)
     {
@@ -1022,7 +1029,7 @@ fetch_operation (message_t msg, char **a
 	    util_send ("[%s", *arg);
 	}
       (*arg) += 5;
-      fetch_header (msg, start, end);
+      rc = fetch_header (msg, start, end);
     }
   else if (strncasecmp (*arg, "HEADER.FIELDS.NOT", 17) == 0)
     {
@@ -1033,7 +1040,7 @@ fetch_operation (message_t msg, char **a
       else
 	util_send ("[");
       (*arg) += 17;
-      fetch_header_fields_not (msg, arg, start, end);
+      rc = fetch_header_fields_not (msg, arg, start, end);
     }
   else if (strncasecmp (*arg, "HEADER.FIELDS", 13) == 0)
     {
@@ -1044,7 +1051,7 @@ fetch_operation (message_t msg, char **a
       else
 	util_send ("[");
       (*arg) += 13;
-      fetch_header_fields (msg, arg, start, end);
+      rc = fetch_header_fields (msg, arg, start, end);
     }
   /* Last case check also for section to catch "[1.2.3]" submessages.  */
   else if (strncasecmp (*arg, "TEXT]", 5) == 0 || section)
@@ -1060,12 +1067,15 @@ fetch_operation (message_t msg, char **a
 	(*arg)++;
       else
 	(*arg) += 5;
-      fetch_body_content (msg, start, end);
+      rc = fetch_body_content (msg, start, end);
     }
   else
+    {
     util_send (" \"\"");
+    rc = RESP_BAD;
+    }
   free (section);
-  return RESP_OK;
+  return rc;
 }
 
 static int
