Modern Apps git

← Modern-Apps.git

pdf: a wrong-page annotation delete no longer destroys the annotation

Commit
72d426dfd49ded852f974cdd16f9df6eb9d49ea1
Author
Vayun Mathur <[email protected]>
When
2026-12-01 07:21 UTC (5 days ago)
Parents
2d26678
delete_annotation removed the reference from the given page
and then unconditionally deleted the object — even when the
reference was not on that page and the call returned false
for 'not deleted'. A highlight on page 0 deleted 'via'
page 1 reported failure yet destroyed the object anyway,
leaving page 0's /Annots holding a dangling reference: the
annotation lost and the saved PDF corrupt. Any caller with
a stale page index (a text-edit session whose page no longer
matches, a race with a page change) turned a no-op into
silent data loss.

The object removal is now gated on the reference actually
having been removed, so the JNI contract (false = not
deleted) is honoured. New regression test builds a 2-page
document, deletes via the wrong page, and asserts the
annotation survives on its own page (failed pre-fix); the
existing same-page round-trip still passes. Verified: cargo
test 675 pass; :pdf:testDevUnitTest BUILD SUCCESSFUL; lint
errors are pre-existing on untouched files.

Diff

pdf/src/main/rust/src/annotations_part5.rs
diff --git a/pdf/src/main/rust/src/annotations_part5.rs b/pdf/src/main/rust/src/annotations_part5.rsindex 36ee9a61af..f73e7938ea 100644--- a/pdf/src/main/rust/src/annotations_part5.rs+++ b/pdf/src/main/rust/src/annotations_part5.rs@@ -261,7 +261,13 @@ pub(crate) fn delete_annotation(handle: i64, page_index: i32, annot_id: i64) ->         None => return false,     };     let removed = remove_annot_ref(doc, page_id, id);-    doc.objects.remove(&id);+    // Only destroy the object once its reference on THIS page is gone. Removing+    // it unconditionally turned a wrong-page (or stale-page) delete into silent+    // data loss: the call reported failure, yet the annotation object was gone+    // and the page that actually lists it kept a dangling reference.+    if removed {+        doc.objects.remove(&id);+    }     removed } 
pdf/src/main/rust/src/cov_annotations_tests.rs
diff --git a/pdf/src/main/rust/src/cov_annotations_tests.rs b/pdf/src/main/rust/src/cov_annotations_tests.rsindex 90241395a0..328e8be3c2 100644--- a/pdf/src/main/rust/src/cov_annotations_tests.rs+++ b/pdf/src/main/rust/src/cov_annotations_tests.rs@@ -157,3 +157,26 @@ fn annotation_add_and_delete_roundtrip() {     assert_eq!(duplicate_annotation(h, 0, id, 0.0, 0.0), 0, "deleted annot cannot duplicate");     close_document(h); }++#[test]+fn delete_with_wrong_page_preserves_annotation() {+    let h = create_empty_document();+    assert_eq!(append_image_page(h, &[0xff, 0xd8], 100, 100), 1);+    assert_eq!(append_image_page(h, &[0xff, 0xd8], 100, 100), 1);+    let id = add_highlight(h, 0, [10.0, 10.0, 100.0, 50.0], 0xFFFF0000).expect("highlight on page 0");+    // Deleting via the wrong page must be a no-op: it reports failure AND+    // leaves the annotation object and its page reference intact.+    assert!(!delete_annotation(h, 1, id), "annot is not on page 1");+    let listed = list_annotations(h, 0).expect("page 0 still lists annotations");+    assert!(listed.len() > 4, "annotation must survive a wrong-page delete");+    {+        let reg = registry().lock().unwrap();+        let doc = reg.get(&h).expect("doc");+        assert!(+            doc.get_dictionary(decode_id(id)).is_ok(),+            "annotation object must not be destroyed by a wrong-page delete"+        );+    }+    assert!(delete_annotation(h, 0, id), "correct-page delete still works");+    close_document(h);+}