Skip to content

PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust

fix(print): refuse huge posters at the sheet limit instead of overflowing (#1335) (#1376)

Marco Arnulfo2796f3de0d35
diff --git a/crates/print/src/lib.rs b/crates/print/src/lib.rs
index 1a2868d7a..3c8a2a7bd 100644
--- a/crates/print/src/lib.rs
+++ b/crates/print/src/lib.rs
@@ -467,7 +467,8 @@ pub fn layout(sizes: &[(f64, f64)], settings: &Settings) -> Result<Vec<Sheet>, P
let (stepx, stepy) = (tw - overlap, th - overlap);
let nx = (((sw - overlap) / stepx).ceil() as usize).max(1);
let ny = (((sh - overlap) / stepy).ceil() as usize).max(1);
- if nx * ny > 1024 {
+ // The counts saturate at usize::MAX for a huge scale, so their product can overflow.
+ if nx.checked_mul(ny).is_none_or(|n| n > 1024) {
return Err(PrintError::Invalid("the poster would need more than 1024 sheets".into()));
}
// Tiles from the top-left, row by row.
diff --git a/crates/print/src/tests.rs b/crates/print/src/tests.rs
index 3ae29f524..22ae44424 100644
--- a/crates/print/src/tests.rs
+++ b/crates/print/src/tests.rs
@@ -192,6 +192,17 @@ fn posters_tile_with_overlap() {
assert!(layout(&[(200.0, 300.0)], &settings(vec![0], Layout::Poster { scale: 400.0, overlap: 400.0, cut_marks: false })).is_err());
}
+/// Issue #1335: a scale so large that the tile counts saturate is refused with the sheet-limit
+/// error, not an overflow in counting the sheets.
+#[test]
+fn huge_posters_are_refused_at_the_sheet_limit() {
+ let refused = Err(PrintError::Invalid("the poster would need more than 1024 sheets".into()));
+ for scale in [100_000.0, 1e22, f64::MAX] {
+ let poster = settings(vec![0], Layout::Poster { scale, overlap: 18.0, cut_marks: true });
+ assert_eq!(layout(&[(200.0, 300.0)], &poster), refused, "scale {scale}");
+ }
+}
+
#[test]
fn imposed_pdf_has_the_sheets_and_honours_comments_and_forms() {
let doc = fixture(3);