Merge pull request #10420 from dotCooCoo/fix/pdf-read-counter-address-reuse

PDF: count test reads of a buffer from a mark, not over its address's whole history
This commit is contained in:
.CooCoo authored and GitHub committed 2026-12-01 02:32:29 -08:00
commit 295da28ec5
3 files changed
+44 -8

No files matched your search

+3 -1
View File
@@ -739,6 +739,8 @@ fn linked_pages_read_the_file_once_and_a_blank_page_keeps_its_crop_box() {
let art = || PdfPage::new(100.0, 50.0, "1 0 0 rg 10 10 20 20 re f");
write(&src, &pdf(&[art(), PdfPage::new(80.0, 40.0, ""), art()], None));
let mut s = Session::new();
// A freed buffer that had the blob's address may have been read before, in another test.
let mark = vectorcraft_pdf::read_mark();
s.execute("document.open", &json!({"path": src, "linkPages": true, "cropTo": "bounding"})).unwrap();
let d = &s.doc().unwrap().doc;
let boards: Vec<Rect> = d.artboards.iter().map(|a| a.rect).collect();
@@ -748,7 +750,7 @@ fn linked_pages_read_the_file_once_and_a_blank_page_keeps_its_crop_box() {
// The pages are read from the one copy every page's blob keeps: a page that read the file
// again would count here.
let blob = d.images.values().next().map(|b| b.bytes.clone()).unwrap();
assert_eq!(vectorcraft_pdf::reads_of(&blob), 0, "the pages are read from the file read once");
assert_eq!(vectorcraft_pdf::reads_of_since(&blob, mark), 0, "the pages are read from the file read once");
}
#[test]
+2 -2
View File
@@ -90,12 +90,12 @@ pub use import_shading::function_evaluation_limit;
pub use marks::{leaves_out_bleed, page_bleed, printer_marks};
pub use output::ActiveProfiles;
pub use page_widths::{FontWidths, PageWidths};
#[cfg(any(test, feature = "read-count"))]
pub use pages::reads_of;
pub use pages::{
IllustratorData, Opened, PageInfo, PdfInfo, TrimPages, has_illustrator_data, illustrator_data, illustrator_data_without_pdf_part, info,
is_postscript, open_pdf, read_illustrator_data_without_pdf_part, read_illustrator_data_without_pdf_part_with,
};
#[cfg(any(test, feature = "read-count"))]
pub use pages::{read_mark, reads_of, reads_of_since};
pub use piece_info::{PRIVATE_CHUNK, PrivateData};
pub use post::{THUMBNAIL_SIZE, Thumbnail};
pub use print::*;
+39 -5
View File
@@ -368,14 +368,33 @@ pub(crate) fn open_with_options(bytes: &[u8], password: Option<&str>, options: &
})
}
/// How many times each buffer has been read as a PDF, by its address (tests).
/// The number the next read as a PDF gets, and every read of a buffer by the buffer's address with
/// its number (tests). A freed buffer's reads stay under its address, so a later buffer at the same
/// address starts with them: count from a [`read_mark`] taken before that buffer exists.
#[cfg(any(test, feature = "read-count"))]
static READS: std::sync::Mutex<std::collections::BTreeMap<usize, usize>> = std::sync::Mutex::new(std::collections::BTreeMap::new());
type Reads = (u64, std::collections::BTreeMap<usize, Vec<u64>>);
#[cfg(any(test, feature = "read-count"))]
static READS: std::sync::Mutex<Reads> = std::sync::Mutex::new((0, std::collections::BTreeMap::new()));
/// How many times `bytes`, at its address, has been read as a PDF on any thread (tests).
#[cfg(any(test, feature = "read-count"))]
pub fn reads_of(bytes: &[u8]) -> usize {
READS.lock().ok().and_then(|r| r.get(&(bytes.as_ptr() as usize)).copied()).unwrap_or(0)
READS.lock().ok().and_then(|r| r.1.get(&(bytes.as_ptr() as usize)).map(Vec::len)).unwrap_or(0)
}
/// The number the next read as a PDF gets, for [`reads_of_since`] (tests).
#[cfg(any(test, feature = "read-count"))]
pub fn read_mark() -> u64 {
READS.lock().map(|r| r.0).unwrap_or(0)
}
/// How many times `bytes`, at its address, has been read as a PDF on any thread since `mark` was
/// taken with [`read_mark`] (tests). Reads made before `mark`, such as reads of a freed buffer
/// that had the same address, are not counted.
#[cfg(any(test, feature = "read-count"))]
pub fn reads_of_since(bytes: &[u8], mark: u64) -> usize {
READS.lock().ok().and_then(|r| r.1.get(&(bytes.as_ptr() as usize)).map(|reads| reads.iter().filter(|&&n| n >= mark).count())).unwrap_or(0)
}
/// [`open_with_options`] without looking at the file's objects ([`hostile`]): the reader builds its
@@ -384,8 +403,9 @@ pub fn reads_of(bytes: &[u8]) -> usize {
fn open_structural_with_options(bytes: &[u8], password: Option<&str>, options: &craft::pdf::OpenOptions) -> Result<Pdf, PdfError> {
#[cfg(any(test, feature = "read-count"))]
if let Ok(mut reads) = READS.lock() {
let n = reads.entry(bytes.as_ptr() as usize).or_default();
*n = n.saturating_add(1);
let n = reads.0;
reads.0 = n.saturating_add(1);
reads.1.entry(bytes.as_ptr() as usize).or_default().push(n);
}
if is_postscript(bytes) {
return Err(PdfError::PostScript);
@@ -763,4 +783,18 @@ mod tests {
assert_eq!(super::decoded_size(1 << 30), "1 GB");
assert_eq!(super::decoded_size((1 << 30) + (1 << 20)), "1025 MB");
}
/// Reads made before a mark, as of a freed buffer whose address a new one takes, are not
/// counted from that mark; reads after it are.
#[test]
fn reads_since_a_mark_leave_out_earlier_reads_at_the_same_address() {
let bytes = b"not a pdf".to_vec();
let start = super::read_mark();
let _ = super::open_structural_with_options(&bytes, None, &super::open_options());
let mark = super::read_mark();
assert_eq!(super::reads_of_since(&bytes, start), 1, "the read from the start");
assert_eq!(super::reads_of_since(&bytes, mark), 0, "the read before the mark");
let _ = super::open_structural_with_options(&bytes, None, &super::open_options());
assert_eq!(super::reads_of_since(&bytes, mark), 1, "the read after the mark");
}
}