From a91de44755539f8eafa760d8d6005cb206bad20e Mon Sep 17 00:00:00 2001 From: Sam Bazley Date: Sun, 25 Oct 2020 15:30:50 +0000 Subject: [PATCH] gdk: Fix parent relative background crash When setting a window's background to ParentRelative on X11, the window depths must match to avoid a BadMatch error. Query the X server for the parent window rather that relying on the parent passed to gtk_window_new() to prevent crashes with reparented windows. Fixes: #3288 --- gdk/x11/gdkwindow-x11.c | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/gdk/x11/gdkwindow-x11.c b/gdk/x11/gdkwindow-x11.c index 1fe7b90cd0..38ff91da6d 100644 --- a/gdk/x11/gdkwindow-x11.c +++ b/gdk/x11/gdkwindow-x11.c @@ -3025,12 +3025,29 @@ G_GNUC_END_IGNORE_DEPRECATIONS if (pattern == parent_relative_pattern) { - GdkWindow *parent; + Window xroot, xparent, *xchildren; + guint nxchildren, xparent_depth; + XWindowAttributes xattrs; + + if (!XQueryTree (GDK_WINDOW_XDISPLAY (window), GDK_WINDOW_XID (window), + &xroot, &xparent, &xchildren, &nxchildren)) + { + XSetWindowBackgroundPixmap (GDK_WINDOW_XDISPLAY (window), + GDK_WINDOW_XID (window), None); + return; + } + + if (xchildren) + XFree (xchildren); + + if (xparent) { + XGetWindowAttributes (GDK_WINDOW_XDISPLAY (window), xparent, &xattrs); + xparent_depth = xattrs.depth; + } /* X throws BadMatch if the parent has a different depth when * using ParentRelative */ - parent = gdk_window_get_parent (window); - if (parent != NULL && window->depth == parent->depth && + if (xparent && window->depth == xparent_depth && cairo_pattern_status (pattern) == CAIRO_STATUS_SUCCESS) { XSetWindowBackgroundPixmap (GDK_WINDOW_XDISPLAY (window),