Files
Brandon Thomas 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

119 lines
3.4 KiB
Rust

use lopdf::Document;
#[cfg(not(feature = "async"))]
fn main() {
// Test loading the encrypted.pdf file
println!("Loading encrypted.pdf...");
let doc = match Document::load("assets/encrypted.pdf") {
Ok(d) => d,
Err(e) => {
eprintln!("Failed to load encrypted.pdf: {:?}", e);
return;
}
};
println!("Document loaded successfully!");
println!("Is encrypted: {}", doc.is_encrypted());
// Try to get pages
let pages = doc.get_pages();
println!("Number of pages: {}", pages.len());
// Try to extract text
if !pages.is_empty() {
let page_numbers: Vec<u32> = pages.keys().cloned().collect();
match doc.extract_text(&page_numbers) {
Ok(text) => {
println!("Text extraction successful!");
println!(
"Text preview (first 200 chars): {}",
text.chars().take(200).collect::<String>()
);
}
Err(e) => {
println!("Text extraction failed: {:?}", e);
}
}
}
// Check if we can access objects
println!("\nChecking object access:");
let max_check = 10;
for i in 1..=max_check {
if doc.get_object((i, 0)).is_ok() {
println!(" Object ({}, 0) found", i);
}
}
// Check trailer
println!("\nTrailer entries:");
if let Ok(root) = doc.trailer.get(b"Root") {
println!(" Root: {:?}", root);
}
if let Ok(encrypt) = doc.trailer.get(b"Encrypt") {
println!(" Encrypt: {:?}", encrypt);
}
if let Ok(info) = doc.trailer.get(b"Info") {
println!(" Info: {:?}", info);
}
}
#[cfg(feature = "async")]
#[tokio::main]
async fn main() {
// Test loading the encrypted.pdf file
println!("Loading encrypted.pdf...");
let doc = match Document::load("assets/encrypted.pdf").await {
Ok(d) => d,
Err(e) => {
eprintln!("Failed to load encrypted.pdf: {:?}", e);
return;
}
};
println!("Document loaded successfully!");
println!("Is encrypted: {}", doc.is_encrypted());
// Try to get pages
let pages = doc.get_pages();
println!("Number of pages: {}", pages.len());
// Try to extract text
if !pages.is_empty() {
let page_numbers: Vec<u32> = pages.keys().cloned().collect();
match doc.extract_text(&page_numbers) {
Ok(text) => {
println!("Text extraction successful!");
println!(
"Text preview (first 200 chars): {}",
text.chars().take(200).collect::<String>()
);
}
Err(e) => {
println!("Text extraction failed: {:?}", e);
}
}
}
// Check if we can access objects
println!("\nChecking object access:");
let max_check = 10;
for i in 1..=max_check {
if doc.get_object((i, 0)).is_ok() {
println!(" Object ({}, 0) found", i);
}
}
// Check trailer
println!("\nTrailer entries:");
if let Ok(root) = doc.trailer.get(b"Root") {
println!(" Root: {:?}", root);
}
if let Ok(encrypt) = doc.trailer.get(b"Encrypt") {
println!(" Encrypt: {:?}", encrypt);
}
if let Ok(info) = doc.trailer.get(b"Info") {
println!(" Info: {:?}", info);
}
}