commit d5bae953924538e01da03ad050176f04ef41078d
Author: Thomas Mittelstaedt <tmstaedt@t-mittelstaedt.de>
Date:   Sat Mar 27 03:05:03 2010 +0100

    More careful approach, just check for non-NULL-ness of closure.vals before accessing it\!

diff --git a/widgets/table/e-table-sorting-utils.c b/widgets/table/e-table-sorting-utils.c
index 344a7cf..d0efc29 100644
--- a/widgets/table/e-table-sorting-utils.c
+++ b/widgets/table/e-table-sorting-utils.c
@@ -64,6 +64,7 @@ etsu_compare(ETableModel *source, ETableSortInfo *sort_info, ETableHeader *full_
 
 typedef struct {
 	gint cols;
+	gint max_number_of_vals;
 	gpointer *vals;
 	gint *ascending;
 	GCompareFunc *compare;
@@ -82,14 +83,20 @@ e_sort_callback(gconstpointer data1, gconstpointer data2, gpointer user_data)
 {
 	gint row1 = *(gint *)data1;
 	gint row2 = *(gint *)data2;
+	gint idx1;
+	gint idx2;
 	ETableSortClosure *closure = user_data;
 	gint j;
 	gint sort_count = closure->cols;
 	gint comp_val = 0;
 	gint ascending = 1;
-	for (j = 0; j < sort_count; j++) {
-		comp_val = (*(closure->compare[j]))(closure->vals[closure->cols * row1 + j], closure->vals[closure->cols * row2 + j]);
-		ascending = closure->ascending[j];
+	for (j = 0; closure != NULL  &&  j < sort_count; j++) {
+		idx1 = closure->cols * row1 + j;
+		idx2 = closure->cols * row2 + j;
+		if (idx1 < closure->max_number_of_vals  &&  idx2 < closure->max_number_of_vals) {
+			comp_val = (*(closure->compare[j]))(closure->vals[idx1], closure->vals[idx2]);
+			ascending = closure->ascending[j];
+		}
 		if (comp_val != 0)
 			break;
 	}
@@ -110,6 +117,7 @@ e_table_sorting_utils_sort(ETableModel *source, ETableSortInfo *sort_info, ETabl
 	gint total_rows;
 	gint i;
 	gint j;
+	gint idx;
 	gint cols;
 	ETableSortClosure closure;
 
@@ -124,7 +132,8 @@ e_table_sorting_utils_sort(ETableModel *source, ETableSortInfo *sort_info, ETabl
 	cols = e_table_sort_info_sorting_get_count(sort_info);
 	closure.cols = cols;
 
-	closure.vals = g_new(gpointer , total_rows * cols);
+	closure.max_number_of_vals = total_rows * cols;
+	closure.vals = g_new(gpointer , closure.max_number_of_vals);
 	closure.ascending = g_new(int, cols);
 	closure.compare = g_new(GCompareFunc, cols);
 
@@ -134,8 +143,11 @@ e_table_sorting_utils_sort(ETableModel *source, ETableSortInfo *sort_info, ETabl
 		col = e_table_header_get_column_by_col_idx(full_header, column.column);
 		if (col == NULL)
 			col = e_table_header_get_column (full_header, e_table_header_count (full_header) - 1);
-		for (i = 0; i < rows; i++) {
-			closure.vals[map_table[i] * cols + j] = e_table_model_value_at (source, col->compare_col, map_table[i]);
+		for (i = 0; (closure.vals != NULL) && (i < rows); i++) {
+			idx = map_table[i] * cols + j;
+			if (idx >= 0  &&  idx < (total_rows * cols)) {
+				closure.vals[idx] = e_table_model_value_at (source, col->compare_col, map_table[i]);
+			}
 		}
 		closure.compare[j] = col->compare;
 		closure.ascending[j] = column.ascending;
