From 890ae8d3eda9d86965f17584518191d45fa2deeb Mon Sep 17 00:00:00 2001 From: storybored379 Date: Mon, 5 Oct 2026 13:36:08 -0700 Subject: [PATCH] XFDF: gracefully ignore invalid Unicode annotation colours (#48) Use checked string slices when reading annotation colour components. Cover colour and interior-colour imports with a synthetic XFDF regression. Co-authored-by: storybored379 Co-authored-by: OpenAI Codex --- crates/xfdf/src/lib.rs | 2 +- crates/xfdf/src/tests.rs | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/crates/xfdf/src/lib.rs b/crates/xfdf/src/lib.rs index 1ccbeab4..2e6336ce 100644 --- a/crates/xfdf/src/lib.rs +++ b/crates/xfdf/src/lib.rs @@ -140,7 +140,7 @@ fn parse_hex(s: &str) -> Option<[f64; 3]> { if h.len() != 6 { return None; } - let p = |i: usize| u8::from_str_radix(&h[i..i + 2], 16).ok().map(|v| f64::from(v) / 255.0); + let p = |i: usize| h.get(i..i + 2).and_then(|s| u8::from_str_radix(s, 16).ok()).map(|v| f64::from(v) / 255.0); Some([p(0)?, p(2)?, p(4)?]) } diff --git a/crates/xfdf/src/tests.rs b/crates/xfdf/src/tests.rs index aef3cfe0..f3af5cc6 100644 --- a/crates/xfdf/src/tests.rs +++ b/crates/xfdf/src/tests.rs @@ -204,3 +204,15 @@ fn data_files_merge_into_a_spreadsheet() { assert!(lines[3].starts_with("\"Grace \"\"Amazing\"\" Hopper, RADM\",Off,"), "{csv}"); assert_eq!(data_values(b"not data"), Err(DataError::UnknownFormat)); } + +#[test] +fn unicode_annotation_colours_are_ignored_without_panicking() { + for attr in ["color", "interior-color"] { + let mut doc = blank(); + let xml = format!( + r##""## + ); + let report = import(&mut doc, xml.as_bytes()).unwrap(); + assert_eq!(report.comments, 1); + } +}