1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2024-11-24 21:57:41 +00:00

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
This commit is contained in:
David Knaack 2020-07-14 23:11:43 +02:00 committed by GitHub
parent 08b74c1672
commit 133574ab09
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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()?;
@ -591,6 +595,24 @@ mod tests {
assert_eq!(<Style>::from_config(&config).unwrap(), Color::Red.bold());
}
#[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")]);