PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust
fix(organize): reject rotation angles that are not multiples of 90 (#801) (#833)
muraa-p527128d0075c
diff --git a/crates/organize/src/lib.rs b/crates/organize/src/lib.rsindex 5419eb773..1459d67c6 100644--- a/crates/organize/src/lib.rs+++ b/crates/organize/src/lib.rs@@ -149,6 +149,9 @@ fn check(indices: &[usize], n: usize) -> Result<(), OrganizeError> {/// Rotate pages by a multiple of 90° (positive = clockwise), adjusting `/Rotate` (§7.7.3.3).pub fn rotate_pages(doc: &mut Document, indices: &[usize], degrees: i64) -> Result<(), OrganizeError> {+ if degrees % 90 != 0 {+ return Err(OrganizeError::Invalid("rotation must be a multiple of 90 degrees".into()));+ }let all = walk(doc)?;check(indices, all.len())?;let delta = (degrees / 90) * 90;diff --git a/crates/organize/src/tests.rs b/crates/organize/src/tests.rsindex 152b961ac..5dddf0296 100644--- a/crates/organize/src/tests.rs+++ b/crates/organize/src/tests.rs@@ -16,6 +16,18 @@ fn page_rotation_resolves_inheritance_overrides_and_missing_pages() {assert_eq!(page_rotation(&doc, 3), Err(OrganizeError::NoSuchPage(3)));}+#[test]+fn rotate_rejects_angles_that_are_not_multiples_of_90() {+ // The page tree only turns in quarter steps, so anything else used to be silently truncated+ // by the integer division. It has to be an error, and the document must stay as it was.+ let mut doc = Document::open(Arc::new(fixture())).unwrap();+ assert_eq!(rotate_pages(&mut doc, &[0], 45), Err(OrganizeError::Invalid("rotation must be a multiple of 90 degrees".into())));+ assert_eq!(rotate_pages(&mut doc, &[0], 91), Err(OrganizeError::Invalid("rotation must be a multiple of 90 degrees".into())));+ assert_eq!(page_rotation(&doc, 0).unwrap(), 90);+ rotate_pages(&mut doc, &[0], -90).unwrap();+ assert_eq!(page_rotation(&doc, 0).unwrap(), 0);+}+/// A 3-page document with a nested page tree. MediaBox and Rotate are inherited from the root,/// Resources from an intermediate node; each page's content says which page it is.fn fixture() -> Vec<u8> {