Commit 89f45e96 authored by Philip Withnall's avatar Philip Withnall

garray: Allow over-allocation in g_array_insert_vals()

Previously, g_array_insert_vals() would crash if called with an index
off the end of the array. This is inconsistent with the behaviour of
other methods (like g_array_set_size()), which will expand the array as

Modify g_array_insert_vals() to expand the array as necessary. New array
elements will be cleared to zero if the GArray was constructed with
(clear_ == TRUE).
Signed-off-by: Philip Withnall's avatarPhilip Withnall <>
parent 3eeec778
......@@ -506,6 +506,11 @@ g_array_prepend_vals (GArray *farray,
* Inserts @len elements into a #GArray at the given index.
* If @index_ is greater than the array’s current length, the array is expanded.
* The elements between the old end of the array and the newly inserted elements
* will be initialised to zero if the array was configured to clear elements;
* otherwise their values will be undefined.
* @data may be %NULL if (and only if) @len is zero. If @len is zero, this
* function is a no-op.
......@@ -538,6 +543,11 @@ g_array_insert_vals (GArray *farray,
if (len == 0)
return farray;
/* Is the index off the end of the array, and hence do we need to over-allocate
* and clear some elements? */
if (index_ >= array->len)
return g_array_append_vals (g_array_set_size (farray, index_), data, len);
g_array_maybe_expand (array, len);
memmove (g_array_elt_pos (array, len + index_),
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment