From fc79b17dced91c6b5626b557f9255bb15cd45ccc Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Wed, 15 Jun 2022 18:12:09 +1000 Subject: GHOST/Wayland: add NULL pointer checks on window access There were a few callers that missed checking a NULL return value which can happen in rare cases. --- intern/ghost/intern/GHOST_SystemWayland.cpp | 87 ++++++++++++++++------------- 1 file changed, 47 insertions(+), 40 deletions(-) diff --git a/intern/ghost/intern/GHOST_SystemWayland.cpp b/intern/ghost/intern/GHOST_SystemWayland.cpp index 635c5bef50e..4ecaf2bed44 100644 --- a/intern/ghost/intern/GHOST_SystemWayland.cpp +++ b/intern/ghost/intern/GHOST_SystemWayland.cpp @@ -565,7 +565,7 @@ static void relative_pointer_handle_relative_motion( { input_t *input = static_cast(data); GHOST_WindowWayland *win = window_from_surface(input->focus_pointer); - if (win == nullptr) { + if (!win) { return; } const wl_fixed_t scale = win->scale(); @@ -1288,13 +1288,6 @@ static void tablet_tool_handle_proximity_in(void *data, input->data_source_serial = serial; /* Update #GHOST_TabletData. */ - GHOST_WindowWayland *win = window_from_surface(input->focus_tablet); - if (!win) { - return; - } - - win->activate(); - GHOST_TabletData &td = tool_input->data; /* Reset, to avoid using stale tilt/pressure. */ td.Xtilt = 0.0f; @@ -1302,6 +1295,12 @@ static void tablet_tool_handle_proximity_in(void *data, /* In case pressure isn't supported. */ td.Pressure = 1.0f; + GHOST_WindowWayland *win = window_from_surface(input->focus_tablet); + if (!win) { + return; + } + win->activate(); + win->setCursorShape(win->getCursorShape()); } static void tablet_tool_handle_proximity_out(void *data, @@ -1309,10 +1308,12 @@ static void tablet_tool_handle_proximity_out(void *data, { tablet_tool_input_t *tool_input = static_cast(data); input_t *input = tool_input->input; - GHOST_WindowWayland *win = window_from_surface(input->focus_tablet); - input->focus_tablet = nullptr; + GHOST_WindowWayland *win = window_from_surface(input->focus_tablet); + if (!win) { + return; + } win->setCursorShape(win->getCursorShape()); } @@ -2714,39 +2715,45 @@ GHOST_TSuccess GHOST_SystemWayland::setCursorGrab(const GHOST_TGrabCursorMode mo if (input->locked_pointer) { /* Request location to restore to. */ if (mode_current == GHOST_kGrabWrap) { + /* The chance this fails is _very_ low. */ GHOST_WindowWayland *win = window_from_surface(surface); - GHOST_Rect bounds; - int32_t xy_new[2] = {input->xy[0], input->xy[1]}; - - /* Fallback to window bounds. */ - if (win->getCursorGrabBounds(bounds) == GHOST_kFailure) { - win->getClientBounds(bounds); + if (!win) { + GHOST_PRINT("could not find window from surface when un-grabbing!" << std::endl); } - - const int scale = win->scale(); - - bounds.m_l = wl_fixed_from_int(bounds.m_l) / scale; - bounds.m_t = wl_fixed_from_int(bounds.m_t) / scale; - bounds.m_r = wl_fixed_from_int(bounds.m_r) / scale; - bounds.m_b = wl_fixed_from_int(bounds.m_b) / scale; - - bounds.wrapPoint(xy_new[0], xy_new[1], 0, win->getCursorGrabAxis()); - - /* Push an event so the new location is registered. */ - if ((xy_new[0] != input->xy[0]) || (xy_new[1] != input->xy[1])) { - input->system->pushEvent(new GHOST_EventCursor(input->system->getMilliSeconds(), - GHOST_kEventCursorMove, - win, - wl_fixed_to_int(scale * xy_new[0]), - wl_fixed_to_int(scale * xy_new[1]), - GHOST_TABLET_DATA_NONE)); + else { + GHOST_Rect bounds; + int32_t xy_new[2] = {input->xy[0], input->xy[1]}; + + /* Fallback to window bounds. */ + if (win->getCursorGrabBounds(bounds) == GHOST_kFailure) { + win->getClientBounds(bounds); + } + + const int scale = win->scale(); + + bounds.m_l = wl_fixed_from_int(bounds.m_l) / scale; + bounds.m_t = wl_fixed_from_int(bounds.m_t) / scale; + bounds.m_r = wl_fixed_from_int(bounds.m_r) / scale; + bounds.m_b = wl_fixed_from_int(bounds.m_b) / scale; + + bounds.wrapPoint(xy_new[0], xy_new[1], 0, win->getCursorGrabAxis()); + + /* Push an event so the new location is registered. */ + if ((xy_new[0] != input->xy[0]) || (xy_new[1] != input->xy[1])) { + input->system->pushEvent(new GHOST_EventCursor(input->system->getMilliSeconds(), + GHOST_kEventCursorMove, + win, + wl_fixed_to_int(scale * xy_new[0]), + wl_fixed_to_int(scale * xy_new[1]), + GHOST_TABLET_DATA_NONE)); + } + input->xy[0] = xy_new[0]; + input->xy[1] = xy_new[1]; + + zwp_locked_pointer_v1_set_cursor_position_hint( + input->locked_pointer, xy_new[0], xy_new[1]); + wl_surface_commit(surface); } - input->xy[0] = xy_new[0]; - input->xy[1] = xy_new[1]; - - zwp_locked_pointer_v1_set_cursor_position_hint( - input->locked_pointer, xy_new[0], xy_new[1]); - wl_surface_commit(surface); } zwp_locked_pointer_v1_destroy(input->locked_pointer); -- cgit v1.2.3