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,6 +822,7 @@ g_array_sort (GArray *farray,
g_return_if_fail (array != NULL);
/* Don't use qsort as we want a guaranteed stable sort */
if (array->len > 0)
g_qsort_with_data (array->data,
array->len,
array->elt_size,
@ -853,6 +854,7 @@ g_array_sort_with_data (GArray *farray,
g_return_if_fail (array != NULL);
if (array->len > 0)
g_qsort_with_data (array->data,
array->len,
array->elt_size,
@ -1984,6 +1986,7 @@ g_ptr_array_sort (GPtrArray *array,
g_return_if_fail (array != NULL);
/* Don't use qsort as we want a guaranteed stable sort */
if (array->len > 0)
g_qsort_with_data (array->pdata,
array->len,
sizeof (gpointer),
@ -2060,6 +2063,7 @@ g_ptr_array_sort_with_data (GPtrArray *array,
{
g_return_if_fail (array != NULL);
if (array->len > 0)
g_qsort_with_data (array->pdata,
array->len,
sizeof (gpointer),

View File

@ -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)));