From 10ba8ebc7050d4290fa5bd9bbd3ed771c266afe3 Mon Sep 17 00:00:00 2001 From: Kirill Primak Date: Wed, 26 Jul 2023 10:50:09 +0300 Subject: [PATCH] Don't assume xdg_surface.{toplevel,popup} is non-NULL This assumption will become incorrect with future commits. --- tinywl/tinywl.c | 4 ++- types/scene/xdg_shell.c | 6 ++-- types/xdg_shell/wlr_xdg_popup.c | 2 +- types/xdg_shell/wlr_xdg_surface.c | 52 +++++++++++++++++++++---------- 4 files changed, 44 insertions(+), 20 deletions(-) diff --git a/tinywl/tinywl.c b/tinywl/tinywl.c index 61c089282..e91ad2834 100644 --- a/tinywl/tinywl.c +++ b/tinywl/tinywl.c @@ -122,7 +122,9 @@ static void focus_view(struct tinywl_view *view, struct wlr_surface *surface) { struct wlr_xdg_surface *previous = wlr_xdg_surface_try_from_wlr_surface(seat->keyboard_state.focused_surface); assert(previous != NULL && previous->role == WLR_XDG_SURFACE_ROLE_TOPLEVEL); - wlr_xdg_toplevel_set_activated(previous->toplevel, false); + if (previous->toplevel != NULL) { + wlr_xdg_toplevel_set_activated(previous->toplevel, false); + } } struct wlr_keyboard *keyboard = wlr_seat_get_keyboard(seat); /* Move the view to the front */ diff --git a/types/scene/xdg_shell.c b/types/scene/xdg_shell.c index eba33c86f..9e3ffb05f 100644 --- a/types/scene/xdg_shell.c +++ b/types/scene/xdg_shell.c @@ -59,8 +59,10 @@ static void scene_xdg_surface_update_position( if (xdg_surface->role == WLR_XDG_SURFACE_ROLE_POPUP) { struct wlr_xdg_popup *popup = xdg_surface->popup; - wlr_scene_node_set_position(&scene_xdg_surface->tree->node, - popup->current.geometry.x, popup->current.geometry.y); + if (popup != NULL) { + wlr_scene_node_set_position(&scene_xdg_surface->tree->node, + popup->current.geometry.x, popup->current.geometry.y); + } } } diff --git a/types/xdg_shell/wlr_xdg_popup.c b/types/xdg_shell/wlr_xdg_popup.c index d3600307a..452e9a1d1 100644 --- a/types/xdg_shell/wlr_xdg_popup.c +++ b/types/xdg_shell/wlr_xdg_popup.c @@ -485,7 +485,7 @@ void wlr_xdg_popup_get_toplevel_coords(struct wlr_xdg_popup *popup, struct wlr_surface *parent = popup->parent; struct wlr_xdg_surface *xdg_surface; while ((xdg_surface = wlr_xdg_surface_try_from_wlr_surface(parent))) { - if (xdg_surface->role == WLR_XDG_SURFACE_ROLE_POPUP) { + if (xdg_surface->role == WLR_XDG_SURFACE_ROLE_POPUP && xdg_surface->popup != NULL) { popup_sx += xdg_surface->popup->current.geometry.x; popup_sy += xdg_surface->popup->current.geometry.y; parent = xdg_surface->popup->parent; diff --git a/types/xdg_shell/wlr_xdg_surface.c b/types/xdg_shell/wlr_xdg_surface.c index eec3a495a..38280ad57 100644 --- a/types/xdg_shell/wlr_xdg_surface.c +++ b/types/xdg_shell/wlr_xdg_surface.c @@ -37,10 +37,14 @@ static void reset_xdg_surface(struct wlr_xdg_surface *surface) { switch (surface->role) { case WLR_XDG_SURFACE_ROLE_TOPLEVEL: - reset_xdg_toplevel(surface->toplevel); + if (surface->toplevel != NULL) { + reset_xdg_toplevel(surface->toplevel); + } break; case WLR_XDG_SURFACE_ROLE_POPUP: - reset_xdg_popup(surface->popup); + if (surface->popup != NULL) { + reset_xdg_popup(surface->popup); + } break; case WLR_XDG_SURFACE_ROLE_NONE: break; @@ -100,12 +104,16 @@ static void xdg_surface_handle_ack_configure(struct wl_client *client, assert(0 && "not reached"); break; case WLR_XDG_SURFACE_ROLE_TOPLEVEL: - handle_xdg_toplevel_ack_configure(surface->toplevel, - configure->toplevel_configure); + if (surface->toplevel != NULL) { + handle_xdg_toplevel_ack_configure(surface->toplevel, + configure->toplevel_configure); + } break; case WLR_XDG_SURFACE_ROLE_POPUP: - handle_xdg_popup_ack_configure(surface->popup, - configure->popup_configure); + if (surface->popup != NULL) { + handle_xdg_popup_ack_configure(surface->popup, + configure->popup_configure); + } break; } @@ -137,12 +145,16 @@ static void surface_send_configure(void *user_data) { assert(0 && "not reached"); break; case WLR_XDG_SURFACE_ROLE_TOPLEVEL: - configure->toplevel_configure = - send_xdg_toplevel_configure(surface->toplevel); + if (surface->toplevel != NULL) { + configure->toplevel_configure = + send_xdg_toplevel_configure(surface->toplevel); + } break; case WLR_XDG_SURFACE_ROLE_POPUP: - configure->popup_configure = - send_xdg_popup_configure(surface->popup); + if (surface->popup != NULL) { + configure->popup_configure = + send_xdg_popup_configure(surface->popup); + } break; } @@ -288,10 +300,14 @@ void xdg_surface_role_commit(struct wlr_surface *wlr_surface) { // inert toplevel or popup return; case WLR_XDG_SURFACE_ROLE_TOPLEVEL: - handle_xdg_toplevel_committed(surface->toplevel); + if (surface->toplevel != NULL) { + handle_xdg_toplevel_committed(surface->toplevel); + } break; case WLR_XDG_SURFACE_ROLE_POPUP: - handle_xdg_popup_committed(surface->popup); + if (surface->popup != NULL) { + handle_xdg_popup_committed(surface->popup); + } break; } @@ -391,12 +407,16 @@ void destroy_xdg_surface_role_object(struct wlr_xdg_surface *surface) { switch (surface->role) { case WLR_XDG_SURFACE_ROLE_TOPLEVEL: - destroy_xdg_toplevel(surface->toplevel); - surface->toplevel = NULL; + if (surface->toplevel != NULL) { + destroy_xdg_toplevel(surface->toplevel); + surface->toplevel = NULL; + } break; case WLR_XDG_SURFACE_ROLE_POPUP: - destroy_xdg_popup(surface->popup); - surface->popup = NULL; + if (surface->popup != NULL) { + destroy_xdg_popup(surface->popup); + surface->popup = NULL; + } break; case WLR_XDG_SURFACE_ROLE_NONE: // This space is intentionally left blank