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
This commit is contained in:
Wez Furlong committed 2020-05-29 07:15:45 -07:00
1 parent 9e89a557d4
commit 92fdf043b8
1 file changed
+2 -1
+2 -1
View File
@@ -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<RgbColor> {
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();