stack: fix pages list bounds check
authorG.Willems <g.willems.dev@laposte.net>
Sat, 24 Jun 2023 01:05:37 +0000 (03:05 +0200)
committerG.Willems <g.willems.dev@laposte.net>
Sat, 24 Jun 2023 01:05:37 +0000 (03:05 +0200)
Fix integer underflow when children->len is 0.
Add missing bounds check in select_item()

gtk/gtkstack.c

index dad20362503fffbe73fbecfffe88ac5b5c42d6f6..b56d24d9c04d55cbe454e8cb3df75bd633f90e75 100644 (file)
@@ -617,8 +617,7 @@ gtk_stack_pages_get_item (GListModel *model,
   GtkStackPrivate *priv = gtk_stack_get_instance_private (pages->stack);
   GtkStackPage *page;
 
-
-  if (position > priv->children->len - 1)
+  if (position >= priv->children->len)
     return NULL;
 
   page = g_ptr_array_index (priv->children, position);
@@ -642,7 +641,7 @@ gtk_stack_pages_is_selected (GtkSelectionModel *model,
   GtkStackPrivate *priv = gtk_stack_get_instance_private (pages->stack);
   GtkStackPage *page;
 
-  if (position > priv->children->len - 1)
+  if (position >= priv->children->len)
     return FALSE;
 
   page = g_ptr_array_index (priv->children, position);
@@ -664,6 +663,9 @@ gtk_stack_pages_select_item (GtkSelectionModel *model,
   GtkStackPrivate *priv = gtk_stack_get_instance_private (pages->stack);
   GtkStackPage *page;
 
+  if (position >= priv->children->len)
+    return FALSE;
+
   page = g_ptr_array_index (priv->children, position);
 
   set_visible_child (pages->stack, page, priv->transition_type, priv->transition_duration);
@@ -813,7 +815,7 @@ gtk_stack_accessible_get_first_accessible_child (GtkAccessible *accessible)
 
   if (priv->children->len > 0)
     page_accessible = GTK_ACCESSIBLE (g_object_ref (g_ptr_array_index (priv->children, 0)));
-  
+
   return page_accessible;
 }