From 23b6699ec35d069c3f4a7b3a90bb5881740aab8e Mon Sep 17 00:00:00 2001 From: Daniel Boles Date: Mon, 7 Aug 2017 18:32:57 +0100 Subject: [PATCH] =?UTF-8?q?Container:=20Don=E2=80=99t=20scroll=20to=20unse?= =?UTF-8?q?t=20focus=20child=20coord?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 file changed, 4 insertions(+), 4 deletions(-) diff --git a/gtk/gtkcontainer.c b/gtk/gtkcontainer.c index bc52c99554..b24bef843d 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;