[gtk+/gtk-2-24] Container: Don’t scroll to unset focus child coord



commit bf8c1c212ebc6d05b534aa1c0edff73103e9cc56
Author: Daniel Boles <dboles src gmail com>
Date:   Mon Aug 7 18:54:30 2017 +0100

    Container: Don’t scroll to unset focus child coord
    
    In gtk_container_real_set_focus_child(), we try to scroll to the
    position of the new :focus-child if we have h or v adjustments.
    
    gtk_widget_translate_coordinates() returns FALSE if neither widget is
    realized or in other situations that cause output parameters x and y not
    to be set. Thus, if the caller did not initialise x/y and uses them even
    if the function returns FALSE, they are using uninitialised variables.
    
    In gtk_container_real_set_focus_child(), we did not check the return
    value but merrily went ahead and used x and y regardless. This is UB, as
    caught by Valgrind, as well as being pointless.
    
    The trivial fix is to exit early if (!gtk_widget_translate_coordinates).
    
    https://bugzilla.gnome.org/show_bug.cgi?id=776909

 gtk/gtkcontainer.c |   12 ++++++------
 1 files changed, 6 insertions(+), 6 deletions(-)
---
diff --git a/gtk/gtkcontainer.c b/gtk/gtkcontainer.c
index 9aaa7d9..c6cd16b 100644
--- a/gtk/gtkcontainer.c
+++ b/gtk/gtkcontainer.c
@@ -1748,14 +1748,14 @@ gtk_container_real_set_focus_child (GtkContainer     *container,
     {
       if (container->focus_child)
        g_object_unref (container->focus_child);
+
       container->focus_child = child;
+
       if (container->focus_child)
        g_object_ref (container->focus_child);
     }
 
-
-  /* check for h/v adjustments
-   */
+  /* Check for h/v adjustments and scroll to show the focus child if possible */
   if (container->focus_child)
     {
       GtkAdjustment *hadj;
@@ -1767,7 +1767,6 @@ gtk_container_real_set_focus_child (GtkContainer     *container,
       vadj = g_object_get_qdata (G_OBJECT (container), vadjustment_key_id);
       if (hadj || vadj) 
        {
-
          focus_child = container->focus_child;
          while (GTK_IS_CONTAINER (focus_child) && 
                 GTK_CONTAINER (focus_child)->focus_child)
@@ -1775,8 +1774,9 @@ gtk_container_real_set_focus_child (GtkContainer     *container,
              focus_child = GTK_CONTAINER (focus_child)->focus_child;
            }
          
-         gtk_widget_translate_coordinates (focus_child, container->focus_child, 
-                                           0, 0, &x, &y);
+           if (!gtk_widget_translate_coordinates (focus_child, container->focus_child,
+                                                  0, 0, &x, &y))
+             return;
 
           x += container->focus_child->allocation.x;
           y += container->focus_child->allocation.y;


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