Add mutable form of GetAnnotDict() to CPDF_AnnotContext.

Change-Id: I08e8b5ba6ff26da19c1da0c9d8805fc42d2bcd95
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/94613
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fpdfapi/page/cpdf_annotcontext.h b/core/fpdfapi/page/cpdf_annotcontext.h
index c3c23c4..265e82a 100644
--- a/core/fpdfapi/page/cpdf_annotcontext.h
+++ b/core/fpdfapi/page/cpdf_annotcontext.h
@@ -27,7 +27,8 @@
   CPDF_Form* GetForm() const { return m_pAnnotForm.get(); }
 
   // Never nullptr.
-  CPDF_Dictionary* GetAnnotDict() const { return m_pAnnotDict.Get(); }
+  RetainPtr<CPDF_Dictionary> GetMutableAnnotDict() { return m_pAnnotDict; }
+  const CPDF_Dictionary* GetAnnotDict() const { return m_pAnnotDict.Get(); }
 
   // Never nullptr.
   IPDF_Page* GetPage() const { return m_pPage.Get(); }
diff --git a/fpdfsdk/fpdf_annot.cpp b/fpdfsdk/fpdf_annot.cpp
index 9cb2396..cf94674 100644
--- a/fpdfsdk/fpdf_annot.cpp
+++ b/fpdfsdk/fpdf_annot.cpp
@@ -224,11 +224,18 @@
   }
 }
 
-CPDF_Dictionary* GetAnnotDictFromFPDFAnnotation(FPDF_ANNOTATION annot) {
+const CPDF_Dictionary* GetAnnotDictFromFPDFAnnotation(
+    const FPDF_ANNOTATION annot) {
   CPDF_AnnotContext* context = CPDFAnnotContextFromFPDFAnnotation(annot);
   return context ? context->GetAnnotDict() : nullptr;
 }
 
+RetainPtr<CPDF_Dictionary> GetMutableAnnotDictFromFPDFAnnotation(
+    FPDF_ANNOTATION annot) {
+  CPDF_AnnotContext* context = CPDFAnnotContextFromFPDFAnnotation(annot);
+  return context ? context->GetMutableAnnotDict() : nullptr;
+}
+
 RetainPtr<CPDF_Dictionary> SetExtGStateInResourceDict(
     CPDF_Document* pDoc,
     const CPDF_Dictionary* pAnnotDict,
@@ -268,7 +275,7 @@
 }
 
 CPDF_FormField* GetFormField(FPDF_FORMHANDLE hHandle, FPDF_ANNOTATION annot) {
-  CPDF_Dictionary* pAnnotDict = GetAnnotDictFromFPDFAnnotation(annot);
+  const CPDF_Dictionary* pAnnotDict = GetAnnotDictFromFPDFAnnotation(annot);
   if (!pAnnotDict)
     return nullptr;
 
@@ -282,7 +289,7 @@
 
 const CPDFSDK_Widget* GetRadioButtonOrCheckBoxWidget(FPDF_FORMHANDLE hHandle,
                                                      FPDF_ANNOTATION annot) {
-  CPDF_Dictionary* pAnnotDict = GetAnnotDictFromFPDFAnnotation(annot);
+  const CPDF_Dictionary* pAnnotDict = GetAnnotDictFromFPDFAnnotation(annot);
   if (!pAnnotDict)
     return nullptr;
 
@@ -433,7 +440,7 @@
 
 FPDF_EXPORT FPDF_ANNOTATION_SUBTYPE FPDF_CALLCONV
 FPDFAnnot_GetSubtype(FPDF_ANNOTATION annot) {
-  CPDF_Dictionary* pAnnotDict = GetAnnotDictFromFPDFAnnotation(annot);
+  const CPDF_Dictionary* pAnnotDict = GetAnnotDictFromFPDFAnnotation(annot);
   if (!pAnnotDict)
     return FPDF_ANNOT_UNKNOWN;
 
@@ -460,8 +467,9 @@
 
   // Check that the annotation already has an appearance stream, since an
   // existing object is to be updated.
+  RetainPtr<CPDF_Dictionary> pAnnotDict = pAnnot->GetMutableAnnotDict();
   CPDF_Stream* pStream =
-      GetAnnotAP(pAnnot->GetAnnotDict(), CPDF_Annot::AppearanceMode::kNormal);
+      GetAnnotAP(pAnnotDict.Get(), CPDF_Annot::AppearanceMode::kNormal);
   if (!pStream)
     return false;
 
@@ -489,9 +497,9 @@
     return -1;
   }
 
-  CPDF_Dictionary* annot_dict = GetAnnotDictFromFPDFAnnotation(annot);
-
-  CPDF_Array* inklist = GetOrCreateArray(annot_dict, "InkList");
+  RetainPtr<CPDF_Dictionary> annot_dict =
+      GetMutableAnnotDictFromFPDFAnnotation(annot);
+  CPDF_Array* inklist = GetOrCreateArray(annot_dict.Get(), "InkList");
   FX_SAFE_SIZE_T safe_ink_size = inklist->size();
   safe_ink_size += 1;
   if (!safe_ink_size.IsValid<int32_t>())
@@ -511,8 +519,8 @@
   if (FPDFAnnot_GetSubtype(annot) != FPDF_ANNOT_INK)
     return false;
 
-  CPDF_Dictionary* annot_dict =
-      CPDFAnnotContextFromFPDFAnnotation(annot)->GetAnnotDict();
+  RetainPtr<CPDF_Dictionary> annot_dict =
+      CPDFAnnotContextFromFPDFAnnotation(annot)->GetMutableAnnotDict();
   annot_dict->RemoveFor("InkList");
   return true;
 }
@@ -529,13 +537,13 @@
     return false;
 
   // If the annotation does not have an AP stream yet, generate and set it.
-  CPDF_Dictionary* pAnnotDict = pAnnot->GetAnnotDict();
+  RetainPtr<CPDF_Dictionary> pAnnotDict = pAnnot->GetMutableAnnotDict();
   CPDF_Stream* pStream =
-      GetAnnotAP(pAnnotDict, CPDF_Annot::AppearanceMode::kNormal);
+      GetAnnotAP(pAnnotDict.Get(), CPDF_Annot::AppearanceMode::kNormal);
   if (!pStream) {
     CPDF_GenerateAP::GenerateEmptyAP(pAnnot->GetPage()->GetDocument(),
-                                     pAnnotDict);
-    pStream = GetAnnotAP(pAnnotDict, CPDF_Annot::AppearanceMode::kNormal);
+                                     pAnnotDict.Get());
+    pStream = GetAnnotAP(pAnnotDict.Get(), CPDF_Annot::AppearanceMode::kNormal);
     if (!pStream)
       return false;
   }
@@ -572,8 +580,9 @@
     return 0;
 
   if (!pAnnot->HasForm()) {
+    RetainPtr<CPDF_Dictionary> pDict = pAnnot->GetMutableAnnotDict();
     CPDF_Stream* pStream =
-        GetAnnotAP(pAnnot->GetAnnotDict(), CPDF_Annot::AppearanceMode::kNormal);
+        GetAnnotAP(pDict.Get(), CPDF_Annot::AppearanceMode::kNormal);
     if (!pStream)
       return 0;
 
@@ -590,8 +599,9 @@
     return nullptr;
 
   if (!pAnnot->HasForm()) {
+    RetainPtr<CPDF_Dictionary> pAnnotDict = pAnnot->GetMutableAnnotDict();
     CPDF_Stream* pStream =
-        GetAnnotAP(pAnnot->GetAnnotDict(), CPDF_Annot::AppearanceMode::kNormal);
+        GetAnnotAP(pAnnotDict.Get(), CPDF_Annot::AppearanceMode::kNormal);
     if (!pStream)
       return nullptr;
 
@@ -614,8 +624,9 @@
 
   // Check that the annotation already has an appearance stream, since an
   // existing object is to be deleted.
+  RetainPtr<CPDF_Dictionary> pAnnotDict = pAnnot->GetMutableAnnotDict();
   CPDF_Stream* pStream =
-      GetAnnotAP(pAnnot->GetAnnotDict(), CPDF_Annot::AppearanceMode::kNormal);
+      GetAnnotAP(pAnnotDict.Get(), CPDF_Annot::AppearanceMode::kNormal);
   if (!pStream)
     return false;
 
@@ -632,14 +643,16 @@
                                                        unsigned int G,
                                                        unsigned int B,
                                                        unsigned int A) {
-  CPDF_Dictionary* pAnnotDict = GetAnnotDictFromFPDFAnnotation(annot);
+  RetainPtr<CPDF_Dictionary> pAnnotDict =
+      GetMutableAnnotDictFromFPDFAnnotation(annot);
+
   if (!pAnnotDict || R > 255 || G > 255 || B > 255 || A > 255)
     return false;
 
   // For annotations with their appearance streams already defined, the path
   // stream's own color definitions take priority over the annotation color
   // definitions set by this method, hence this method will simply fail.
-  if (HasAPStream(pAnnotDict))
+  if (HasAPStream(pAnnotDict.Get()))
     return false;
 
   // Set the opacity of the annotation.
@@ -666,14 +679,16 @@
                                                        unsigned int* G,
                                                        unsigned int* B,
                                                        unsigned int* A) {
-  CPDF_Dictionary* pAnnotDict = GetAnnotDictFromFPDFAnnotation(annot);
+  RetainPtr<CPDF_Dictionary> pAnnotDict =
+      GetMutableAnnotDictFromFPDFAnnotation(annot);
+
   if (!pAnnotDict || !R || !G || !B || !A)
     return false;
 
   // For annotations with their appearance streams already defined, the path
   // stream's own color definitions take priority over the annotation color
   // definitions retrieved by this method, hence this method will simply fail.
-  if (HasAPStream(pAnnotDict))
+  if (HasAPStream(pAnnotDict.Get()))
     return false;
 
   const CPDF_Array* pColor = pAnnotDict->GetArrayFor(
@@ -740,14 +755,15 @@
   if (!FPDFAnnot_HasAttachmentPoints(annot) || !quad_points)
     return false;
 
-  CPDF_Dictionary* pAnnotDict =
-      CPDFAnnotContextFromFPDFAnnotation(annot)->GetAnnotDict();
-  CPDF_Array* pQuadPointsArray = GetQuadPointsArrayFromDictionary(pAnnotDict);
+  RetainPtr<CPDF_Dictionary> pAnnotDict =
+      CPDFAnnotContextFromFPDFAnnotation(annot)->GetMutableAnnotDict();
+  CPDF_Array* pQuadPointsArray =
+      GetQuadPointsArrayFromDictionary(pAnnotDict.Get());
   if (!IsValidQuadPointsIndex(pQuadPointsArray, quad_index))
     return false;
 
   SetQuadPointsAtIndex(pQuadPointsArray, quad_index, quad_points);
-  UpdateBBox(pAnnotDict);
+  UpdateBBox(pAnnotDict.Get());
   return true;
 }
 
@@ -757,13 +773,14 @@
   if (!FPDFAnnot_HasAttachmentPoints(annot) || !quad_points)
     return false;
 
-  CPDF_Dictionary* pAnnotDict =
-      CPDFAnnotContextFromFPDFAnnotation(annot)->GetAnnotDict();
-  CPDF_Array* pQuadPointsArray = GetQuadPointsArrayFromDictionary(pAnnotDict);
+  RetainPtr<CPDF_Dictionary> pAnnotDict =
+      CPDFAnnotContextFromFPDFAnnotation(annot)->GetMutableAnnotDict();
+  CPDF_Array* pQuadPointsArray =
+      GetQuadPointsArrayFromDictionary(pAnnotDict.Get());
   if (!pQuadPointsArray)
-    pQuadPointsArray = AddQuadPointsArrayToDictionary(pAnnotDict);
+    pQuadPointsArray = AddQuadPointsArrayToDictionary(pAnnotDict.Get());
   AppendQuadPoints(pQuadPointsArray, quad_points);
-  UpdateBBox(pAnnotDict);
+  UpdateBBox(pAnnotDict.Get());
   return true;
 }
 
@@ -796,7 +813,8 @@
 
 FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFAnnot_SetRect(FPDF_ANNOTATION annot,
                                                       const FS_RECTF* rect) {
-  CPDF_Dictionary* pAnnotDict = GetAnnotDictFromFPDFAnnotation(annot);
+  RetainPtr<CPDF_Dictionary> pAnnotDict =
+      GetMutableAnnotDictFromFPDFAnnotation(annot);
   if (!pAnnotDict || !rect)
     return false;
 
@@ -814,7 +832,7 @@
     return true;
 
   CPDF_Stream* pStream =
-      GetAnnotAP(pAnnotDict, CPDF_Annot::AppearanceMode::kNormal);
+      GetAnnotAP(pAnnotDict.Get(), CPDF_Annot::AppearanceMode::kNormal);
   if (pStream && newRect.Contains(pStream->GetDict()->GetRectFor("BBox")))
     pStream->GetDict()->SetRectFor("BBox", newRect);
   return true;
@@ -920,7 +938,8 @@
                                                         float horizontal_radius,
                                                         float vertical_radius,
                                                         float border_width) {
-  CPDF_Dictionary* annot_dict = GetAnnotDictFromFPDFAnnotation(annot);
+  RetainPtr<CPDF_Dictionary> annot_dict =
+      GetMutableAnnotDictFromFPDFAnnotation(annot);
   if (!annot_dict)
     return false;
 
@@ -979,7 +998,8 @@
 FPDFAnnot_SetStringValue(FPDF_ANNOTATION annot,
                          FPDF_BYTESTRING key,
                          FPDF_WIDESTRING value) {
-  CPDF_Dictionary* pAnnotDict = GetAnnotDictFromFPDFAnnotation(annot);
+  RetainPtr<CPDF_Dictionary> pAnnotDict =
+      GetMutableAnnotDictFromFPDFAnnotation(annot);
   if (!pAnnotDict)
     return false;
 
@@ -1024,7 +1044,8 @@
 FPDFAnnot_SetAP(FPDF_ANNOTATION annot,
                 FPDF_ANNOT_APPEARANCEMODE appearanceMode,
                 FPDF_WIDESTRING value) {
-  CPDF_Dictionary* pAnnotDict = GetAnnotDictFromFPDFAnnotation(annot);
+  RetainPtr<CPDF_Dictionary> pAnnotDict =
+      GetMutableAnnotDictFromFPDFAnnotation(annot);
   if (!pAnnotDict)
     return false;
 
@@ -1037,7 +1058,8 @@
                 "FPDF_ANNOT_APPEARANCEMODE_COUNT");
   const char* modeKey = kModeKeyForMode[appearanceMode];
 
-  CPDF_Dictionary* pApDict = pAnnotDict->GetDictFor(pdfium::annotation::kAP);
+  RetainPtr<CPDF_Dictionary> pApDict =
+      pAnnotDict->GetMutableDictFor(pdfium::annotation::kAP);
 
   // If value is null, we're in remove mode. Otherwise, we're in add/update
   // mode.
@@ -1072,7 +1094,7 @@
     // color.
     if (pAnnotDict->KeyExist("CA") && pAnnotDict->GetNumberFor("CA") < 1.0f) {
       RetainPtr<CPDF_Dictionary> pResourceDict =
-          SetExtGStateInResourceDict(pDoc, pAnnotDict, "Normal");
+          SetExtGStateInResourceDict(pDoc, pAnnotDict.Get(), "Normal");
       pStreamDict->SetFor("Resources", pResourceDict);
     }
 
@@ -1099,7 +1121,8 @@
                 FPDF_ANNOT_APPEARANCEMODE appearanceMode,
                 FPDF_WCHAR* buffer,
                 unsigned long buflen) {
-  CPDF_Dictionary* pAnnotDict = GetAnnotDictFromFPDFAnnotation(annot);
+  RetainPtr<CPDF_Dictionary> pAnnotDict =
+      GetMutableAnnotDictFromFPDFAnnotation(annot);
   if (!pAnnotDict)
     return 0;
 
@@ -1109,7 +1132,7 @@
   CPDF_Annot::AppearanceMode mode =
       static_cast<CPDF_Annot::AppearanceMode>(appearanceMode);
 
-  CPDF_Stream* pStream = GetAnnotAPNoFallback(pAnnotDict, mode);
+  CPDF_Stream* pStream = GetAnnotAPNoFallback(pAnnotDict.Get(), mode);
   return Utf16EncodeMaybeCopyAndReturnLength(
       pStream ? pStream->GetUnicodeText() : WideString(), buffer, buflen);
 }
@@ -1120,12 +1143,13 @@
   if (!pAnnot)
     return nullptr;
 
-  CPDF_Dictionary* pLinkedDict = pAnnot->GetAnnotDict()->GetDictFor(key);
+  RetainPtr<CPDF_Dictionary> pLinkedDict =
+      pAnnot->GetMutableAnnotDict()->GetMutableDictFor(key);
   if (!pLinkedDict || pLinkedDict->GetNameFor("Type") != "Annot")
     return nullptr;
 
   auto pLinkedAnnot =
-      std::make_unique<CPDF_AnnotContext>(pLinkedDict, pAnnot->GetPage());
+      std::make_unique<CPDF_AnnotContext>(pLinkedDict.Get(), pAnnot->GetPage());
 
   // Caller takes ownership.
   return FPDFAnnotationFromCPDFAnnotContext(pLinkedAnnot.release());
@@ -1139,7 +1163,8 @@
 
 FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFAnnot_SetFlags(FPDF_ANNOTATION annot,
                                                        int flags) {
-  CPDF_Dictionary* pAnnotDict = GetAnnotDictFromFPDFAnnotation(annot);
+  RetainPtr<CPDF_Dictionary> pAnnotDict =
+      GetMutableAnnotDictFromFPDFAnnotation(annot);
   if (!pAnnotDict)
     return false;
 
@@ -1260,7 +1285,7 @@
   if (!pForm)
     return false;
 
-  CPDF_Dictionary* pAnnotDict = GetAnnotDictFromFPDFAnnotation(annot);
+  const CPDF_Dictionary* pAnnotDict = GetAnnotDictFromFPDFAnnotation(annot);
   if (!pAnnotDict)
     return false;
 
@@ -1350,7 +1375,7 @@
     return nullptr;
 
   return FPDFLinkFromCPDFDictionary(
-      CPDFAnnotContextFromFPDFAnnotation(annot)->GetAnnotDict());
+      CPDFAnnotContextFromFPDFAnnotation(annot)->GetMutableAnnotDict().Get());
 }
 
 FPDF_EXPORT int FPDF_CALLCONV
@@ -1361,7 +1386,7 @@
 
 FPDF_EXPORT int FPDF_CALLCONV
 FPDFAnnot_GetFormControlIndex(FPDF_FORMHANDLE hHandle, FPDF_ANNOTATION annot) {
-  CPDF_Dictionary* pAnnotDict = GetAnnotDictFromFPDFAnnotation(annot);
+  const CPDF_Dictionary* pAnnotDict = GetAnnotDictFromFPDFAnnotation(annot);
   if (!pAnnotDict)
     return -1;
 
@@ -1394,7 +1419,8 @@
   if (!uri || FPDFAnnot_GetSubtype(annot) != FPDF_ANNOT_LINK)
     return false;
 
-  CPDF_Dictionary* annot_dict = GetAnnotDictFromFPDFAnnotation(annot);
+  RetainPtr<CPDF_Dictionary> annot_dict =
+      GetMutableAnnotDictFromFPDFAnnotation(annot);
   CPDF_Dictionary* action = annot_dict->SetNewFor<CPDF_Dictionary>("A");
   action->SetNewFor<CPDF_Name>("Type", "Action");
   action->SetNewFor<CPDF_Name>("S", "URI");
diff --git a/fpdfsdk/fpdf_annot_embeddertest.cpp b/fpdfsdk/fpdf_annot_embeddertest.cpp
index 63affb9..d138185 100644
--- a/fpdfsdk/fpdf_annot_embeddertest.cpp
+++ b/fpdfsdk/fpdf_annot_embeddertest.cpp
@@ -136,14 +136,15 @@
   // Verify that appearance stream is created as form XObject
   CPDF_AnnotContext* context = CPDFAnnotContextFromFPDFAnnotation(annot.get());
   ASSERT_TRUE(context);
-  CPDF_Dictionary* annot_dict = context->GetAnnotDict();
+  const CPDF_Dictionary* annot_dict = context->GetAnnotDict();
   ASSERT_TRUE(annot_dict);
-  CPDF_Dictionary* ap_dict = annot_dict->GetDictFor(pdfium::annotation::kAP);
+  const CPDF_Dictionary* ap_dict =
+      annot_dict->GetDictFor(pdfium::annotation::kAP);
   ASSERT_TRUE(ap_dict);
-  CPDF_Dictionary* stream_dict = ap_dict->GetDictFor("N");
+  const CPDF_Dictionary* stream_dict = ap_dict->GetDictFor("N");
   ASSERT_TRUE(stream_dict);
   // Check for non-existence of resources dictionary in case of opaque color
-  CPDF_Dictionary* resources_dict = stream_dict->GetDictFor("Resources");
+  const CPDF_Dictionary* resources_dict = stream_dict->GetDictFor("Resources");
   ASSERT_FALSE(resources_dict);
   ByteString type = stream_dict->GetStringFor(pdfium::annotation::kType);
   EXPECT_EQ("XObject", type);
@@ -184,17 +185,19 @@
 
   CPDF_AnnotContext* context = CPDFAnnotContextFromFPDFAnnotation(annot.get());
   ASSERT_TRUE(context);
-  CPDF_Dictionary* annot_dict = context->GetAnnotDict();
+  const CPDF_Dictionary* annot_dict = context->GetAnnotDict();
   ASSERT_TRUE(annot_dict);
-  CPDF_Dictionary* ap_dict = annot_dict->GetDictFor(pdfium::annotation::kAP);
+  const CPDF_Dictionary* ap_dict =
+      annot_dict->GetDictFor(pdfium::annotation::kAP);
   ASSERT_TRUE(ap_dict);
-  CPDF_Dictionary* stream_dict = ap_dict->GetDictFor("N");
+  const CPDF_Dictionary* stream_dict = ap_dict->GetDictFor("N");
   ASSERT_TRUE(stream_dict);
-  CPDF_Dictionary* resources_dict = stream_dict->GetDictFor("Resources");
+  const CPDF_Dictionary* resources_dict = stream_dict->GetDictFor("Resources");
   ASSERT_TRUE(stream_dict);
-  CPDF_Dictionary* extGState_dict = resources_dict->GetDictFor("ExtGState");
+  const CPDF_Dictionary* extGState_dict =
+      resources_dict->GetDictFor("ExtGState");
   ASSERT_TRUE(extGState_dict);
-  CPDF_Dictionary* gs_dict = extGState_dict->GetDictFor("GS");
+  const CPDF_Dictionary* gs_dict = extGState_dict->GetDictFor("GS");
   ASSERT_TRUE(gs_dict);
   ByteString type = gs_dict->GetStringFor(pdfium::annotation::kType);
   EXPECT_EQ("ExtGState", type);
@@ -220,7 +223,7 @@
   CPDF_AnnotContext* context =
       CPDFAnnotContextFromFPDFAnnotation(ink_annot.get());
   ASSERT_TRUE(context);
-  CPDF_Dictionary* annot_dict = context->GetAnnotDict();
+  const CPDF_Dictionary* annot_dict = context->GetAnnotDict();
   ASSERT_TRUE(annot_dict);
 
   static constexpr FS_POINTF kFirstInkStroke[] = {
@@ -267,7 +270,7 @@
   EXPECT_EQ(0, FPDFAnnot_AddInkStroke(ink_annot.get(), kFirstInkStroke,
                                       kFirstStrokePointCount));
 
-  CPDF_Array* inklist = annot_dict->GetArrayFor("InkList");
+  const CPDF_Array* inklist = annot_dict->GetArrayFor("InkList");
   ASSERT_TRUE(inklist);
   EXPECT_EQ(1u, inklist->size());
   EXPECT_EQ(kFirstStrokePointCount * 2, inklist->GetArrayAt(0)->size());
@@ -310,7 +313,7 @@
   CPDF_AnnotContext* context =
       CPDFAnnotContextFromFPDFAnnotation(ink_annot.get());
   ASSERT_TRUE(context);
-  CPDF_Dictionary* annot_dict = context->GetAnnotDict();
+  const CPDF_Dictionary* annot_dict = context->GetAnnotDict();
   ASSERT_TRUE(annot_dict);
 
   static constexpr FS_POINTF kInkStroke[] = {{80.0f, 90.0f}, {81.0f, 91.0f},
@@ -323,7 +326,7 @@
   EXPECT_EQ(0,
             FPDFAnnot_AddInkStroke(ink_annot.get(), kInkStroke, kPointCount));
 
-  CPDF_Array* inklist = annot_dict->GetArrayFor("InkList");
+  const CPDF_Array* inklist = annot_dict->GetArrayFor("InkList");
   ASSERT_TRUE(inklist);
   ASSERT_EQ(1u, inklist->size());
   EXPECT_EQ(kPointCount * 2, inklist->GetArrayAt(0)->size());
diff --git a/fpdfsdk/fpdf_formfill.cpp b/fpdfsdk/fpdf_formfill.cpp
index 91c0274..5b261cf 100644
--- a/fpdfsdk/fpdf_formfill.cpp
+++ b/fpdfsdk/fpdf_formfill.cpp
@@ -649,9 +649,9 @@
   if (!page_view->IsValid())
     return false;
 
-  CPDF_Dictionary* annot_dict = annot_context->GetAnnotDict();
+  RetainPtr<CPDF_Dictionary> annot_dict = annot_context->GetMutableAnnotDict();
   ObservedPtr<CPDFSDK_Annot> cpdfsdk_annot(
-      page_view->GetAnnotByDict(annot_dict));
+      page_view->GetAnnotByDict(annot_dict.Get()));
   if (!cpdfsdk_annot)
     return false;