mirror of
https://github.com/storytold/pdfcraft.git
synced 2026-10-25 10:39:01 -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>
103 lines
3.1 KiB
Rust
103 lines
3.1 KiB
Rust
use lopdf::{Document, Object};
|
|
use std::collections::HashMap;
|
|
use std::env;
|
|
|
|
#[cfg(feature = "async")]
|
|
use tokio::runtime::Builder;
|
|
|
|
#[cfg(not(feature = "async"))]
|
|
fn load_document(path: &str) -> Result<Document, Box<dyn std::error::Error>> {
|
|
Ok(Document::load(path)?)
|
|
}
|
|
|
|
#[cfg(feature = "async")]
|
|
fn load_document(path: &str) -> Result<Document, Box<dyn std::error::Error>> {
|
|
Ok(Builder::new_current_thread()
|
|
.enable_all()
|
|
.build()
|
|
.unwrap()
|
|
.block_on(async move { Document::load(path).await })?)
|
|
}
|
|
|
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
let args: Vec<String> = env::args().collect();
|
|
let input_file = if args.len() > 1 { &args[1] } else { "assets/example.pdf" };
|
|
|
|
println!("Analyzing PDF: {}", input_file);
|
|
let doc = load_document(input_file)?;
|
|
|
|
// Count object types
|
|
let mut type_counts: HashMap<String, usize> = HashMap::new();
|
|
let mut stream_count = 0;
|
|
let mut compressible_count = 0;
|
|
|
|
for ((_id, _gen), obj) in &doc.objects {
|
|
let type_name = match obj {
|
|
Object::Null => "Null",
|
|
Object::Boolean(_) => "Boolean",
|
|
Object::Integer(_) => "Integer",
|
|
Object::Real(_) => "Real",
|
|
Object::Name(_) => "Name",
|
|
Object::String(_, _) => "String",
|
|
Object::Array(_) => "Array",
|
|
Object::Dictionary(dict) => {
|
|
// Try to get the Type entry to be more specific
|
|
if let Ok(Object::Name(name)) = dict.get(b"Type") {
|
|
let type_str = String::from_utf8_lossy(name);
|
|
&format!("Dict/{}", type_str)
|
|
} else {
|
|
"Dictionary"
|
|
}
|
|
}
|
|
Object::Stream(_) => {
|
|
stream_count += 1;
|
|
"Stream"
|
|
}
|
|
Object::Reference(_) => "Reference",
|
|
};
|
|
|
|
// Check if object can be compressed
|
|
if !matches!(obj, Object::Stream(_)) {
|
|
compressible_count += 1;
|
|
}
|
|
|
|
*type_counts.entry(type_name.to_string()).or_insert(0) += 1;
|
|
}
|
|
|
|
println!("\nPDF Statistics:");
|
|
println!("Version: {}", doc.version);
|
|
println!("Total objects: {}", doc.objects.len());
|
|
println!("Stream objects: {} (cannot be compressed)", stream_count);
|
|
println!("Compressible objects: {}", compressible_count);
|
|
println!(
|
|
"Compression potential: {:.1}%",
|
|
(compressible_count as f64 / doc.objects.len() as f64) * 100.0
|
|
);
|
|
|
|
println!("\nObject type breakdown:");
|
|
let mut sorted_types: Vec<_> = type_counts.iter().collect();
|
|
sorted_types.sort_by_key(|(_, count)| *count);
|
|
sorted_types.reverse();
|
|
|
|
for (type_name, count) in sorted_types {
|
|
println!(" {}: {}", type_name, count);
|
|
}
|
|
|
|
// Check if already using object streams
|
|
let has_objstm = doc.objects.values().any(|obj| {
|
|
if let Object::Dictionary(dict) = obj
|
|
&& let Ok(Object::Name(name)) = dict.get(b"Type")
|
|
{
|
|
return name == b"ObjStm";
|
|
}
|
|
false
|
|
});
|
|
|
|
println!(
|
|
"\nAlready using object streams: {}",
|
|
if has_objstm { "Yes" } else { "No" }
|
|
);
|
|
|
|
Ok(())
|
|
}
|