Files
pdfcraft/vendor/lopdf/examples/debug_save_with_objstreams.rs
T
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

87 lines
2.7 KiB
Rust

use lopdf::{Document, Object, SaveOptions, dictionary};
use std::io::Write;
fn main() {
println!("Debugging save_with_object_streams...\n");
// Create a simple document
let mut doc = Document::with_version("1.5");
// Add some simple objects that can be compressed
let obj1_id = doc.add_object(Object::Integer(42));
let obj2_id = doc.add_object(Object::String(b"Hello".to_vec(), lopdf::StringFormat::Literal));
let obj3_id = doc.add_object(dictionary! {
"Type" => "TestObject",
"Value" => 123
});
println!("Created objects:");
println!(" {} 0 R: Integer", obj1_id.0);
println!(" {} 0 R: String", obj2_id.0);
println!(" {} 0 R: Dictionary", obj3_id.0);
// Add required catalog structure
let pages_id = doc.add_object(dictionary! {
"Type" => "Pages",
"Kids" => vec![],
"Count" => 0
});
let catalog_id = doc.add_object(dictionary! {
"Type" => "Catalog",
"Pages" => pages_id
});
doc.trailer.set("Root", catalog_id);
println!("\nSaving with object streams...");
// Save with object streams
let options = SaveOptions {
use_object_streams: true,
..Default::default()
};
let mut buffer = Vec::new();
doc.save_with_options(&mut buffer, options).unwrap();
println!("Saved {} bytes", buffer.len());
// Write to file for inspection
let mut file = std::fs::File::create("debug_objstream_save.pdf").unwrap();
file.write_all(&buffer).unwrap();
// Load it back and check
println!("\nLoading back and analyzing...");
let loaded = Document::load_mem(&buffer).unwrap();
println!("Loaded {} objects", loaded.objects.len());
// Find object streams
for (id, obj) in &loaded.objects {
if let Object::Stream(stream) = obj
&& let Ok(type_obj) = stream.dict.get(b"Type")
&& let Ok(type_name) = type_obj.as_name()
&& type_name == b"ObjStm"
{
println!("\nFound object stream {} 0 R:", id.0);
println!(" Dictionary: {:?}", stream.dict);
println!(" Has Filter: {}", stream.dict.get(b"Filter").is_ok());
println!(" Content size: {} bytes", stream.content.len());
// Check if it looks compressed
let looks_compressed = stream.content.iter().take(10).any(|&b| !(32..=127).contains(&b));
println!(" Content looks compressed: {}", looks_compressed);
if !looks_compressed {
println!(
" First 50 bytes: {:?}",
&stream.content[..stream.content.len().min(50)]
);
}
}
}
println!("\nDone! Check debug_objstream_save.pdf");
}