forge
Explore
Log in

Don't you dare go Hollow

Guard against removed outputs in several places

Browse files at this commit

d09fa2709c84a8b55cdb3de1982a64a5f9a48194Ivan MolodetskikhParent: 2c3315a
Output::from_resource() succeeds even after the global has been disabled
and removed from niri. Clients operating on these disabled outputs could
cause panics in several places because niri assumed the output existed.
src/handlers/layer_shell.rsmodified+1 -2
@@ -1,6 +1,5 @@
1 use smithay::delegate_layer_shell;1 use smithay::delegate_layer_shell;
2 use smithay::desktop::{layer_map_for_output, LayerSurface, PopupKind, WindowSurfaceType};2 use smithay::desktop::{layer_map_for_output, LayerSurface, PopupKind, WindowSurfaceType};
3-use smithay::output::Output;
4 use smithay::reexports::wayland_server::protocol::wl_output::WlOutput;3 use smithay::reexports::wayland_server::protocol::wl_output::WlOutput;
5 use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface;4 use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface;
6 use smithay::wayland::compositor::{add_pre_commit_hook, get_parent, with_states, HookId};5 use smithay::wayland::compositor::{add_pre_commit_hook, get_parent, with_states, HookId};
@@ -27,7 +26,7 @@ impl WlrLayerShellHandler for State {
27 namespace: String,26 namespace: String,
28 ) {27 ) {
29 let output = if let Some(wl_output) = &wl_output {28 let output = if let Some(wl_output) = &wl_output {
30- Output::from_resource(wl_output)29+ self.niri.output_from_resource(wl_output)
31 } else {30 } else {
32 self.niri.layout.active_output().cloned()31 self.niri.layout.active_output().cloned()
33 };32 };
src/handlers/mod.rsmodified+10 -2
@@ -466,7 +466,7 @@ impl SessionLockHandler for State {
466 }466 }
467 467
468 fn new_surface(&mut self, surface: LockSurface, output: WlOutput) {468 fn new_surface(&mut self, surface: LockSurface, output: WlOutput) {
469- let Some(output) = Output::from_resource(&output) else {469+ let Some(output) = self.niri.output_from_resource(&output) else {
470 warn!("no Output matching WlOutput");470 warn!("no Output matching WlOutput");
471 return;471 return;
472 };472 };
@@ -551,7 +551,9 @@ impl ForeignToplevelHandler for State {
551 {551 {
552 let window = mapped.window.clone();552 let window = mapped.window.clone();
553 553
554- if let Some(requested_output) = wl_output.as_ref().and_then(Output::from_resource) {554+ if let Some(requested_output) =
555+ wl_output.and_then(|o| self.niri.output_from_resource(&o))
556+ {
555 if Some(&requested_output) != current_output {557 if Some(&requested_output) != current_output {
556 self.niri.layout.move_to_output(558 self.niri.layout.move_to_output(
557 Some(&window),559 Some(&window),
@@ -627,6 +629,12 @@ delegate_ext_workspace!(State);
627 629
628 impl ScreencopyHandler for State {630 impl ScreencopyHandler for State {
629 fn frame(&mut self, manager: &ZwlrScreencopyManagerV1, screencopy: Screencopy) {631 fn frame(&mut self, manager: &ZwlrScreencopyManagerV1, screencopy: Screencopy) {
632+ // This can happen if the output was removed before this was called.
633+ if !self.niri.output_exists(screencopy.output()) {
634+ trace!("screencopy output no longer exists");
635+ return;
636+ }
637+
630 // If with_damage then push it onto the queue for redraw of the output,638 // If with_damage then push it onto the queue for redraw of the output,
631 // otherwise render it immediately.639 // otherwise render it immediately.
632 if screencopy.with_damage() {640 if screencopy.with_damage() {
src/handlers/xdg_shell.rsmodified+1 -1
@@ -612,7 +612,7 @@ impl XdgShellHandler for State {
612 toplevel: ToplevelSurface,612 toplevel: ToplevelSurface,
613 wl_output: Option<wl_output::WlOutput>,613 wl_output: Option<wl_output::WlOutput>,
614 ) {614 ) {
615- let requested_output = wl_output.as_ref().and_then(Output::from_resource);615+ let requested_output = wl_output.and_then(|o| self.niri.output_from_resource(&o));
616 616
617 if let Some((mapped, current_output)) = self617 if let Some((mapped, current_output)) = self
618 .niri618 .niri
src/input/mod.rsmodified+2 -0
@@ -295,6 +295,7 @@ impl State {
295 I::Device: 'static,295 I::Device: 'static,
296 {296 {
297 let device_output = event.device().output(self);297 let device_output = event.device().output(self);
298+ let device_output = device_output.filter(|output| self.niri.output_exists(output));
298 let device_output = device_output.as_ref();299 let device_output = device_output.as_ref();
299 let (target_geo, keep_ratio, px, transform) =300 let (target_geo, keep_ratio, px, transform) =
300 if let Some(output) = device_output.or_else(|| self.niri.output_for_tablet()) {301 if let Some(output) = device_output.or_else(|| self.niri.output_for_tablet()) {
@@ -4039,6 +4040,7 @@ impl State {
4039 fallback_output: Option<&Output>,4040 fallback_output: Option<&Output>,
4040 ) -> Option<Point<f64, Logical>> {4041 ) -> Option<Point<f64, Logical>> {
4041 let output = evt.device().output(self);4042 let output = evt.device().output(self);
4043+ let output = output.filter(|output| self.niri.output_exists(output));
4042 let output = output.as_ref().or(fallback_output)?;4044 let output = output.as_ref().or(fallback_output)?;
4043 let output_geo = self.niri.global_space.output_geometry(output).unwrap();4045 let output_geo = self.niri.global_space.output_geometry(output).unwrap();
4044 let transform = output.current_transform();4046 let transform = output.current_transform();
src/niri.rsmodified+15 -0
@@ -110,6 +110,7 @@ use smithay::wayland::viewporter::ViewporterState;
110 use smithay::wayland::virtual_keyboard::VirtualKeyboardManagerState;110 use smithay::wayland::virtual_keyboard::VirtualKeyboardManagerState;
111 use smithay::wayland::xdg_activation::XdgActivationState;111 use smithay::wayland::xdg_activation::XdgActivationState;
112 use smithay::wayland::xdg_foreign::XdgForeignState;112 use smithay::wayland::xdg_foreign::XdgForeignState;
113+use wayland_server::protocol::wl_output::WlOutput;
113 114
114 #[cfg(feature = "dbus")]115 #[cfg(feature = "dbus")]
115 use crate::a11y::A11y;116 use crate::a11y::A11y;
@@ -2874,6 +2875,20 @@ impl Niri {
2874 self.reposition_outputs(Some(&output));2875 self.reposition_outputs(Some(&output));
2875 }2876 }
2876 2877
2878+ pub fn output_exists(&self, output: &Output) -> bool {
2879+ self.output_state.contains_key(output)
2880+ }
2881+
2882+ /// Converts a `WlOutput` to a corresponding `Output` if it exists.
2883+ ///
2884+ /// Compared to raw `Output::from_resource`, this method also verifies that the output still
2885+ /// exists in niri. Right after the output global is disabled, but before it is removed for
2886+ /// good, `Output::from_resource` will succeed, but since niri already forgot the output,
2887+ /// accessing it can cause logic bugs.
2888+ pub fn output_from_resource(&self, wl_output: &WlOutput) -> Option<Output> {
2889+ Output::from_resource(wl_output).filter(|output| self.output_exists(output))
2890+ }
2891+
2877 pub fn remove_output(&mut self, output: &Output) {2892 pub fn remove_output(&mut self, output: &Output) {
2878 for layer in layer_map_for_output(output).layers() {2893 for layer in layer_map_for_output(output).layers() {
2879 layer.layer_surface().send_close();2894 layer.layer_surface().send_close();