Files
pdfcraft/vendor/lopdf/tests/page_content.rs
Brandon ThomasandClaude Opus 5.5 1449b71a19 Never crash: bound the allocations behind the nightly fuzz runner's out-of-memory deaths
Fuzzed inputs drove one check-one process to 2-6 GB (the 16 GB CI runner
running two of them was killed every night). Three causes, each with a test:
- lopdf (bootstrap inspector) sized PNG predictor rows from /Columns before
  reading data: /Columns 4294967295 allocated two 4 GiB rows. Vendored lopdf
  0.45.0 refuses rows longer than the data (and uses saturating row widths);
  its load-time stream decoding also gets a 256 MiB limit (it had none).
- hayro sized a tiling pattern's cell pixmap from /XStep x scale, bounded only
  by u16: a step far beyond /BBox allocated 2 GB. The vendored patch keeps
  the cell within 3000 px a side, lowering the resolution instead.
- cos decoded cross-reference streams up to the general 1 GiB limit; they now
  stop at what 8,388,607 objects' rows can hold (~200 MB at most), and the
  section is reconstructed (logged) instead.

On the nightly's seed (41) the worst captured input fell from 6.2 GB to 6 MB,
and a 300 s fuzz run had no process above 300 MB (before: many at 2-5 GB).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-04 17:59:25 -04:00

46 lines
1.7 KiB
Rust

use lopdf::{Document, Stream, content::Content, dictionary};
#[test]
fn get_page_content_separates_streams_at_token_boundary() {
let mut doc = Document::with_version("1.5");
// Producer writes stream that ends without trailing whitespace
let s1 = doc.add_object(Stream::new(dictionary! {}, b"1 0 0 1 50".to_vec()));
let s2 = doc.add_object(Stream::new(dictionary! {}, b"100 cm".to_vec()));
let page_id = doc.add_object(dictionary! {
"Type" => "Page",
"Contents" => vec![s1.into(), s2.into()],
});
let bytes = doc.get_page_content(page_id);
let content = Content::decode(&bytes).unwrap();
assert_eq!(content.operations.len(), 1);
let op = &content.operations[0];
assert_eq!(op.operator, "cm");
let operands: Vec<f32> = op.operands.iter().map(|o| o.as_float().unwrap()).collect();
assert_eq!(operands, [1.0, 0.0, 0.0, 1.0, 50.0, 100.0]);
}
#[test]
fn get_page_content_terminates_trailing_comment() {
let mut doc = Document::with_version("1.5");
// Producer writes stream that ends with a comment without EOL
let s1 = doc.add_object(Stream::new(dictionary! {}, b"% a comment".to_vec()));
let s2 = doc.add_object(Stream::new(dictionary! {}, b"1 0 0 1 50 100 cm".to_vec()));
let page_id = doc.add_object(dictionary! {
"Type" => "Page",
"Contents" => vec![s1.into(), s2.into()],
});
let bytes = doc.get_page_content(page_id);
let content = Content::decode(&bytes).unwrap();
assert_eq!(content.operations.len(), 1);
let op = &content.operations[0];
assert_eq!(op.operator, "cm");
let operands: Vec<f32> = op.operands.iter().map(|o| o.as_float().unwrap()).collect();
assert_eq!(operands, [1.0, 0.0, 0.0, 1.0, 50.0, 100.0]);
}