From 80e47a71611303b2f4be0168aeb678c4d800e22a Mon Sep 17 00:00:00 2001 From: Marius Vlad Date: Fri, 18 Oct 2024 11:13:38 +0300 Subject: [PATCH] libweston/desktop: Avoid a potential crash on invalid resource This checks for a valid resource when getting a xdg_popup. Similar to the check in get_toplevel. Signed-off-by: Marius Vlad --- libweston/desktop/xdg-shell-v6.c | 20 ++++++++++++++------ libweston/desktop/xdg-shell.c | 20 ++++++++++++++------ 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/libweston/desktop/xdg-shell-v6.c b/libweston/desktop/xdg-shell-v6.c index 65db389d..1f11416d 100644 --- a/libweston/desktop/xdg-shell-v6.c +++ b/libweston/desktop/xdg-shell-v6.c @@ -1117,12 +1117,9 @@ weston_desktop_xdg_surface_protocol_get_popup(struct wl_client *wl_client, struct wl_resource *parent_resource, struct wl_resource *positioner_resource) { - struct weston_desktop_surface *dsurface = - wl_resource_get_user_data(resource); - struct weston_surface *wsurface = - weston_desktop_surface_get_surface(dsurface); - struct weston_desktop_xdg_popup *popup = - weston_desktop_surface_get_implementation_data(dsurface); + struct weston_desktop_surface *dsurface = NULL; + struct weston_surface *wsurface = NULL; + struct weston_desktop_xdg_popup *popup = NULL; struct weston_desktop_surface *parent_surface = wl_resource_get_user_data(parent_resource); struct weston_desktop_xdg_surface *parent = @@ -1131,6 +1128,17 @@ weston_desktop_xdg_surface_protocol_get_popup(struct wl_client *wl_client, wl_resource_get_user_data(positioner_resource); struct weston_coord_surface offset; + dsurface = wl_resource_get_user_data(resource); + if (!dsurface) { + wl_resource_post_error(resource, + ZXDG_SURFACE_V6_ERROR_NOT_CONSTRUCTED, + "xdg surface destroyed"); + return; + } + + wsurface = weston_desktop_surface_get_surface(dsurface); + popup = weston_desktop_surface_get_implementation_data(dsurface); + /* Checking whether the size and anchor rect both have a positive size * is enough to verify both have been correctly set */ if (positioner->size.width == 0 || positioner->anchor_rect.width == 0) { diff --git a/libweston/desktop/xdg-shell.c b/libweston/desktop/xdg-shell.c index c2b74de5..f1942663 100644 --- a/libweston/desktop/xdg-shell.c +++ b/libweston/desktop/xdg-shell.c @@ -1323,18 +1323,26 @@ weston_desktop_xdg_surface_protocol_get_popup(struct wl_client *wl_client, struct wl_resource *parent_resource, struct wl_resource *positioner_resource) { - struct weston_desktop_surface *dsurface = - wl_resource_get_user_data(resource); - struct weston_surface *wsurface = - weston_desktop_surface_get_surface(dsurface); - struct weston_desktop_xdg_popup *popup = - weston_desktop_surface_get_implementation_data(dsurface); + struct weston_desktop_surface *dsurface = NULL; + struct weston_surface *wsurface = NULL; + struct weston_desktop_xdg_popup *popup = NULL; struct weston_desktop_surface *parent_surface; struct weston_desktop_xdg_surface *parent; struct weston_desktop_xdg_positioner *positioner = wl_resource_get_user_data(positioner_resource); struct weston_coord_surface offset; + dsurface = wl_resource_get_user_data(resource); + if (!dsurface) { + wl_resource_post_error(resource, + XDG_SURFACE_ERROR_DEFUNCT_ROLE_OBJECT, + "xdg surface destroyed"); + return; + } + + wsurface = weston_desktop_surface_get_surface(dsurface); + popup = weston_desktop_surface_get_implementation_data(dsurface); + /* Popup parents are allowed to be non-null, but only if a parent is * specified 'using some other protocol' before committing. Since we * don't support such a protocol yet, clients cannot legitimately