mirror of
https://github.com/storytold/pdfcraft.git
synced 2026-10-28 12:08:46 -06:00
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>
125 lines
3.6 KiB
Rust
125 lines
3.6 KiB
Rust
use lopdf::{Document, SaveOptions};
|
|
use std::fs::File;
|
|
use std::io::Write;
|
|
|
|
fn main() {
|
|
println!("Testing object stream round-trip...\n");
|
|
|
|
// First, create a simple PDF with object streams
|
|
let mut doc = Document::with_version("1.5");
|
|
|
|
// Add a simple page
|
|
let pages_id = doc.new_object_id();
|
|
let page_id = doc.new_object_id();
|
|
let content_id = doc.new_object_id();
|
|
let font_id = doc.new_object_id();
|
|
|
|
// Page content
|
|
doc.objects.insert(
|
|
content_id,
|
|
lopdf::Stream::new(
|
|
lopdf::dictionary! {},
|
|
b"BT /F1 12 Tf 100 700 Td (Hello World) Tj ET".to_vec(),
|
|
)
|
|
.into(),
|
|
);
|
|
|
|
// Font
|
|
doc.objects.insert(
|
|
font_id,
|
|
lopdf::dictionary! {
|
|
"Type" => "Font",
|
|
"Subtype" => "Type1",
|
|
"BaseFont" => "Helvetica"
|
|
}
|
|
.into(),
|
|
);
|
|
|
|
// Page
|
|
doc.objects.insert(
|
|
page_id,
|
|
lopdf::dictionary! {
|
|
"Type" => "Page",
|
|
"Parent" => pages_id,
|
|
"MediaBox" => vec![0.into(), 0.into(), 612.into(), 792.into()],
|
|
"Contents" => content_id,
|
|
"Resources" => lopdf::dictionary! {
|
|
"Font" => lopdf::dictionary! {
|
|
"F1" => font_id
|
|
}
|
|
}
|
|
}
|
|
.into(),
|
|
);
|
|
|
|
// Pages
|
|
doc.objects.insert(
|
|
pages_id,
|
|
lopdf::dictionary! {
|
|
"Type" => "Pages",
|
|
"Kids" => vec![page_id.into()],
|
|
"Count" => 1
|
|
}
|
|
.into(),
|
|
);
|
|
|
|
// Catalog
|
|
let catalog_id = doc.add_object(lopdf::dictionary! {
|
|
"Type" => "Catalog",
|
|
"Pages" => pages_id
|
|
});
|
|
|
|
doc.trailer.set("Root", catalog_id);
|
|
|
|
// Save with object streams
|
|
println!("Saving original PDF with object streams...");
|
|
let options = SaveOptions {
|
|
use_object_streams: true,
|
|
..Default::default()
|
|
};
|
|
|
|
let mut original_bytes = Vec::new();
|
|
doc.save_with_options(&mut original_bytes, options.clone()).unwrap();
|
|
println!("Original size: {} bytes", original_bytes.len());
|
|
|
|
// Write to file for inspection
|
|
let mut file = File::create("test_roundtrip_original.pdf").unwrap();
|
|
file.write_all(&original_bytes).unwrap();
|
|
|
|
// Now load it back
|
|
println!("\nLoading PDF back...");
|
|
let mut loaded_doc = Document::load_mem(&original_bytes).unwrap();
|
|
println!("Loaded {} objects", loaded_doc.objects.len());
|
|
|
|
// Check for object streams
|
|
let mut obj_stream_count = 0;
|
|
for (id, obj) in &loaded_doc.objects {
|
|
if let lopdf::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"
|
|
{
|
|
obj_stream_count += 1;
|
|
println!("Found object stream {} 0 R", id.0);
|
|
if let Ok(filter) = stream.dict.get(b"Filter") {
|
|
println!(" Has Filter: {:?}", filter);
|
|
} else {
|
|
println!(" WARNING: No Filter!");
|
|
}
|
|
}
|
|
}
|
|
println!("Total object streams found: {}", obj_stream_count);
|
|
|
|
// Save again with object streams
|
|
println!("\nSaving loaded PDF with object streams again...");
|
|
let mut resaved_bytes = Vec::new();
|
|
loaded_doc.save_with_options(&mut resaved_bytes, options).unwrap();
|
|
println!("Resaved size: {} bytes", resaved_bytes.len());
|
|
|
|
// Write to file for inspection
|
|
let mut file = File::create("test_roundtrip_resaved.pdf").unwrap();
|
|
file.write_all(&resaved_bytes).unwrap();
|
|
|
|
println!("\nDone! Check test_roundtrip_original.pdf and test_roundtrip_resaved.pdf");
|
|
}
|