Files
pdfcraft/vendor/lopdf/examples/debug_compression_detailed.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

112 lines
3.6 KiB
Rust

use lopdf::{Document, SaveOptions};
#[cfg(feature = "async")]
use tokio::runtime::Builder;
#[cfg(not(feature = "async"))]
fn load_document(path: &str) -> Result<Document, lopdf::Error> {
Document::load(path)
}
#[cfg(feature = "async")]
fn load_document(path: &str) -> Result<Document, lopdf::Error> {
Builder::new_current_thread()
.enable_all()
.build()
.unwrap()
.block_on(async move { Document::load(path).await })
}
fn main() {
println!("Debugging object stream compression in detail...\n");
// Load the user's PDF
let pdf_path = "/Users/nicolasdao/Downloads/pdfs/RFQ - SDS WebApp.docx.pdf";
println!("Loading PDF: {}", pdf_path);
let mut doc = match load_document(pdf_path) {
Ok(d) => d,
Err(e) => {
eprintln!("Failed to load PDF: {}", e);
return;
}
};
println!("Loaded {} objects", doc.objects.len());
// Count how many objects can be compressed
let mut compressible_count = 0;
for (id, obj) in &doc.objects {
if id.1 == 0 && lopdf::ObjectStream::can_be_compressed(*id, obj, &doc) {
compressible_count += 1;
}
}
println!("Compressible objects: {}", compressible_count);
// Save with object streams
println!("\nSaving with object streams (compression level 6)...");
let options = SaveOptions {
use_object_streams: true,
use_xref_streams: true, // MUST use xref streams with object streams!
object_stream_config: lopdf::ObjectStreamConfig {
max_objects_per_stream: 100,
compression_level: 6,
},
..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
std::fs::write("debug_compression.pdf", &buffer).unwrap();
// Load back and check
println!("\nLoading back and checking object streams...");
let loaded = Document::load_mem(&buffer).unwrap();
let mut obj_stream_count = 0;
let mut obj_stream_with_filter = 0;
for (id, obj) in &loaded.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;
let has_filter = stream.dict.get(b"Filter").is_ok();
if has_filter {
obj_stream_with_filter += 1;
}
println!("\nObject Stream {} 0 R:", id.0);
println!(" Has Filter: {}", has_filter);
if let Ok(n) = stream.dict.get(b"N").and_then(|o| o.as_i64()) {
println!(" Contains {} objects", n);
}
if let Ok(first) = stream.dict.get(b"First").and_then(|o| o.as_i64()) {
println!(" First offset: {}", first);
}
println!(" Content size: {} bytes", stream.content.len());
// Check if content looks compressed
let looks_compressed = stream.content.iter().take(20).any(|&b| b > 127);
println!(" Content looks compressed: {}", looks_compressed);
}
}
println!("\nSummary:");
println!(" Total object streams: {}", obj_stream_count);
println!(" Object streams with Filter: {}", obj_stream_with_filter);
if obj_stream_with_filter < obj_stream_count {
println!(
"\nWARNING: {} object streams are missing Filter!",
obj_stream_count - obj_stream_with_filter
);
}
}