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

64 lines
2.5 KiB
Rust

use lopdf::Document;
use std::str::FromStr;
#[cfg(not(feature = "async"))]
fn main() {
// Collect command line arguments: input_file angle output_file
let args: Vec<String> = std::env::args().collect();
assert!(args.len() == 4, "Not enough arguments: input_file angle output_file");
let input_file = &args[1];
let angle = i64::from_str(&args[2]).expect("error in parsing angle argument");
assert!(angle % 90 == 0, "angle must be a multiple of 90");
let output_file = &args[3];
let mut doc = Document::load(input_file).unwrap();
// Note: this example sets Rotate on each page individually for flexibility,
// but you can also set it on any node in the page tree and child pages will
// inherit the value.
for (_, page_id) in doc.get_pages() {
let page_dict = doc
.get_object_mut(page_id)
.and_then(|obj| obj.as_dict_mut())
.expect("Missing page!");
// Get the current rotation if any; the default is 0
let current_rotation = page_dict.get(b"Rotate").and_then(|obj| obj.as_i64()).unwrap_or(0);
// Add the angle and update
page_dict.set("Rotate", (current_rotation + angle) % 360);
}
// Store file in current working directory.
doc.save(output_file).unwrap();
}
#[cfg(feature = "async")]
#[tokio::main]
async fn main() {
// Collect command line arguments: input_file angle output_file
let args: Vec<String> = std::env::args().collect();
assert!(args.len() == 4, "Not enough arguments: input_file angle output_file");
let input_file = &args[1];
let angle = i64::from_str(&args[2]).expect("error in parsing angle argument");
assert!(angle % 90 == 0, "angle must be a multiple of 90");
let output_file = &args[3];
let mut doc = Document::load(input_file).await.unwrap();
// Note: this example sets Rotate on each page individually for flexibility,
// but you can also set it on any node in the page tree and child pages will
// inherit the value.
for (_, page_id) in doc.get_pages() {
let page_dict = doc
.get_object_mut(page_id)
.and_then(|obj| obj.as_dict_mut())
.expect("Missing page!");
// Get the current rotation if any; the default is 0
let current_rotation = page_dict.get(b"Rotate").and_then(|obj| obj.as_i64()).unwrap_or(0);
// Add the angle and update
page_dict.set("Rotate", (current_rotation + angle) % 360);
}
// Store file in current working directory.
doc.save(output_file).unwrap();
}