From 7d2fbd959a2242db3933c677028b5f825bb23712 Mon Sep 17 00:00:00 2001 From: NetterSilver <133535268+Silvertree2010@users.noreply.github.com> Date: Sat, 24 Oct 2026 07:49:38 +0200 Subject: [PATCH] Reject repeated leading '#' in Rgb::parse_hex (#1746) Found while testing crates/color/src/lib.rs:24 Co-authored-by: Claude Haiku 5.5 --- crates/color/src/lib.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/crates/color/src/lib.rs b/crates/color/src/lib.rs index 9e3ddd0b..2d60e3ac 100644 --- a/crates/color/src/lib.rs +++ b/crates/color/src/lib.rs @@ -21,7 +21,8 @@ impl Rgb { format!("#{:02x}{:02x}{:02x}", self.0, self.1, self.2) } pub fn parse_hex(s: &str) -> Option { - let s = s.trim().trim_start_matches('#'); + let s = s.trim(); + let s = s.strip_prefix('#').unwrap_or(s); if s.len() != 6 || !s.chars().all(|c| c.is_ascii_hexdigit()) { return None; } @@ -233,6 +234,15 @@ mod tests { assert_eq!(Rgb::parse_hex("#012345"), Some(Rgb(1, 35, 69))); } + #[test] + fn parse_hex_rejects_repeated_hash() { + assert_eq!(Rgb::parse_hex("##ff0000"), None); + assert_eq!(Rgb::parse_hex("###ff0000"), None); + assert_eq!(Color::parse("##ff0000"), None); + assert_eq!(Rgb::parse_hex("#ff0000"), Some(Rgb(255, 0, 0))); + assert_eq!(Rgb::parse_hex("ff0000"), Some(Rgb(255, 0, 0))); + } + #[test] fn palette_known_entries() { assert_eq!(aci_rgb(1), Rgb(255, 0, 0));