[balsa] Check for zero-length array



commit 9e4882da8f8ec4e871267aa887db556cfc964593
Author: Peter Bloomfield <PeterBloomfield bellsouth net>
Date:   Wed Feb 7 15:36:19 2018 -0500

    Check for zero-length array
    
    * libbalsa/mailbox.c (lbm_sort): check for zero-length array.

 ChangeLog          |    4 ++++
 libbalsa/mailbox.c |   46 +++++++++++++++++++++++++---------------------
 2 files changed, 29 insertions(+), 21 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 52ac5be..951767f 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,9 @@
 2018-02-07  Peter Bloomfield  <pbloomfield bellsouth net>
 
+       * libbalsa/mailbox.c (lbm_sort): check for zero-length array.
+
+2018-02-07  Peter Bloomfield  <pbloomfield bellsouth net>
+
        Fix two use-after-free false positives
 
        * src/filter-edit-callbacks.c (change_filter_name): Use
diff --git a/libbalsa/mailbox.c b/libbalsa/mailbox.c
index 3ce0f9a..5a71796 100644
--- a/libbalsa/mailbox.c
+++ b/libbalsa/mailbox.c
@@ -3431,13 +3431,10 @@ lbm_has_valid_index_entry(LibBalsaMailbox * mailbox, guint msgno)
 static void
 lbm_sort(LibBalsaMailbox * mbox, GNode * parent)
 {
-    GtkTreeIter iter;
     GArray *sort_array;
     GPtrArray *node_array;
     GNode *node, *tmp_node, *prev;
     guint i, j;
-    gint *new_order;
-    GtkTreePath *path;
     gboolean sort_no = mbox->view->sort_field == LB_MAILBOX_SORT_NO;
 #if !defined(LOCAL_MAILBOX_SORTED_JUST_ONCE_ON_OPENING)
     gboolean can_sort_all = sort_no || LIBBALSA_IS_MAILBOX_IMAP(mbox);
@@ -3505,24 +3502,31 @@ lbm_sort(LibBalsaMailbox * mbox, GNode * parent)
         prev->next = NULL;
 
     /* Let the world know about our new order */
-    new_order = g_new(gint, node_array->len);
-    i = j = 0;
-    for (tmp_node = node; tmp_node; tmp_node = tmp_node->next) {
-        guint msgno = GPOINTER_TO_UINT(tmp_node->data);
-        new_order[j] = can_sort_all || lbm_has_valid_index_entry(mbox, msgno)
-            ? g_array_index(sort_array, SortTuple, i++).offset
-            : j;
-        j++;
+    if (node_array->len > 0) {
+        gint *new_order;
+        GtkTreeIter iter;
+        GtkTreePath *path;
+
+        new_order = g_new(gint, node_array->len);
+        i = j = 0;
+        for (tmp_node = node; tmp_node; tmp_node = tmp_node->next) {
+            guint msgno = GPOINTER_TO_UINT(tmp_node->data);
+            new_order[j] = can_sort_all || lbm_has_valid_index_entry(mbox, msgno)
+                ? g_array_index(sort_array, SortTuple, i++).offset
+                : j;
+            j++;
+        }
+
+        iter.stamp = mbox->stamp;
+        iter.user_data = parent;
+        path = parent->parent ? mbox_model_get_path(GTK_TREE_MODEL(mbox), &iter)
+                              : gtk_tree_path_new();
+        gtk_tree_model_rows_reordered(GTK_TREE_MODEL(mbox),
+                                      path, &iter, new_order);
+        gtk_tree_path_free(path);
+        g_free(new_order);
     }
 
-    iter.stamp = mbox->stamp;
-    iter.user_data = parent;
-    path = parent->parent ? mbox_model_get_path(GTK_TREE_MODEL(mbox), &iter)
-                          : gtk_tree_path_new();
-    gtk_tree_model_rows_reordered(GTK_TREE_MODEL(mbox),
-                                  path, &iter, new_order);
-    gtk_tree_path_free(path);
-    g_free(new_order);
     g_array_free(sort_array, TRUE);
     g_ptr_array_free(node_array, TRUE);
 


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