Merge branch 'master' into 'master'

array: Avoid sorting 0-sized arrays

Closes #2264

See merge request GNOME/glib!1789
This commit is contained in:
Philip Withnall 2020-12-09 15:11:57 +00:00
commit 7e63bc8eb1
2 changed files with 40 additions and 20 deletions

View File

@ -822,11 +822,12 @@ g_array_sort (GArray *farray,
g_return_if_fail (array != NULL); g_return_if_fail (array != NULL);
/* Don't use qsort as we want a guaranteed stable sort */ /* Don't use qsort as we want a guaranteed stable sort */
g_qsort_with_data (array->data, if (array->len > 0)
array->len, g_qsort_with_data (array->data,
array->elt_size, array->len,
(GCompareDataFunc)compare_func, array->elt_size,
NULL); (GCompareDataFunc)compare_func,
NULL);
} }
/** /**
@ -853,11 +854,12 @@ g_array_sort_with_data (GArray *farray,
g_return_if_fail (array != NULL); g_return_if_fail (array != NULL);
g_qsort_with_data (array->data, if (array->len > 0)
array->len, g_qsort_with_data (array->data,
array->elt_size, array->len,
compare_func, array->elt_size,
user_data); compare_func,
user_data);
} }
/** /**
@ -1984,11 +1986,12 @@ g_ptr_array_sort (GPtrArray *array,
g_return_if_fail (array != NULL); g_return_if_fail (array != NULL);
/* Don't use qsort as we want a guaranteed stable sort */ /* Don't use qsort as we want a guaranteed stable sort */
g_qsort_with_data (array->pdata, if (array->len > 0)
array->len, g_qsort_with_data (array->pdata,
sizeof (gpointer), array->len,
(GCompareDataFunc)compare_func, sizeof (gpointer),
NULL); (GCompareDataFunc)compare_func,
NULL);
} }
/* Please keep this doc-comment in sync with /* 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_return_if_fail (array != NULL);
g_qsort_with_data (array->pdata, if (array->len > 0)
array->len, g_qsort_with_data (array->pdata,
sizeof (gpointer), array->len,
compare_func, sizeof (gpointer),
user_data); compare_func,
user_data);
} }
/** /**

View File

@ -590,6 +590,10 @@ array_sort (gconstpointer test_data)
gint prev, cur; gint prev, cur;
garray = g_array_new (config->zero_terminated, config->clear_, sizeof (gint)); 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++) for (i = 0; i < 10000; i++)
{ {
cur = g_random_int_range (0, 10000); cur = g_random_int_range (0, 10000);
@ -622,6 +626,10 @@ array_sort_with_data (gconstpointer test_data)
gint prev, cur; gint prev, cur;
garray = g_array_new (config->zero_terminated, config->clear_, sizeof (gint)); 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++) for (i = 0; i < 10000; i++)
{ {
cur = g_random_int_range (0, 10000); cur = g_random_int_range (0, 10000);
@ -1334,6 +1342,10 @@ pointer_array_sort (void)
gint prev, cur; gint prev, cur;
gparray = g_ptr_array_new (); gparray = g_ptr_array_new ();
/* Sort empty array */
g_ptr_array_sort (gparray, ptr_compare);
for (i = 0; i < 10000; i++) for (i = 0; i < 10000; i++)
{ {
val = g_random_int_range (0, 10000); val = g_random_int_range (0, 10000);
@ -1505,6 +1517,10 @@ pointer_array_sort_with_data (void)
gint prev, cur; gint prev, cur;
gparray = g_ptr_array_new (); 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++) for (i = 0; i < 10000; i++)
g_ptr_array_add (gparray, GINT_TO_POINTER (g_random_int_range (0, 10000))); g_ptr_array_add (gparray, GINT_TO_POINTER (g_random_int_range (0, 10000)));