History

fix: `Color/Name`::from_str()` won't panic when encountering unicode values.

Previously it would try to byte-index a presumed ascii string to convert hex to RGB, which could panic if the string wasn't actually ascii. Now it validates that the characters to convert are actually on a character boundary.

Sebastian Thiel committed 2 years ago

Commit f9d566f1 parent 3f4db4a2 files · +2 −1Browse files

Unified · Split

2 files
  1. gix-config-value/src/color.rs +1 −1
  2. gix-config-value/tests/value/color.rs +1 −0
gix-config-value/src/color.rs+1−1
@@ -204,7 +204,7 @@ impl FromStr for Name { · 203 unmodified lines
204204 }
205205
206206 if let Some(s) = s.strip_prefix('#') {
207− if s.len() == 6 {
207+ if s.len() == 6 && s.is_char_boundary(2) && s.is_char_boundary(4) && s.is_char_boundary(6) {
208208 let rgb = (
209209 u8::from_str_radix(&s[..2], 16),
210210 u8::from_str_radix(&s[2..4], 16),
gix-config-value/tests/value/color.rs+1−0
@@ -54,6 +54,7 @@ mod name { · 53 unmodified lines
5454 assert!(Name::from_str("#").is_err());
5555 assert!(Name::from_str("#fff").is_err());
5656 assert!(Name::from_str("#gggggg").is_err());
57+ assert!(Name::from_str("#=»©=").is_err());
5758 }
5859 }
5960