Merge branch 'backport-1789-array-sort-empty-glib-2-66' into 'glib-2-66'

Backport !1789 “array: Avoid sorting 0-sized arrays” to glib-2-66

See merge request GNOME/glib!1792
This commit is contained in:
Sebastian Dröge 2020-12-09 15:40:27 +00:00
commit 120b659bd1
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); 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 */
if (array->len > 0)
g_qsort_with_data (array->data, g_qsort_with_data (array->data,
array->len, array->len,
array->elt_size, array->elt_size,
@ -853,6 +854,7 @@ g_array_sort_with_data (GArray *farray,
g_return_if_fail (array != NULL); g_return_if_fail (array != NULL);
if (array->len > 0)
g_qsort_with_data (array->data, g_qsort_with_data (array->data,
array->len, array->len,
array->elt_size, array->elt_size,
@ -1984,6 +1986,7 @@ 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 */
if (array->len > 0)
g_qsort_with_data (array->pdata, g_qsort_with_data (array->pdata,
array->len, array->len,
sizeof (gpointer), sizeof (gpointer),
@ -2060,6 +2063,7 @@ g_ptr_array_sort_with_data (GPtrArray *array,
{ {
g_return_if_fail (array != NULL); g_return_if_fail (array != NULL);
if (array->len > 0)
g_qsort_with_data (array->pdata, g_qsort_with_data (array->pdata,
array->len, array->len,
sizeof (gpointer), sizeof (gpointer),

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