[glib: 1/2] array: Avoid sorting 0-sized arrays




commit c2052a4ada435b0ed02ac1e796c52ca7a62647d7
Author: Timm Bäder <tbaeder redhat com>
Date:   Wed Dec 9 13:57:24 2020 +0100

    array: Avoid sorting 0-sized arrays
    
    Fixes #2264

 glib/garray.c           | 44 ++++++++++++++++++++++++--------------------
 glib/tests/array-test.c | 16 ++++++++++++++++
 2 files changed, 40 insertions(+), 20 deletions(-)
---
diff --git a/glib/garray.c b/glib/garray.c
index 3f23b980d..4b6de199a 100644
--- a/glib/garray.c
+++ b/glib/garray.c
@@ -822,11 +822,12 @@ g_array_sort (GArray       *farray,
   g_return_if_fail (array != NULL);
 
   /* Don't use qsort as we want a guaranteed stable sort */
-  g_qsort_with_data (array->data,
-                     array->len,
-                     array->elt_size,
-                     (GCompareDataFunc)compare_func,
-                     NULL);
+  if (array->len > 0)
+    g_qsort_with_data (array->data,
+                       array->len,
+                       array->elt_size,
+                       (GCompareDataFunc)compare_func,
+                       NULL);
 }
 
 /**
@@ -853,11 +854,12 @@ g_array_sort_with_data (GArray           *farray,
 
   g_return_if_fail (array != NULL);
 
-  g_qsort_with_data (array->data,
-                     array->len,
-                     array->elt_size,
-                     compare_func,
-                     user_data);
+  if (array->len > 0)
+    g_qsort_with_data (array->data,
+                       array->len,
+                       array->elt_size,
+                       compare_func,
+                       user_data);
 }
 
 /**
@@ -1984,11 +1986,12 @@ g_ptr_array_sort (GPtrArray    *array,
   g_return_if_fail (array != NULL);
 
   /* Don't use qsort as we want a guaranteed stable sort */
-  g_qsort_with_data (array->pdata,
-                     array->len,
-                     sizeof (gpointer),
-                     (GCompareDataFunc)compare_func,
-                     NULL);
+  if (array->len > 0)
+    g_qsort_with_data (array->pdata,
+                       array->len,
+                       sizeof (gpointer),
+                       (GCompareDataFunc)compare_func,
+                       NULL);
 }
 
 /* Please keep this doc-comment in sync with
@@ -2060,11 +2063,12 @@ g_ptr_array_sort_with_data (GPtrArray        *array,
 {
   g_return_if_fail (array != NULL);
 
-  g_qsort_with_data (array->pdata,
-                     array->len,
-                     sizeof (gpointer),
-                     compare_func,
-                     user_data);
+  if (array->len > 0)
+    g_qsort_with_data (array->pdata,
+                       array->len,
+                       sizeof (gpointer),
+                       compare_func,
+                       user_data);
 }
 
 /**
diff --git a/glib/tests/array-test.c b/glib/tests/array-test.c
index 8fd76693b..adedfc19f 100644
--- a/glib/tests/array-test.c
+++ b/glib/tests/array-test.c
@@ -590,6 +590,10 @@ array_sort (gconstpointer test_data)
   gint prev, cur;
 
   garray = g_array_new (config->zero_terminated, config->clear_, sizeof (gint));
+
+  /* Sort empty array */
+  g_array_sort (garray, int_compare);
+
   for (i = 0; i < 10000; i++)
     {
       cur = g_random_int_range (0, 10000);
@@ -622,6 +626,10 @@ array_sort_with_data (gconstpointer test_data)
   gint prev, cur;
 
   garray = g_array_new (config->zero_terminated, config->clear_, sizeof (gint));
+
+  /* Sort empty array */
+  g_array_sort_with_data (garray, int_compare_data, NULL);
+
   for (i = 0; i < 10000; i++)
     {
       cur = g_random_int_range (0, 10000);
@@ -1334,6 +1342,10 @@ pointer_array_sort (void)
   gint prev, cur;
 
   gparray = g_ptr_array_new ();
+
+  /* Sort empty array */
+  g_ptr_array_sort (gparray, ptr_compare);
+
   for (i = 0; i < 10000; i++)
     {
       val = g_random_int_range (0, 10000);
@@ -1505,6 +1517,10 @@ pointer_array_sort_with_data (void)
   gint prev, cur;
 
   gparray = g_ptr_array_new ();
+
+  /* Sort empty array */
+  g_ptr_array_sort_with_data (gparray, ptr_compare_data, NULL);
+
   for (i = 0; i < 10000; i++)
     g_ptr_array_add (gparray, GINT_TO_POINTER (g_random_int_range (0, 10000)));
 


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