From d3320015cd1351ec81f50e67ffcb073667e4c727 Mon Sep 17 00:00:00 2001 From: Bob Singor Date: Sun, 29 Nov 2026 21:35:13 +0200 Subject: [PATCH] Read an annotation's dictionary const when asking where it draws EPDFAnnot_GetDrawingRect() took the annotation's dictionary with GetMutableAnnotDict() to build the CPDF_Annot it asks. On a layer that copies the dictionary in, outside any transaction, though the call only reads: an appearance that is missing is drawn in memory, and nothing is written. The engine asks where an annotation or a widget without a stored appearance draws before it renders it. So the first render of a page left a copy of each such annotation of the file in the layer. The engine tells whether a page changed by what the layer holds of it: after a page's delete was undone and the page was rendered for the first time, it took the page for a changed one and left the redo of the delete alone. The dictionary is now read const, as a render (fpdf_view.cpp) and the flattener read it for the same drawing. Test: EPDFAnnotEmbedderTest.AskingWhereAMarkDrawsLeavesALayerAlone. It fails without the change: the layer holds one object after the call. It leaves nothing in a layer, so the list of layer-mode writers stays as it is. --- docs/embedpdf/fork-ledger.md | 6 ++++-- fpdfsdk/epdf_annot.cpp | 8 +++++--- fpdfsdk/epdf_annot_embeddertest.cpp | 29 +++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/docs/embedpdf/fork-ledger.md b/docs/embedpdf/fork-ledger.md index 2635fb7b5..9a9868fd7 100644 --- a/docs/embedpdf/fork-ledger.md +++ b/docs/embedpdf/fork-ledger.md @@ -118,10 +118,12 @@ saved. edits, and the copy happens outside any transaction. On a frozen base it writes shared state. **Where.** Const walks in the content parser, `cpdf_interactiveform.cpp`, `cpdf_nametree.cpp` (`CreateForReading`), `cpdf_linklist.cpp` and `cpdf_annotlist.cpp`; in-memory appearances in -`cpdf_annot.cpp`. Writes that read first: the field pruning in `fpdf_flatten.cpp` compares fields -read const and takes an array mutable only when an entry goes. +`cpdf_annot.cpp`, and where one draws (`EPDFAnnot_GetDrawingRect` in `epdf_annot.cpp`). Writes +that read first: the field pruning in `fpdf_flatten.cpp` compares fields read const and takes an +array mutable only when an entry goes. **Guarded by.** `cpdf_read_only_graph_guard_unittest.cpp`, `EPDFAnnotEmbedderTest.DrawingARedactionMarkWithoutAppearanceWritesNone`, +`EPDFAnnotEmbedderTest.AskingWhereAMarkDrawsLeavesALayerAlone`, `EPDFStampResizeEmbedderTest.ADrawingFromALayersPageLeavesTheLayerAlone`; layer mode's list of tests that write (`testing/embedpdf_layer_mode_writers.txt`): a test that only reads must leave nothing in a layer. diff --git a/fpdfsdk/epdf_annot.cpp b/fpdfsdk/epdf_annot.cpp index 175f2464b..27c4daed9 100644 --- a/fpdfsdk/epdf_annot.cpp +++ b/fpdfsdk/epdf_annot.cpp @@ -4537,13 +4537,15 @@ EPDFAnnot_GetDrawingRect(FPDF_ANNOTATION annot, if (!context || !rect) { return false; } - RetainPtr dict = context->GetMutableAnnotDict(); + const CPDF_Dictionary* dict = context->GetAnnotDict(); CPDF_Document* doc = context->GetPage()->GetDocument(); if (!dict || !doc) { return false; } - // Generating only when drawn: asking where it draws writes nothing. - CPDF_Annot drawn(std::move(dict), doc, + // Generating only when drawn, from a dictionary read as a render reads it: + // asking where it draws writes nothing. Taken for writing, the dictionary + // would be copied into a layer. + CPDF_Annot drawn(pdfium::WrapRetain(const_cast(dict)), doc, CPDF_Annot::MissingAppearance::kGenerateWhenDrawn); std::optional box = drawn.GetDrawingRect( static_cast(appearanceMode)); diff --git a/fpdfsdk/epdf_annot_embeddertest.cpp b/fpdfsdk/epdf_annot_embeddertest.cpp index 8bd5ec5a5..f23e06acd 100644 --- a/fpdfsdk/epdf_annot_embeddertest.cpp +++ b/fpdfsdk/epdf_annot_embeddertest.cpp @@ -1364,6 +1364,35 @@ TEST_F(EPDFAnnotEmbedderTest, DrawingARedactionMarkWithoutAppearanceWritesNone) EPDF_ReleaseBaseDocument(base); } +// Asking where a mark without an appearance draws (redact_annot.pdf) only +// reads it: on a layer the mark's dictionary is not copied in, so the layer +// holds as little afterwards as before. +TEST_F(EPDFAnnotEmbedderTest, AskingWhereAMarkDrawsLeavesALayerAlone) { + FileAccessForTesting base_access("redact_annot.pdf"); + EPDF_BASE_DOCUMENT base = EPDF_LoadBaseDocument(&base_access, nullptr); + ASSERT_TRUE(base); + { + ScopedFPDFDocument layer( + EPDFLayer_OpenLayer(base, nullptr, nullptr, nullptr)); + ASSERT_TRUE(layer); + ScopedFPDFPage page(FPDF_LoadPage(layer.get(), 0)); + ASSERT_TRUE(page); + ScopedFPDFAnnotation mark(FPDFPage_GetAnnot(page.get(), 0)); + ASSERT_EQ(FPDF_ANNOT_REDACT, FPDFAnnot_GetSubtype(mark.get())); + ASSERT_FALSE(FPDFAnnot_HasKey(mark.get(), "AP")); + ASSERT_EQ(0u, EPDFLayer_GetPromotedObjectCount(layer.get())); + + FS_RECTF drawn = {}; + ASSERT_TRUE(EPDFAnnot_GetDrawingRect( + mark.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, &drawn)); + EXPECT_GT(drawn.right, drawn.left); + EXPECT_GT(drawn.top, drawn.bottom); + EXPECT_FALSE(FPDFAnnot_HasKey(mark.get(), "AP")); + EXPECT_EQ(0u, EPDFLayer_GetPromotedObjectCount(layer.get())); + } + EPDF_ReleaseBaseDocument(base); +} + TEST_F(EPDFAnnotEmbedderTest, DrawingRectIsWhereTheAppearanceIsDrawn) { embedpdf_test::ScopedTestDocument doc(NewTestDocument()); ASSERT_TRUE(doc);