From 1184738b54878b6d55731cd4a5f73e5c98883fde Mon Sep 17 00:00:00 2001 From: Grzegorz Martens Date: Mon, 9 Nov 2026 13:00:28 +0100 Subject: [PATCH] redact: an image stored again keeps the generation its header gives Main's #1143 (863af864a), merged in 77b9cfa31, has cos accept both generations of a slot whose cross-reference row and object header disagree: `8 0` in the row and `8 1 obj` in the file, a reference of either reads the object. `unshare` stored an image again with `Document::set` under the row's generation, which keeps that one alone. A page that named the image by its header's, `/Im 8 1 R`, then read null, in the document and after a full save: a redaction on another page took the image off a page no mark reached. It now stores it with `Document::set_existing`, which keeps both. It was the one object this PR's code writes back after reading. Test: `an_image_stored_again_keeps_the_generation_its_header_gives` patches the second page's image to `8 1 obj` behind a row of generation 0, and its page to `/Im 8 1 R`. After a redaction on the first page, that page still draws the scan, in the document and after `write_full` and reopening, and the dictionary is gone. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AmsdeUCdTaD72jkDw2pJKY --- crates/redact/src/globals.rs | 4 +++- crates/redact/src/tests.rs | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/crates/redact/src/globals.rs b/crates/redact/src/globals.rs index aed33c501c..f12378a2fa 100644 --- a/crates/redact/src/globals.rs +++ b/crates/redact/src/globals.rs @@ -617,7 +617,9 @@ pub(crate) fn unshare(doc: &mut Document, before: &HashSet, images: &mut Im let Object::Stream(s) = &*object else { continue }; match without_dictionary(&mut run, doc, s, images)? { Some(stored) => { - doc.set(r, Object::Stream(stored)); + // In place, with both generations of a slot whose cross-reference row and object + // header disagree, which cos reads either way (#1143): `set` would keep only `r`'s. + doc.set_existing(r, Object::Stream(stored))?; stored_again += 1; } // A dictionary that names itself isn't something that uses one. diff --git a/crates/redact/src/tests.rs b/crates/redact/src/tests.rs index 3ae8fd5d81..4453d87aae 100644 --- a/crates/redact/src/tests.rs +++ b/crates/redact/src/tests.rs @@ -1995,6 +1995,30 @@ fn a_size_through_another_generation_isnt_the_one_its_own_reference_reads() { assert!(matches!(result, Err(RedactError::SharedJbig2Globals(1))), "{result:?}"); } +/// An image whose cross-reference row and object header give different generations is read through +/// either (#1143), and page 2 names it by the header's. Stored again without the dictionary, it keeps +/// both, so that page still draws it, in the document and in a full save. +#[test] +fn an_image_stored_again_keeps_the_generation_its_header_gives() { + let scan = shared_scan(); + let bytes = SharedPages::new(&scan).build(&scan).bytes().to_vec(); + let patch = |bytes: Vec, from: &[u8], to: &[u8]| { + let at = bytes.windows(from.len()).position(|w| w == from).unwrap(); + assert_eq!(bytes.windows(from.len()).filter(|w| *w == from).count(), 1); + [&bytes[..at], to, &bytes[at + from.len()..]].concat() + }; + let bytes = patch(patch(bytes, b"\n8 0 obj", b"\n8 1 obj"), b"/Im 8 0 R", b"/Im 8 1 R"); + let mut doc = Document::open(Arc::new(bytes)).unwrap(); + assert_eq!(drawn_samples(&doc, 1), scan.bits, "the second page reads its image through the header's generation"); + mark(&mut doc, 0, &[COVERS], ""); + assert_eq!(apply(&mut doc, None).unwrap().images_restored, 1); + assert_eq!(drawn_samples(&doc, 1), scan.bits, "the image stored again is still drawn"); + let full = write_full(&doc, &SaveOptions::default()).unwrap(); + assert!(!full.windows(12).any(|w| w == b"LEAK_GLOBALS"), "the dictionary left the full save"); + assert_eq!(drawn_samples(&Document::open(Arc::new(full)).unwrap(), 1), scan.bits, "and is drawn after it"); + assert_gone(&doc, b"LEAK_GLOBALS"); +} + /// However the image that stays names the dictionary, it comes out without it: its parameters by /// reference, behind another filter, or a copy of the dictionary, as a file put back together from /// pages split apart has, each page with its own. It is also read when its size or filter is given