[glib: 2/4] garray: Allow over-allocation in g_array_insert_vals()
- From: Philip Withnall <pwithnall src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [glib: 2/4] garray: Allow over-allocation in g_array_insert_vals()
- Date: Mon, 4 Jun 2018 10:35:32 +0000 (UTC)
commit 89f45e96b246178534c6e0180fbf754596e934d9
Author: Philip Withnall <withnall endlessm com>
Date: Wed May 9 15:35:23 2018 +0100
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
necessary.
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 <withnall endlessm com>
https://bugzilla.gnome.org/show_bug.cgi?id=795975
glib/garray.c | 10 ++++++++++
1 file changed, 10 insertions(+)
---
diff --git a/glib/garray.c b/glib/garray.c
index 5207eccc3..5fa8c4663 100644
--- a/glib/garray.c
+++ b/glib/garray.c
@@ -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_),
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]