swayfx/sway
Thomas Hebb a235794a96 input/seat: unset has_focus when focus_stack becomes empty
We currently track the focus of a seat in two ways: we use a list called
focus_stack to track the order in which nodes have been focused, with
the first node representing what's currently focused, and we use a
variable called has_focus to indicate whether anything has focus--i.e.
whether we should actually treat that first node as focused at any given
time.

In a number of places, we treat has_focus as implying that a focused
node exists. If it's true, we attempt to dereference the return value of
seat_get_focus(), our helper function for getting the first node in
focus_list, with no further checks. But this isn't quite correct with
the current implementation of seat_get_focus(): not only does it return
NULL when has_focus is false, it also returns NULL when focus_stack
contains no items.

In most cases, focus_stack never becomes empty and so this doesn't
matter at all. Since focus_stack stores a history of focused nodes, we
rarely remove nodes from it. The exception to this is when a node itself
goes away. In that case, we call seat_node_destroy() to remove it from
focus_stack and free it. But we don't unset has_focus if we've removed
the final node! This lets us get into a state where has_focus is true
but seat_get_focus() returns NULL, leading to a segfault when we try to
dereference it.

Fix the issue both by updating has_focus in seat_node_destroy() and by
adding an assertion in seat_get_focus() that ensures focus_stack and
has_focus are in sync, which will make it easier to track down similar
issues in the future.

Fixes #6395.

[1] There's some discussion in #1585 from when this was implemented
about whether has_focus is actually necessary; it's possible we could
remove it entirely, but for the moment this is the architecture we have.

(cherry picked from commit 921b0a863382b70234aeb4bd589c10328e9ff042)
2022-01-07 15:09:24 +01:00
..
commands Add focused_tab_title 2021-12-10 16:09:29 +01:00
config sway: replace noop_output by fallback_output 2021-11-25 17:48:34 +01:00
desktop output: change output::destroy to output::disable 2021-12-17 14:39:48 +01:00
input input/seat: unset has_focus when focus_stack becomes empty 2022-01-07 15:09:24 +01:00
tree Add cairo_image_surface_create error handling 2021-12-23 12:16:37 +01:00
commands.c Add focused_tab_title 2021-12-10 16:09:29 +01:00
config.c Add focused_tab_title 2021-12-10 16:09:29 +01:00
criteria.c container: Move pending state to state struct 2021-02-16 22:05:00 -05:00
decoration.c Fix double free when unmapping any view 2018-11-15 15:22:09 +10:00
ipc-json.c Update wlr_box includes 2021-07-07 11:29:14 +02:00
ipc-server.c sway: replace noop_output by fallback_output 2021-11-25 17:48:34 +01:00
main.c sway: allow IPCs on proprietary drivers 2021-11-14 10:43:09 +03:00
meson.build Add 'output render_bit_depth [8|10]' command 2021-11-23 15:51:54 +01:00
server.c Add support for linux-dmabuf surface hints 2021-12-13 09:59:33 -06:00
sway-bar.5.scd man: update Pango font description URL 2021-08-04 02:15:04 +02:00
sway-input.5.scd sway-input.5.scd: Fix spelling errors 2021-10-02 10:21:28 +02:00
sway-ipc.7.scd sway-ipc.7.scd: fix typo 2021-09-19 17:28:37 +02:00
sway-output.5.scd Add 'output render_bit_depth [8|10]' command 2021-11-23 15:51:54 +01:00
sway.1.scd man: update maintainer 2021-01-08 09:33:51 +01:00
sway.5.scd Add focused_tab_title 2021-12-10 16:09:29 +01:00
swaynag.c Bump RLIMIT_NOFILE 2021-10-30 08:19:35 -06:00
xdg_activation_v1.c Implement xdg-activation-v1 2021-06-02 19:39:40 +02:00
xdg_decoration.c Fix compile after wlroots commit c3e54021f8bbf3ec1c1e5906459be0ae85f5fb8c 2021-10-07 21:29:37 +03:00