diff --git a/glib/garray.c b/glib/garray.c index 413ea8539..f67099315 100644 --- a/glib/garray.c +++ b/glib/garray.c @@ -2338,23 +2338,23 @@ g_ptr_array_insert (GPtrArray *array, gpointer data) { GRealPtrArray *rarray = (GRealPtrArray *)array; + guint real_index; g_return_if_fail (rarray); g_return_if_fail (index_ >= -1); - g_return_if_fail (index_ <= (gint)rarray->len); + g_return_if_fail (index_ < 0 || (guint) index_ <= rarray->len); g_ptr_array_maybe_expand (rarray, 1u + rarray->null_terminated); - if (index_ < 0) - index_ = rarray->len; + real_index = (index_ >= 0) ? (guint) index_ : rarray->len; - if ((guint) index_ < rarray->len) - memmove (&(rarray->pdata[index_ + 1]), - &(rarray->pdata[index_]), - (rarray->len - index_) * sizeof (gpointer)); + if (real_index < rarray->len) + memmove (&(rarray->pdata[real_index + 1]), + &(rarray->pdata[real_index]), + (rarray->len - real_index) * sizeof (gpointer)); rarray->len++; - rarray->pdata[index_] = data; + rarray->pdata[real_index] = data; ptr_array_maybe_null_terminate (rarray); }