From 92fdf043b804316da786ede4d9333c66ed857600 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Fri, 29 May 2020 07:15:45 -0700 Subject: [PATCH] termwiz: fix panic in RgbColor::from_rgb_str w/ empty string Need to check the len before looking at the first character! closes https://github.com/wez/wezterm/issues/198 --- termwiz/src/color.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/termwiz/src/color.rs b/termwiz/src/color.rs index 26395c4df..a6ff89195 100644 --- a/termwiz/src/color.rs +++ b/termwiz/src/color.rs @@ -141,7 +141,7 @@ impl RgbColor { /// Construct a color from a string of the form `#RRGGBB` where /// R, G and B are all hex digits. pub fn from_rgb_str(s: &str) -> Option { - if s.as_bytes()[0] == b'#' && s.len() == 7 { + if s.len() == 7 && s.as_bytes()[0] == b'#' { let mut chars = s.chars().skip(1); macro_rules! digit { @@ -318,6 +318,7 @@ mod tests { #[test] fn from_rgb() { + assert!(RgbColor::from_rgb_str("").is_none()); assert!(RgbColor::from_rgb_str("#xyxyxy").is_none()); let black = RgbColor::from_rgb_str("#000000").unwrap();