Reject non-hex characters such as '+' in Rgba::from_hex

Found while testing crates/color/src/lib.rs:40-47

Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
NetterSilver
2026-10-22 16:06:19 +02:00
co-authored by Claude Haiku 5.5
parent b55cb5bc15
commit 52bfb9c4bb
+15 -1
View File
@@ -39,7 +39,13 @@ impl Rgba {
/// `#rrggbb` or `#rrggbbaa`.
pub fn from_hex(s: &str) -> Option<Rgba> {
let s = s.trim().trim_start_matches('#');
let p = |i: usize| u8::from_str_radix(s.get(i..i + 2)?, 16).ok();
let p = |i: usize| {
let pair = s.get(i..i + 2)?;
if !pair.bytes().all(|b| b.is_ascii_hexdigit()) {
return None;
}
u8::from_str_radix(pair, 16).ok()
};
match s.len() {
6 => Some(Rgba::from_u8(p(0)?, p(2)?, p(4)?)),
8 => Some(Rgba { a: p(6)? as f32 / 255.0, ..Rgba::from_u8(p(0)?, p(2)?, p(4)?) }),
@@ -262,6 +268,14 @@ mod tests {
assert!(r > 0.99 && g < 0.01 && b < 0.01, "{r} {g} {b}");
}
#[test]
fn rejects_signed_hex_pairs() {
assert!(Rgba::from_hex("#+1+2+3").is_none());
assert!(Rgba::from_hex("#1+2+3+").is_none());
assert!(Rgba::from_hex("#ff00+0ff").is_none());
assert!(Rgba::from_hex("#a1B2c3").is_some());
}
#[test]
fn srgb_roundtrip() {
for i in 0..=100 {