[glib] array: Support clearing an empty array with g_array_remove_range()



commit 37756a06c9c8821e4cbc22218046b88cdc85ca90
Author: Philip Withnall <philip withnall collabora co uk>
Date:   Tue Mar 8 18:59:34 2016 +0000

    array: Support clearing an empty array with g_array_remove_range()
    
    Previously, calling g_array_remove_range(array, 0, array->len) on an
    empty array would result in a precondition failure in
    g_array_remove_range(), as the given start index (0), was not strictly
    less than the array length (0).
    
    Allow the index to equal the array length, so that zero elements can be
    removed from any array. A subsequent check makes sure that the array
    length is not overflowed by the index + length.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=763339

 glib/garray.c           |    6 +++---
 glib/tests/array-test.c |    8 ++++++++
 2 files changed, 11 insertions(+), 3 deletions(-)
---
diff --git a/glib/garray.c b/glib/garray.c
index 8c26f87..727c102 100644
--- a/glib/garray.c
+++ b/glib/garray.c
@@ -666,7 +666,7 @@ g_array_remove_range (GArray *farray,
   GRealArray *array = (GRealArray*) farray;
 
   g_return_val_if_fail (array, NULL);
-  g_return_val_if_fail (index_ < array->len, NULL);
+  g_return_val_if_fail (index_ <= array->len, NULL);
   g_return_val_if_fail (index_ + length <= array->len, NULL);
 
   if (array->clear_func != NULL)
@@ -1263,7 +1263,7 @@ g_ptr_array_remove_range (GPtrArray *array,
   guint n;
 
   g_return_val_if_fail (rarray != NULL, NULL);
-  g_return_val_if_fail (index_ < rarray->len, NULL);
+  g_return_val_if_fail (index_ <= rarray->len, NULL);
   g_return_val_if_fail (index_ + length <= rarray->len, NULL);
 
   if (rarray->element_free_func != NULL)
@@ -1813,7 +1813,7 @@ g_byte_array_remove_range (GByteArray *array,
                            guint       length)
 {
   g_return_val_if_fail (array, NULL);
-  g_return_val_if_fail (index_ < array->len, NULL);
+  g_return_val_if_fail (index_ <= array->len, NULL);
   g_return_val_if_fail (index_ + length <= array->len, NULL);
 
   return (GByteArray *)g_array_remove_range ((GArray *)array, index_, length);
diff --git a/glib/tests/array-test.c b/glib/tests/array-test.c
index 86b613a..a0074d4 100644
--- a/glib/tests/array-test.c
+++ b/glib/tests/array-test.c
@@ -168,6 +168,10 @@ array_remove_range (void)
       prev = cur;
     }
 
+  /* Ensure the entire array can be cleared, even when empty. */
+  g_array_remove_range (garray, 0, garray->len);
+  g_array_remove_range (garray, 0, garray->len);
+
   g_array_free (garray, TRUE);
 }
 
@@ -711,6 +715,10 @@ byte_array_remove_range (void)
       g_assert (gbarray->data[4*i+3] == 'd');
     }
 
+  /* Ensure the entire array can be cleared, even when empty. */
+  g_byte_array_remove_range (gbarray, 0, gbarray->len);
+  g_byte_array_remove_range (gbarray, 0, gbarray->len);
+
   g_byte_array_free (gbarray, TRUE);
 }
 


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