diff options
author | David Knaack <davidkna@users.noreply.github.com> | 2020-07-14 23:11:43 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-07-14 23:11:43 +0200 |
commit | 133574ab0900b6f6d262357822f6b5547de47640 (patch) | |
tree | f5b7a0dfcd832add9f5d013934e9565b15b77b22 /src/config.rs | |
parent | 08b74c1672d886de2112223e0e57d3e36f376f15 (diff) |
fix(config): don't panic when hex color is too short (#1473)
* fix(config): don't panic when hex color is too short
* check length instead of using get
Diffstat (limited to 'src/config.rs')
-rw-r--r-- | src/config.rs | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/src/config.rs b/src/config.rs index be55af0f8..9099011aa 100644 --- a/src/config.rs +++ b/src/config.rs @@ -384,6 +384,10 @@ fn parse_color_string(color_string: &str) -> Option<ansi_term::Color> { "Attempting to read hexadecimal color string: {}", color_string ); + if color_string.len() != 7 { + log::debug!("Could not parse hexadecimal string: {}", color_string); + return None; + } let r: u8 = u8::from_str_radix(&color_string[1..3], 16).ok()?; let g: u8 = u8::from_str_radix(&color_string[3..5], 16).ok()?; let b: u8 = u8::from_str_radix(&color_string[5..7], 16).ok()?; @@ -592,6 +596,24 @@ mod tests { } #[test] + fn test_from_hex_color_style() { + let config = Value::from("#00000"); + assert_eq!(<Style>::from_config(&config), None); + + let config = Value::from("#0000000"); + assert_eq!(<Style>::from_config(&config), None); + + let config = Value::from("#NOTHEX"); + assert_eq!(<Style>::from_config(&config), None); + + let config = Value::from("#a12BcD"); + assert_eq!( + <Style>::from_config(&config).unwrap(), + Color::RGB(0xA1, 0x2B, 0xCD).into() + ); + } + + #[test] fn test_from_vec() { let config: Value = Value::Array(vec![Value::from("S")]); assert_eq!(<Vec<&str>>::from_config(&config).unwrap(), vec!["S"]); |