[gtk+] Container: Don’t scroll to unset focus child coord



commit 23b6699ec35d069c3f4a7b3a90bb5881740aab8e
Author: Daniel Boles <dboles src gnome org>
Date:   Mon Aug 7 18:32:57 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 returned 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
    revealed 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 |    8 ++++----
 1 files changed, 4 insertions(+), 4 deletions(-)
---
diff --git a/gtk/gtkcontainer.c b/gtk/gtkcontainer.c
index bc52c99..b24bef8 100644
--- a/gtk/gtkcontainer.c
+++ b/gtk/gtkcontainer.c
@@ -2038,8 +2038,7 @@ gtk_container_real_set_focus_child (GtkContainer *container,
   g_return_if_fail (GTK_IS_CONTAINER (container));
   g_return_if_fail (focus_child == NULL || GTK_IS_WIDGET (focus_child));
 
-  /* check for h/v adjustments
-   */
+  /* Check for h/v adjustments and scroll to show the focus child if possible */
   if (focus_child)
     {
       GtkAdjustment *hadj;
@@ -2056,8 +2055,9 @@ gtk_container_real_set_focus_child (GtkContainer *container,
           while (gtk_widget_get_focus_child (child))
             child = gtk_widget_get_focus_child (child);
 
-          gtk_widget_translate_coordinates (child, focus_child,
-                                            0, 0, &x, &y);
+          if (!gtk_widget_translate_coordinates (child, focus_child,
+                                                 0, 0, &x, &y))
+            return;
 
           _gtk_widget_get_allocation (focus_child, &allocation);
           x += allocation.x;


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