Files
pdfcraft/vendor/lopdf/src/save_options.rs
T
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

84 lines
2.2 KiB
Rust

use crate::ObjectStreamConfig;
/// Options for saving PDF documents
#[derive(Debug, Clone, Default)]
pub struct SaveOptions {
/// Enable object streams for compressing non-stream objects
pub use_object_streams: bool,
/// Enable cross-reference streams instead of traditional xref tables
pub use_xref_streams: bool,
/// Enable linearization (fast web view)
pub linearize: bool,
/// Configuration for object streams
pub object_stream_config: ObjectStreamConfig,
}
impl SaveOptions {
/// Create a builder for SaveOptions
pub fn builder() -> SaveOptionsBuilder {
SaveOptionsBuilder::default()
}
}
/// Builder for SaveOptions
#[derive(Default)]
pub struct SaveOptionsBuilder {
use_object_streams: bool,
use_xref_streams: bool,
linearize: bool,
max_objects_per_stream: usize,
compression_level: u32,
}
impl SaveOptionsBuilder {
/// Enable or disable object streams
pub fn use_object_streams(mut self, value: bool) -> Self {
self.use_object_streams = value;
self
}
/// Enable or disable cross-reference streams
pub fn use_xref_streams(mut self, value: bool) -> Self {
self.use_xref_streams = value;
self
}
/// Enable or disable linearization
pub fn linearize(mut self, value: bool) -> Self {
self.linearize = value;
self
}
/// Set maximum objects per stream
pub fn max_objects_per_stream(mut self, value: usize) -> Self {
self.max_objects_per_stream = value;
self
}
/// Set compression level (0-9)
pub fn compression_level(mut self, value: u32) -> Self {
self.compression_level = value;
self
}
/// Build the SaveOptions
pub fn build(self) -> SaveOptions {
SaveOptions {
use_object_streams: self.use_object_streams,
use_xref_streams: self.use_xref_streams,
linearize: self.linearize,
object_stream_config: ObjectStreamConfig {
max_objects_per_stream: if self.max_objects_per_stream == 0 {
100
} else {
self.max_objects_per_stream
},
compression_level: self.compression_level,
},
}
}
}