[balsa] Improve IMAP error handling, mostly related to compression.



commit 187b835035e465b670dcbbff8062a95f02d38521
Author: Pawel Salek <pawsa damage localdomain>
Date:   Fri Jan 8 19:52:38 2010 +0100

    Improve IMAP error handling, mostly related to compression.
    
    * libbalsa/imap/imap_compress.c: improve error handling.
    * libbalsa/mailbox_imap.c: catch error conditions early.

 ChangeLog                     |    5 +++++
 libbalsa/imap/imap_compress.c |   19 ++++++++++---------
 libbalsa/mailbox_imap.c       |   27 +++++++++++++++++++++------
 3 files changed, 36 insertions(+), 15 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index fc05906..2a9cdf8 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2010-01-08  Pawel Salek
+
+	* libbalsa/imap/imap_compress.c: improve error handling.
+	* libbalsa/mailbox_imap.c: catch error conditions early.
+
 2010-01-05  Pawel Salek
 
 	* libbalsa/imap/siobuf.[ch]: change encode interface so that
diff --git a/libbalsa/imap/imap_compress.c b/libbalsa/imap/imap_compress.c
index 7c9d8de..d919730 100644
--- a/libbalsa/imap/imap_compress.c
+++ b/libbalsa/imap/imap_compress.c
@@ -49,12 +49,13 @@ imap_compress_cb(char **dstbuf, int *dstlen,
   err = deflate(&icb->out_stream, Z_SYNC_FLUSH);
   if ( !(err == Z_OK || err == Z_STREAM_END || err == Z_BUF_ERROR) ) {
     fprintf(stderr, "deflate error1 %d\n", err);
-    /* FIXME - break the connection here, no point in continuing. */
+    *dstlen = 0;
+  } else {
+    *dstlen = IMAP_COMPRESS_BUFFER_SIZE - icb->out_stream.avail_out;
+    /* printf("imap_compress_cb %d bytes to %d\n", srclen, *dstlen); */
+    icb->out_compressed += *dstlen;
   }
 
-  *dstlen = IMAP_COMPRESS_BUFFER_SIZE - icb->out_stream.avail_out;
-  /* printf("imap_compress_cb %d bytes to %d\n", srclen, *dstlen); */
-  icb->out_compressed += *dstlen;
   return *dstlen;
 }
 
@@ -79,12 +80,12 @@ imap_decompress_cb(char **dstbuf, int *dstlen,
   
   if (!(err == Z_OK || err == Z_BUF_ERROR || err == Z_STREAM_END)) {
     fprintf(stderr, "inflate error %d\n", err);
-    /* FIXME break the connection. */
+    *dstlen = -1;
+  } else {
+    *dstlen = IMAP_COMPRESS_BUFFER_SIZE - icb->in_stream.avail_out;
+    /* printf("imap_decompress_cb %d bytes to %d\n", srclen, *dstlen); */
+    icb->in_uncompressed += *dstlen;
   }
-
-  *dstlen = IMAP_COMPRESS_BUFFER_SIZE - icb->in_stream.avail_out;
-  /* printf("imap_decompress_cb %d bytes to %d\n", srclen, *dstlen); */
-  icb->in_uncompressed += *dstlen;
   return *dstlen;
 }
 
diff --git a/libbalsa/mailbox_imap.c b/libbalsa/mailbox_imap.c
index 8ddc4d6..da01e20 100644
--- a/libbalsa/mailbox_imap.c
+++ b/libbalsa/mailbox_imap.c
@@ -1126,12 +1126,17 @@ save_to(unsigned seqno, const char *buf, size_t buflen, void* arg)
 static FILE*
 get_cache_stream(LibBalsaMailbox *mailbox, guint msgno, gboolean peek)
 {
-    unsigned uid = IMAP_MSGNO_UID(mailbox, msgno);
+    unsigned uid;
     LibBalsaMailboxImap *mimap = LIBBALSA_MAILBOX_IMAP(mailbox);
     FILE *stream;
     gchar **pair, *path;
 
     g_assert(mimap->handle);
+    g_return_val_if_fail(msgno <=
+                         imap_mbox_handle_get_exists(mimap->handle),
+                         NULL);
+
+    uid = IMAP_MSGNO_UID(mailbox, msgno);
     pair = get_cache_name_pair(mimap, "body", uid);
     path = g_build_filename(pair[0], pair[1], NULL);
     stream = fopen(path, "rb");
@@ -1513,8 +1518,12 @@ GHashTable * libbalsa_mailbox_imap_get_matchings(LibBalsaMailboxImap* mbox,
 	for(msgs= LIBBALSA_MAILBOX(mbox)->message_list; msgs;
 	    msgs = msgs->next){
 	    LibBalsaMessage *m = LIBBALSA_MESSAGE(msgs->data);
-	    ImapUID uid = IMAP_MESSAGE_UID(m);
-	    g_hash_table_insert(cbdata->uids, GUINT_TO_POINTER(uid), m);
+
+            if (m->msgno <= imap_mbox_handle_get_exists(mbox->handle)) {
+                ImapUID uid = IMAP_MESSAGE_UID(m);
+                g_hash_table_insert(cbdata->uids, GUINT_TO_POINTER(uid), m);
+            } else
+                g_warning("Msg %d out of range\n", m->msgno);
 	}
 #else	
         g_warning("Search results ignored. Fixme!");
@@ -2087,9 +2096,11 @@ get_struct_from_cache(LibBalsaMailbox *mailbox, LibBalsaMessage *message,
         GMimeStream *stream, *fstream;
         GMimeFilter *filter;
         GMimeParser *mime_parser;
-
-        pair = get_cache_name_pair(LIBBALSA_MAILBOX_IMAP(mailbox), "body",
-                                   IMAP_MESSAGE_UID(message));
+        LibBalsaMailboxImap *mimap = LIBBALSA_MAILBOX_IMAP(mailbox);
+        g_return_val_if_fail(message->msgno <=
+                             imap_mbox_handle_get_exists(mimap->handle),
+                             FALSE);
+        pair = get_cache_name_pair(mimap, "body", IMAP_MESSAGE_UID(message));
 
         filename = g_build_filename(pair[0], pair[1], NULL);
         g_strfreev(pair);
@@ -2353,6 +2364,10 @@ lbm_imap_get_msg_part_from_cache(LibBalsaMessage * msg,
     FILE *fp;
     gchar *section;
 
+    g_return_val_if_fail(msg->msgno <=
+                         imap_mbox_handle_get_exists(mimap->handle),
+                         FALSE);
+
    /* look for a part cache */
     section = get_section_for(msg, part);
     pair = get_cache_name_pair(mimap, "part", IMAP_MESSAGE_UID(msg));



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]