From 188febfc0591e99d1577d3032076eac9ca9a7e33 Mon Sep 17 00:00:00 2001 From: Thomas Linford Date: Thu, 28 Apr 2022 11:45:21 +0200 Subject: [PATCH] fix(stability): avoid link handler panic on bad index (#1356) root cause needs investigating, but for now fix the crash. --- zellij-server/src/panes/link_handler.rs | 43 ++++++++++++++++++------- 1 file changed, 32 insertions(+), 11 deletions(-) diff --git a/zellij-server/src/panes/link_handler.rs b/zellij-server/src/panes/link_handler.rs index 3de81c3a..e6119a6c 100644 --- a/zellij-server/src/panes/link_handler.rs +++ b/zellij-server/src/panes/link_handler.rs @@ -50,17 +50,31 @@ impl LinkHandler { } pub fn output_osc8(&self, link_anchor: Option) -> Option { - link_anchor.map(|link| match link { - LinkAnchor::Start(index) => { - let link = self.links.get(&index).unwrap(); - let id = link - .id - .as_ref() - .map_or("".to_string(), |id| format!("id={}", id)); - format!("\u{1b}]8;{};{}{}", id, link.uri, TERMINATOR) - } - LinkAnchor::End => format!("\u{1b}]8;;{}", TERMINATOR), - }) + link_anchor + .map(|link| match link { + LinkAnchor::Start(index) => { + let link = self.links.get(&index); + + let output = link.map(|link| { + let id = link + .id + .as_ref() + .map_or("".to_string(), |id| format!("id={}", id)); + format!("\u{1b}]8;{};{}{}", id, link.uri, TERMINATOR) + }); + + if output.is_none() { + log::warn!( + "attempted to output osc8 link start, but id: {} was not found!", + index + ); + } + + output + } + LinkAnchor::End => Some(format!("\u{1b}]8;;{}", TERMINATOR)), + }) + .flatten() } } @@ -108,4 +122,11 @@ mod tests { let expected = format!("\u{1b}]8;;{}", TERMINATOR); assert_eq!(link_handler.output_osc8(anchor).unwrap(), expected); } + + #[test] + fn return_none_on_missing_link_id() { + let link_handler = LinkHandler::default(); + let anchor = LinkAnchor::Start(100); + assert_eq!(link_handler.output_osc8(Some(anchor)), None); + } }