Coverity fixes
authorColin Leroy <colin@colino.net>
Fri, 6 Jun 2014 11:38:18 +0000 (13:38 +0200)
committerColin Leroy <colin@colino.net>
Fri, 6 Jun 2014 11:38:18 +0000 (13:38 +0200)
src/crash.c
src/matcher.c
src/matcher_parser_lex.l
src/mbox.c
src/messageview.c
src/mh.c
src/mimeview.c
src/msgcache.c
src/news.c
src/partial_download.c

index 5f3eaa9..d3c5022 100644 (file)
@@ -333,7 +333,10 @@ static void crash_debug(unsigned long crash_pid,
        int choutput[2];
        pid_t pid;
 
-       pipe(choutput);
+       if (pipe(choutput) == -1) {
+               g_print("can't pipe - error %s", errno);
+               return;
+       }
 
        if (0 == (pid = fork())) {
                char *argp[10];
index 8c1fd11..4382c59 100644 (file)
@@ -1531,7 +1531,7 @@ static gboolean matcherlist_match_binary_content(MatcherList *matchers, MimeInfo
        gchar buf[BUFFSIZE];
        GSList *l;
 
-       if (partinfo->type == MIMETYPE_TEXT)
+       if (!partinfo || partinfo->type == MIMETYPE_TEXT)
                return FALSE;
        else
                outfp = procmime_get_binary_content(partinfo);
@@ -1551,9 +1551,8 @@ static gboolean matcherlist_match_binary_content(MatcherList *matchers, MimeInfo
                        /* Don't scan non-text parts when looking in body, only
                         * when looking in whole message
                         */
-                       if (partinfo && partinfo->type != MIMETYPE_TEXT &&
-                       (matcher->criteria == MATCHCRITERIA_NOT_BODY_PART ||
-                       matcher->criteria == MATCHCRITERIA_BODY_PART))
+                       if (matcher->criteria == MATCHCRITERIA_NOT_BODY_PART ||
+                           matcher->criteria == MATCHCRITERIA_BODY_PART)
                                continue;
 
                        /* if the criteria is ~body_part or ~message, ZERO lines
index bf36c58..209e503 100644 (file)
@@ -90,7 +90,7 @@ void matcher_parser_init(void)
                if (!g_utf8_validate(string_buf, -1, NULL)) {
                        gchar *tmp = conv_codeset_strdup(string_buf, conv_get_locale_charset_str(), CS_INTERNAL);
                        if (tmp) {
-                               strcpy(string_buf, tmp);
+                               g_strlcpy(string_buf, tmp, sizeof(string_buf));
                                g_free(tmp);
                        }
                }
index 04f5108..6ef1cad 100644 (file)
@@ -380,6 +380,7 @@ gint lock_mbox(const gchar *base, LockType type)
 #if HAVE_FCNTL_H && !defined(G_OS_WIN32)
                if (fcntl(lockfd, F_SETLK, &fl) == -1) {
                        g_warning("can't fnctl %s (%s)", base, strerror(errno));
+                       close(lockfd);
                        return -1;
                } else {
                        fcntled = TRUE;
index 3acacd2..6618c8b 100644 (file)
@@ -836,7 +836,7 @@ static gint disposition_notification_send(MsgInfo *msginfo)
                ok = strcasecmp(to_addr, buf);
                g_free(to_addr);
        } else {
-               strncpy(buf, _("<No Return-Path found>"), 
+               g_strlcpy(buf, _("<No Return-Path found>"), 
                                sizeof(buf));
        }
        
@@ -1276,7 +1276,7 @@ gint messageview_show(MessageView *messageview, MsgInfo *msginfo,
 {
        gchar *text = NULL;
        gchar *file;
-       MimeInfo *mimeinfo, *encinfo, *brokeninfo, *root;
+       MimeInfo *mimeinfo, *encinfo, *root;
        gchar *subject = NULL;
        cm_return_val_if_fail(msginfo != NULL, -1);
 
@@ -1431,7 +1431,7 @@ gint messageview_show(MessageView *messageview, MsgInfo *msginfo,
                return_receipt_show(messageview->noticeview, 
                                    messageview->msginfo);
 
-       if ((brokeninfo = find_broken_part(mimeinfo)) != NULL) {
+       if (find_broken_part(mimeinfo) != NULL) {
                noticeview_set_icon(messageview->noticeview,
                                    STOCK_PIXMAP_NOTICE_WARN);
                if (!noticeview_is_visible(messageview->noticeview)) {
index 5b3bf72..e7b9888 100644 (file)
--- a/src/mh.c
+++ b/src/mh.c
@@ -859,8 +859,8 @@ static gchar *mh_item_get_path(Folder *folder, FolderItem *item)
        if (!is_dir_exist(real_path) && is_dir_exist(path)) {
                /* mmh, older version did put utf8 filenames instead of
                 * the correct encoding */
-               g_rename(path, real_path);
-               folder_item_scan(item);
+               if (g_rename(path, real_path) == 0)
+                       folder_item_scan(item);
        }
 
        g_free(path);
@@ -1354,8 +1354,10 @@ static void mh_write_sequences(FolderItem *item, gboolean remove_unseen)
                if (fclose(mh_sequences_new_fp) == EOF)
                        err = TRUE;
 
-               if (!err)
-                       g_rename(mh_sequences_new, mh_sequences_old);
+               if (!err) {
+                       if (g_rename(mh_sequences_new, mh_sequences_old) < 0)
+                               FILE_OP_ERROR(mh_sequences_new, "rename");
+               }
                g_free(sequence);
                procmsg_msg_list_free(msglist);
        }
index 61dd53e..8b20d5c 100644 (file)
@@ -1696,21 +1696,22 @@ static void mimeview_drag_data_get(GtkWidget        *widget,
                FILE *fp;
 
                fp = g_fopen(partinfo->data.filename, "rb");
-               fseek(fp, partinfo->offset, SEEK_SET);
-               headers = procheader_get_header_array_asis(fp);
-               if (headers) {
-                       gint i;
-                       for (i = 0; i < headers->len; i++) {
-                               Header *header = g_ptr_array_index(headers, i);
-                               if (procheader_headername_equal(header->name, "Subject")) {
-                                       unfold_line(header->body);
-                                       name = g_strconcat(header->body, ".txt", NULL);
-                                       subst_for_filename(name);
+               if (fp != NULL && fseek(fp, partinfo->offset, SEEK_SET) == 0) {
+                       headers = procheader_get_header_array_asis(fp);
+                       if (headers) {
+                               gint i;
+                               for (i = 0; i < headers->len; i++) {
+                                       Header *header = g_ptr_array_index(headers, i);
+                                       if (procheader_headername_equal(header->name, "Subject")) {
+                                               unfold_line(header->body);
+                                               name = g_strconcat(header->body, ".txt", NULL);
+                                               subst_for_filename(name);
+                                       }
                                }
+                               procheader_header_array_destroy(headers);
                        }
-                       procheader_header_array_destroy(headers);
+                       fclose(fp);
                }
-               fclose(fp);
                if (name)
                        filename = g_path_get_basename(name);
                g_free(name);
index 9524026..cb2afcc 100644 (file)
@@ -714,12 +714,6 @@ MsgCache *msgcache_read_cache(FolderItem *item, const gchar *cache_file)
                        if(msginfo->msgid)
                                g_hash_table_insert(cache->msgid_table, msginfo->msgid, msginfo);
                }
-
-#ifdef G_OS_WIN32
-               UnmapViewOfFile((void*) cache_data);
-#else
-               munmap(cache_data, map_len);
-#endif
        } else {
                while (fread(&num, sizeof(num), 1, fp) == 1) {
                        if (swapping)
@@ -772,8 +766,14 @@ MsgCache *msgcache_read_cache(FolderItem *item, const gchar *cache_file)
                }
        }
 bail_err:
+       if (cache_data != NULL && cache_data != MAP_FAILED) {
+#ifdef G_OS_WIN32
+               UnmapViewOfFile((void*) cache_data);
+#else
+               munmap(cache_data, map_len);
+#endif
+       }
        fclose(fp);
-
        if (conv != NULL) {
                if (conv->free != NULL)
                        conv->free(conv);
@@ -859,11 +859,6 @@ void msgcache_read_mark(MsgCache *cache, const gchar *mark_file)
                                msginfo->flags.perm_flags = perm_flags;
                        }
                }
-#ifdef G_OS_WIN32
-               UnmapViewOfFile((void*) cache_data);
-#else
-               munmap(cache_data, map_len);
-#endif
        } else {
                while (fread(&num, sizeof(num), 1, fp) == 1) {
                        if (swapping)
@@ -881,6 +876,13 @@ void msgcache_read_mark(MsgCache *cache, const gchar *mark_file)
                }       
        }
 bail_err:
+       if (cache_data != NULL && cache_data != MAP_FAILED) {
+#ifdef G_OS_WIN32
+               UnmapViewOfFile((void*) cache_data);
+#else
+               munmap(cache_data, map_len);
+#endif
+       }
        fclose(fp);
        if (error) {
                debug_print("error reading cache mark from %s\n", mark_file);
@@ -961,11 +963,6 @@ void msgcache_read_tags(MsgCache *cache, const gchar *tags_file)
                                msginfo->tags = g_slist_reverse(msginfo->tags);
                        }
                }
-#ifdef G_OS_WIN32
-               UnmapViewOfFile((void*) cache_data);
-#else
-               munmap(cache_data, map_len);
-#endif
        } else {
                while (fread(&num, sizeof(num), 1, fp) == 1) {
                        gint id = -1;
@@ -991,6 +988,13 @@ void msgcache_read_tags(MsgCache *cache, const gchar *tags_file)
                }
        }
 bail_err:
+       if (cache_data != NULL && cache_data != MAP_FAILED) {
+#ifdef G_OS_WIN32
+               UnmapViewOfFile((void*) cache_data);
+#else
+               munmap(cache_data, map_len);
+#endif
+       }
        fclose(fp);
        if (error) {
                debug_print("error reading cache tags from %s\n", tags_file);
@@ -1136,7 +1140,8 @@ gint msgcache_write(const gchar *cache_file, const gchar *mark_file, const gchar
 
        if (w_err != 0) {
                g_warning("failed to write charset\n");
-               fclose(write_fps.cache_fp);
+               if (write_fps.cache_fp)
+                       fclose(write_fps.cache_fp);
                claws_unlink(new_cache);
                g_free(new_cache);
                g_free(new_mark);
@@ -1148,7 +1153,8 @@ gint msgcache_write(const gchar *cache_file, const gchar *mark_file, const gchar
                write_fps.mark_fp = msgcache_open_data_file(new_mark, MARK_VERSION,
                        DATA_WRITE, NULL, 0);
                if (write_fps.mark_fp == NULL) {
-                       fclose(write_fps.cache_fp);
+                       if (write_fps.cache_fp)
+                               fclose(write_fps.cache_fp);
                        claws_unlink(new_cache);
                        g_free(new_cache);
                        g_free(new_mark);
@@ -1163,8 +1169,10 @@ gint msgcache_write(const gchar *cache_file, const gchar *mark_file, const gchar
                write_fps.tags_fp = msgcache_open_data_file(new_tags, TAGS_VERSION,
                        DATA_WRITE, NULL, 0);
                if (write_fps.tags_fp == NULL) {
-                       fclose(write_fps.cache_fp);
-                       fclose(write_fps.mark_fp);
+                       if (write_fps.cache_fp)
+                               fclose(write_fps.cache_fp);
+                       if (write_fps.mark_fp)
+                               fclose(write_fps.mark_fp);
                        claws_unlink(new_cache);
                        claws_unlink(new_mark);
                        g_free(new_cache);
index 6d91b4a..d678e8f 100644 (file)
@@ -354,8 +354,7 @@ static Session *news_session_new(Folder *folder, const gchar *server, gushort po
        
        if (r != NEWSNNTP_NO_ERROR) {
                log_error(LOG_PROTOCOL, _("Error logging in to %s:%d...\n"), server, port);
-               if (session != NULL)
-                       session_destroy(SESSION(session));
+               session_destroy(SESSION(session));
                return NULL;
        }
 
@@ -763,7 +762,7 @@ void news_remove_group_list_cache(Folder *folder)
        g_free(path);
 
        if (is_file_exist(filename)) {
-               if (remove(filename) < 0)
+               if (claws_unlink(filename) < 0)
                        FILE_OP_ERROR(filename, "remove");
        }
        g_free(filename);
@@ -977,7 +976,7 @@ gint news_cancel_article(Folder * folder, MsgInfo * msginfo)
        }
 
        news_post(folder, tmp);
-       remove(tmp);
+       claws_unlink(tmp);
 
        g_free(tmp);
 
@@ -1295,8 +1294,6 @@ static GSList *news_get_msginfos(Folder *folder, FolderItem *item, GSList *msgnu
        NewsSession *session;
        GSList *elem, *msginfo_list = NULL, *tmp_msgnum_list, *tmp_msginfo_list;
        guint first, last, next;
-/*     guint tofetch, fetched;
-*/
        
        cm_return_val_if_fail(folder != NULL, NULL);
        cm_return_val_if_fail(FOLDER_CLASS(folder) == &news_class, NULL);
@@ -1310,9 +1307,6 @@ static GSList *news_get_msginfos(Folder *folder, FolderItem *item, GSList *msgnu
        tmp_msgnum_list = g_slist_sort(tmp_msgnum_list, g_int_compare);
 
        progressindicator_start(PROGRESS_TYPE_NETWORK);
-/*     tofetch = g_slist_length(tmp_msgnum_list);
-       fetched = 0;
-*/
 
        first = GPOINTER_TO_INT(tmp_msgnum_list->data);
        last = first;
@@ -1322,13 +1316,8 @@ static GSList *news_get_msginfos(Folder *folder, FolderItem *item, GSList *msgnu
        for(elem = g_slist_next(tmp_msgnum_list); elem != NULL; elem = g_slist_next(elem)) {
                next = GPOINTER_TO_INT(elem->data);
                if(next != (last + 1)) {
-/*                     session->fetch_base_percentage = ((gfloat) fetched) / ((gfloat) tofetch);
-                       session->fetch_total_percentage = ((gfloat) (last - first + 1)) / ((gfloat) tofetch);
-*/
                        tmp_msginfo_list = news_get_msginfos_for_range(session, item, first, last);
                        msginfo_list = g_slist_concat(msginfo_list, tmp_msginfo_list);
-/*                     fetched = last - first + 1;
-*/
                        first = next;
                }
                last = next;
@@ -1336,9 +1325,6 @@ static GSList *news_get_msginfos(Folder *folder, FolderItem *item, GSList *msgnu
        
        news_folder_unlock(NEWS_FOLDER(item->folder));
        
-/*     session->fetch_base_percentage = ((gfloat) fetched) / ((gfloat) tofetch);
-       session->fetch_total_percentage = ((gfloat) (last - first + 1)) / ((gfloat) tofetch);
-*/
        tmp_msginfo_list = news_get_msginfos_for_range(session, item, first, last);
        msginfo_list = g_slist_concat(msginfo_list, tmp_msginfo_list);
 
index 6ef11b9..d7b0ce6 100644 (file)
@@ -316,8 +316,7 @@ static int partial_uidl_mark_mail(MsgInfo *msginfo, int download)
        }
 
        fclose(fp);
-       claws_unlink(filename);
-       g_rename(pathnew, filename);
+       rename_force(pathnew, filename);
        g_free(pathnew);
        msginfo->planned_download = download;
        msgcache_update_msg(msginfo->folder->cache, msginfo);