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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AmsdeUCdTaD72jkDw2pJKY
This commit is contained in:
Grzegorz MartensandClaude Opus 5.5 committed 2026-11-09 13:00:28 +01:00
1 parent 0a00862e91
commit 1184738b54
2 files changed
+27 -1

No files matched your search

+3 -1
View File
@@ -617,7 +617,9 @@ pub(crate) fn unshare(doc: &mut Document, before: &HashSet<u32>, 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.
+24
View File
@@ -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<u8>, 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