fix(render): an unreadable CMap token counts once against the skip limit, with unit tests (hayro-cmap patch 3, hayro-postscript patch 2)

- `Scanner::parse_object_or_skip` skips whitespace and comments before
  taking its start offset, so a token after a space fails once, not
  twice, whoever calls it. (The CMap scan already skipped whitespace in
  `at_end`, so CMap results do not change.)
- Unit tests in hayro-postscript (stepping over a token, a failed read
  not read again, never past the end of the data) and in hayro-cmap
  (bfchar and bfrange after a stray `>` or a spaced name, the 1,024-token
  limit at its boundary, a CMap of unreadable tokens only failing).
- The render test checks a bfrange entry after the bad token, a run past
  the limit before the mappings, and a CMap of unreadable tokens only;
  its wall-clock assertion is gone, the no-re-read property is the
  deterministic hayro-postscript test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
mtlcl410
2026-11-07 06:52:15 +08:00
co-authored by Claude Opus 5.5
parent 46ed507680
commit 0647a4e724
5 changed files with 120 additions and 17 deletions
+13 -8
View File
@@ -9630,7 +9630,7 @@ fn a_stream_with_a_crypt_filter_of_its_own_is_drawn() {
#[test]
fn a_to_unicode_cmap_reads_its_mappings_past_a_token_it_cannot_parse() {
use hayro::hayro_interpret::hayro_cmap::{BfString, CMap};
let body = "1 begincodespacerange\n<0000> <FFFF>\nendcodespacerange\n2 beginbfchar\n<002B> <0048>\n<0048> <0065>\nendbfchar\nendcmap\n";
let body = "1 begincodespacerange\n<0000> <FFFF>\nendcodespacerange\n2 beginbfchar\n<002B> <0048>\n<0048> <0065>\nendbfchar\n1 beginbfrange\n<0050> <0052> <0061>\nendbfrange\nendcmap\n";
let stray = format!(
"/CIDInit /ProcSet findresource begin 12 dict begin begincmap\n/CIDSystemInfo <</Registry (Adobe) /Ordering (UC>) /Supplement 0>> def\n/CMapName /Adobe-Identity-UC> def\n/CMapType 2 def\n{body}"
);
@@ -9641,15 +9641,20 @@ fn a_to_unicode_cmap_reads_its_mappings_past_a_token_it_cannot_parse() {
let cmap = CMap::parse(data.as_bytes(), |_| None).unwrap_or_else(|| panic!("{what}: parsed"));
assert_eq!(cmap.lookup_bf_string(0x2B), Some(BfString::Char('H')), "{what}");
assert_eq!(cmap.lookup_bf_string(0x48), Some(BfString::Char('e')), "{what}");
assert_eq!(cmap.lookup_bf_string(0x51), Some(BfString::Char('b')), "{what}: bfrange");
}
// Unreadable tokens are stepped over a bounded number of times: a long run of them ends
// the scan rather than costing its length squared, and what came before is kept.
// Unreadable tokens are stepped over a bounded number of times (1,024 per CMap): a long run
// of them ends the scan, and the mappings read before it are kept.
let flood = format!("{body}{}", "-".repeat(1 << 16));
assert!(CMap::parse(flood.as_bytes(), |_| None).is_some_and(|c| c.lookup_bf_string(0x2B).is_some()));
// What a failed read went through is not read again: unclosed strings, each of which reads
// to the end of the data, cost one pass over it, not one pass each.
let unclosed = format!("{body}{}{}", "(".repeat(4096), "x".repeat(1 << 23));
let started = std::time::Instant::now();
// A run past the limit before the mappings ends the scan before them, and a CMap that read
// no mappings, like one of unreadable tokens only, reports failure.
let flood_first = format!("{}\n{body}", "-".repeat(1025));
assert!(CMap::parse(flood_first.as_bytes(), |_| None).is_none());
assert!(CMap::parse("-".repeat(1 << 16).as_bytes(), |_| None).is_none());
// Unclosed strings after the mappings: the scan ends and keeps them. (That the first one,
// which reads to the end of the data, is not read again for each `(` inside it is
// hayro-postscript's `parse_object_or_skip_does_not_read_a_failed_read_again`.)
let unclosed = format!("{body}{}{}", "(".repeat(4096), "x".repeat(1 << 16));
assert!(CMap::parse(unclosed.as_bytes(), |_| None).is_some_and(|c| c.lookup_bf_string(0x48).is_some()));
assert!(started.elapsed() < std::time::Duration::from_secs(3), "{:?}", started.elapsed());
}
+2 -2
View File
File diff suppressed because one or more lines are too long
+43
View File
@@ -1176,6 +1176,49 @@ endbfchar
assert!(cmap.lookup_bf_string(0x41).is_some());
}
// PdfCraft patch (3): a token the scanner can't read (a stray `>` after a CMap name, a name
// with spaces in it) is stepped over, so the mappings after it are still read; at most
// `MAX_SKIPPED` of them per CMap.
#[test]
fn mappings_after_an_unreadable_token_are_read() {
for header in [
&b"/CMapName /Adobe-Identity-UC> def"[..],
b"/CMapName /-usr-share-fonts-Panton Family - Panton.otf,000-UTF16 def",
] {
let mut data = header.to_vec();
data.extend_from_slice(
b"\n1 beginbfchar <41> <0058> endbfchar\n1 beginbfrange <50> <52> <0061> endbfrange",
);
let cmap = parse_with_preamble(&data);
assert_eq!(cmap.lookup_bf_string(0x41), Some(BfString::Char('X')));
assert_eq!(cmap.lookup_bf_string(0x51), Some(BfString::Char('b')));
}
}
/// A CMap with `count` unreadable tokens, each after a space, before one `bfchar` mapping.
fn mapping_after_unreadable_tokens(count: usize) -> Option<CMap> {
let mut data = PREAMBLE.to_vec();
data.extend_from_slice(&b" >".repeat(count));
data.extend_from_slice(b"\n1 beginbfchar <41> <0058> endbfchar");
CMap::parse(&data, |_| None)
}
#[test]
fn the_scan_steps_over_at_most_max_skipped_unreadable_tokens() {
let limit = parse::MAX_SKIPPED;
let cmap = mapping_after_unreadable_tokens(limit).expect("read up to the limit");
assert_eq!(cmap.lookup_bf_string(0x41), Some(BfString::Char('X')));
// One more ends the scan before the mapping, and a CMap with no mappings fails.
assert!(mapping_after_unreadable_tokens(limit + 1).is_none());
}
#[test]
fn a_cmap_of_unreadable_tokens_only_reports_failure() {
assert!(CMap::parse(b"> ) >", |_| None).is_none());
assert!(CMap::parse(&b" >".repeat(4 * parse::MAX_SKIPPED), |_| None).is_none());
}
#[test]
fn bad_bfchar_code_keeps_later_entries_aligned() {
// The malformed 5-byte code still has a destination object; both
+1 -1
View File
@@ -16,7 +16,7 @@ struct Context<F> {
}
/// PdfCraft patch (3): most unreadable tokens one CMap may step over before its scan stops.
const MAX_SKIPPED: usize = 1024;
pub(crate) const MAX_SKIPPED: usize = 1024;
pub(crate) fn parse_inner<'a>(
data: &[u8],
+61 -6
View File
@@ -61,16 +61,21 @@ impl<'a> Scanner<'a> {
}
/// PdfCraft patch (2): parse the next object; when it can't be read, read on from where
/// the failed read stopped, or, when it read nothing, from one byte past where it started,
/// so a caller can read on past a token it can't parse (a stray `>` in a name, a CMap name
/// with spaces in it) instead of stopping there. Every failure moves forward, and what a
/// failed read went through (an unclosed string, to the end of the data) is not read
/// again, so a loop over this ends and reads the data about once.
/// the failed read stopped, or, when it read nothing, from one byte past where its token
/// starts, so a caller can read on past a token it can't parse (a stray `>` in a name, a
/// CMap name with spaces in it) instead of stopping there. Every failure moves forward, one
/// failure per unreadable token, and what a failed read went through (an unclosed string,
/// to the end of the data) is not read again, so a loop over this ends and reads the data
/// about once.
pub fn parse_object_or_skip(&mut self) -> Result<Object<'a>> {
// The whitespace and comments before the token are not the token: skipped first, they
// don't count as progress, so a token after a space fails once, not twice.
object::skip_whitespace_and_comments(&mut self.reader);
let start = self.reader.offset();
let read = object::read(&mut self.reader);
if read.is_err() && self.reader.offset() <= start {
self.reader.jump(start.saturating_add(1).min(self.reader.data.len()));
self.reader
.jump(start.saturating_add(1).min(self.reader.data.len()));
}
read
}
@@ -191,4 +196,54 @@ endcmap"#;
let mut scanner = Scanner::new(b"42 ");
assert!(scanner.parse_name().is_err());
}
/// PdfCraft patch (2) test: a token that can't be read fails once and is stepped over,
/// whether or not whitespace or a comment comes before it, and the objects after it read.
#[test]
fn parse_object_or_skip_steps_over_an_unreadable_token() {
let mut s = Scanner::new(b"/A> /B % note\n > /C");
assert_eq!(
s.parse_object_or_skip(),
Ok(Object::Name(Name::new(b"A", true)))
);
assert!(s.parse_object_or_skip().is_err());
assert_eq!(
s.parse_object_or_skip(),
Ok(Object::Name(Name::new(b"B", true)))
);
assert!(s.parse_object_or_skip().is_err());
assert_eq!(
s.parse_object_or_skip(),
Ok(Object::Name(Name::new(b"C", true)))
);
assert!(s.at_end());
}
/// PdfCraft patch (2) test: what a failed read went through is not read again. An unclosed
/// string reads to the end of the data, so one failure leaves nothing to read; the nested
/// `(` inside it are not each read to the end once more.
#[test]
fn parse_object_or_skip_does_not_read_a_failed_read_again() {
let mut s = Scanner::new(b"/A (((( x");
assert_eq!(
s.parse_object_or_skip(),
Ok(Object::Name(Name::new(b"A", true)))
);
assert!(s.parse_object_or_skip().is_err());
assert!(s.at_end());
}
/// PdfCraft patch (2) test: at the end of the data a read fails and the scanner stays at
/// the end; it never steps past the data.
#[test]
fn parse_object_or_skip_stays_at_the_end() {
for input in [&b""[..], b" ", b">"] {
let mut s = Scanner::new(input);
for _ in 0..3 {
assert!(s.parse_object_or_skip().is_err());
assert!(s.reader.offset() <= input.len());
}
assert!(s.at_end());
}
}
}