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

67 lines
2.6 KiB
Rust

use lopdf::{Object, ObjectStream};
fn main() {
println!("Testing Object Stream creation...\n");
// Create a simple object stream with a few objects
let mut obj_stream = ObjectStream::builder().max_objects(10).compression_level(6).build();
// Add some simple objects
obj_stream.add_object((1, 0), Object::Integer(42)).unwrap();
obj_stream
.add_object((2, 0), Object::String(b"Hello".to_vec(), lopdf::StringFormat::Literal))
.unwrap();
obj_stream.add_object((3, 0), Object::Boolean(true)).unwrap();
println!("Added {} objects to stream", obj_stream.object_count());
// Build the stream content
match obj_stream.build_stream_content() {
Ok(content) => {
println!("\nStream content built successfully:");
println!("Content size: {} bytes", content.len());
// Show the content
if let Ok(content_str) = std::str::from_utf8(&content) {
println!("\nContent (first 200 chars):");
println!("{}", &content_str[..content_str.len().min(200)]);
} else {
println!(
"\nRaw content (first 100 bytes): {:?}",
&content[..content.len().min(100)]
);
}
// Try to create the stream object
match obj_stream.to_stream_object() {
Ok(stream) => {
println!("\n\nStream object created successfully!");
println!("Dictionary: {:?}", stream.dict);
println!("Compressed: {}", stream.dict.get(b"Filter").is_ok());
println!("Content length: {}", stream.content.len());
// Try to decompress and verify
match stream.decompressed_content() {
Ok(decompressed) => {
println!("\nDecompressed successfully!");
println!("Decompressed size: {} bytes", decompressed.len());
if let Ok(decomp_str) = std::str::from_utf8(&decompressed) {
println!("Decompressed content:\n{}", decomp_str);
}
}
Err(e) => {
println!("\nERROR: Failed to decompress: {}", e);
}
}
}
Err(e) => {
println!("\nERROR: Failed to create stream object: {}", e);
}
}
}
Err(e) => {
println!("ERROR: Failed to build stream content: {}", e);
}
}
}