Remove non-preferred forms of getters from CPDF_Dictionary.

Then deal with the fallout, either retaining as necessary or making
const references.

Change-Id: Idab065fe7c073255f2bf9a0a70eadba904533304
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/94670
Commit-Queue: Tom Sepez <tsepez@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
diff --git a/core/fpdfapi/font/cpdf_type3font.cpp b/core/fpdfapi/font/cpdf_type3font.cpp
index f2ed02b..8610d1e 100644
--- a/core/fpdfapi/font/cpdf_type3font.cpp
+++ b/core/fpdfapi/font/cpdf_type3font.cpp
@@ -57,7 +57,7 @@
 }
 
 bool CPDF_Type3Font::Load() {
-  m_pFontResources.Reset(m_pFontDict->GetDictFor("Resources"));
+  m_pFontResources = m_pFontDict->GetMutableDictFor("Resources");
   const CPDF_Array* pMatrix = m_pFontDict->GetArrayFor("FontMatrix");
   float xscale = 1.0f;
   float yscale = 1.0f;
@@ -90,7 +90,7 @@
       }
     }
   }
-  m_pCharProcs.Reset(m_pFontDict->GetDictFor("CharProcs"));
+  m_pCharProcs = m_pFontDict->GetMutableDictFor("CharProcs");
   if (m_pFontDict->GetDirectObjectFor("Encoding"))
     LoadPDFEncoding(false, false);
   return true;
diff --git a/core/fpdfapi/page/cpdf_contentparser.cpp b/core/fpdfapi/page/cpdf_contentparser.cpp
index eb99020..2fed936 100644
--- a/core/fpdfapi/page/cpdf_contentparser.cpp
+++ b/core/fpdfapi/page/cpdf_contentparser.cpp
@@ -66,7 +66,7 @@
   if (pGraphicStates)
     form_matrix.Concat(pGraphicStates->m_CTM);
 
-  CPDF_Array* pBBox = pForm->GetDict()->GetArrayFor("BBox");
+  const CPDF_Array* pBBox = pForm->GetDict()->GetArrayFor("BBox");
   CFX_FloatRect form_bbox;
   CPDF_Path ClipPath;
   if (pBBox) {
@@ -82,10 +82,12 @@
       form_bbox = pParentMatrix->TransformRect(form_bbox);
   }
 
-  CPDF_Dictionary* pResources = pForm->GetDict()->GetDictFor("Resources");
+  RetainPtr<CPDF_Dictionary> pResources =
+      pForm->GetDict()->GetMutableDictFor("Resources");
   m_pParser = std::make_unique<CPDF_StreamContentParser>(
       pForm->GetDocument(), pForm->GetPageResources(), pForm->GetResources(),
-      pParentMatrix, pForm, pResources, form_bbox, pGraphicStates, pParsedSet);
+      pParentMatrix, pForm, pResources.Get(), form_bbox, pGraphicStates,
+      pParsedSet);
   m_pParser->GetCurStates()->m_CTM = form_matrix;
   m_pParser->GetCurStates()->m_ParentMatrix = form_matrix;
   if (ClipPath.HasRef()) {
@@ -136,7 +138,7 @@
 CPDF_ContentParser::Stage CPDF_ContentParser::GetContent() {
   DCHECK_EQ(m_CurrentStage, Stage::kGetContent);
   DCHECK(m_pObjectHolder->IsPage());
-  CPDF_Array* pContent =
+  const CPDF_Array* pContent =
       m_pObjectHolder->GetDict()->GetArrayFor(pdfium::page_object::kContents);
   const CPDF_Stream* pStreamObj = ToStream(
       pContent ? pContent->GetDirectObjectAt(m_CurrentOffset) : nullptr);
diff --git a/core/fpdfapi/page/cpdf_form.cpp b/core/fpdfapi/page/cpdf_form.cpp
index 5e4176c..ab6a8e9 100644
--- a/core/fpdfapi/page/cpdf_form.cpp
+++ b/core/fpdfapi/page/cpdf_form.cpp
@@ -41,9 +41,10 @@
           pDoc,
           pFormStream->GetDict(),
           pPageResources,
-          ChooseResourcesDict(pFormStream->GetDict()->GetDictFor("Resources"),
-                              pParentResources,
-                              pPageResources)),
+          ChooseResourcesDict(
+              pFormStream->GetDict()->GetMutableDictFor("Resources").Get(),
+              pParentResources,
+              pPageResources)),
       m_pFormStream(pFormStream) {
   LoadTransparencyInfo();
 }
diff --git a/core/fpdfapi/page/cpdf_page.cpp b/core/fpdfapi/page/cpdf_page.cpp
index 79c411e..3201e06 100644
--- a/core/fpdfapi/page/cpdf_page.cpp
+++ b/core/fpdfapi/page/cpdf_page.cpp
@@ -80,10 +80,10 @@
 }
 
 const CPDF_Object* CPDF_Page::GetPageAttr(const ByteString& name) const {
-  std::set<CPDF_Dictionary*> visited;
-  CPDF_Dictionary* pPageDict = GetDict();
+  std::set<const CPDF_Dictionary*> visited;
+  const CPDF_Dictionary* pPageDict = GetDict();
   while (pPageDict && !pdfium::Contains(visited, pPageDict)) {
-    CPDF_Object* pObj = pPageDict->GetDirectObjectFor(name);
+    const CPDF_Object* pObj = pPageDict->GetDirectObjectFor(name);
     if (pObj)
       return pObj;
 
diff --git a/core/fpdfapi/page/cpdf_pageobjectholder.cpp b/core/fpdfapi/page/cpdf_pageobjectholder.cpp
index 671a497..b820b44 100644
--- a/core/fpdfapi/page/cpdf_pageobjectholder.cpp
+++ b/core/fpdfapi/page/cpdf_pageobjectholder.cpp
@@ -113,7 +113,7 @@
 }
 
 void CPDF_PageObjectHolder::LoadTransparencyInfo() {
-  CPDF_Dictionary* pGroup = m_pDict->GetDictFor("Group");
+  const CPDF_Dictionary* pGroup = m_pDict->GetDictFor("Group");
   if (!pGroup)
     return;
 
diff --git a/core/fpdfapi/page/cpdf_streamcontentparser.cpp b/core/fpdfapi/page/cpdf_streamcontentparser.cpp
index 12ce7e0..55ff2e9 100644
--- a/core/fpdfapi/page/cpdf_streamcontentparser.cpp
+++ b/core/fpdfapi/page/cpdf_streamcontentparser.cpp
@@ -1119,14 +1119,14 @@
   if (!m_pResources)
     return nullptr;
 
-  CPDF_Dictionary* pDict = m_pResources->GetDictFor(type);
+  RetainPtr<CPDF_Dictionary> pDict = m_pResources->GetMutableDictFor(type);
   if (pDict)
-    return pDict;
+    return pDict.Get();  // TODO(tsepez): return retained and below.
 
   if (m_pResources == m_pPageResources || !m_pPageResources)
     return nullptr;
 
-  return m_pPageResources->GetDictFor(type);
+  return m_pPageResources->GetMutableDictFor(type).Get();
 }
 
 CPDF_Object* CPDF_StreamContentParser::FindResourceObj(const ByteString& type,
diff --git a/core/fpdfapi/parser/cfdf_document.cpp b/core/fpdfapi/parser/cfdf_document.cpp
index 13eab14..93adcd8 100644
--- a/core/fpdfapi/parser/cfdf_document.cpp
+++ b/core/fpdfapi/parser/cfdf_document.cpp
@@ -68,7 +68,7 @@
       RetainPtr<CPDF_Dictionary> pMainDict =
           ToDictionary(parser.GetObjectBody(this));
       if (pMainDict)
-        m_pRootDict.Reset(pMainDict->GetDictFor("Root"));
+        m_pRootDict = pMainDict->GetMutableDictFor("Root");
 
       break;
     }
diff --git a/core/fpdfapi/parser/cpdf_dictionary.cpp b/core/fpdfapi/parser/cpdf_dictionary.cpp
index 518600d..56d1e53 100644
--- a/core/fpdfapi/parser/cpdf_dictionary.cpp
+++ b/core/fpdfapi/parser/cpdf_dictionary.cpp
@@ -148,11 +148,6 @@
   return p ? p->GetNumber() : 0;
 }
 
-RetainPtr<CPDF_Dictionary> CPDF_Dictionary::GetMutableDictFor(
-    const ByteString& key) {
-  return pdfium::WrapRetain(GetDictFor(key));
-}
-
 const CPDF_Dictionary* CPDF_Dictionary::GetDictFor(
     const ByteString& key) const {
   const CPDF_Object* p = GetDirectObjectFor(key);
@@ -165,22 +160,18 @@
   return nullptr;
 }
 
-CPDF_Dictionary* CPDF_Dictionary::GetDictFor(const ByteString& key) {
-  return const_cast<CPDF_Dictionary*>(
-      static_cast<const CPDF_Dictionary*>(this)->GetDictFor(key));
-}
-
-RetainPtr<CPDF_Array> CPDF_Dictionary::GetMutableArrayFor(
+RetainPtr<CPDF_Dictionary> CPDF_Dictionary::GetMutableDictFor(
     const ByteString& key) {
-  return pdfium::WrapRetain(GetArrayFor(key));
+  return pdfium::WrapRetain(const_cast<CPDF_Dictionary*>(GetDictFor(key)));
 }
 
 const CPDF_Array* CPDF_Dictionary::GetArrayFor(const ByteString& key) const {
   return ToArray(GetDirectObjectFor(key));
 }
 
-CPDF_Array* CPDF_Dictionary::GetArrayFor(const ByteString& key) {
-  return ToArray(GetDirectObjectFor(key));
+RetainPtr<CPDF_Array> CPDF_Dictionary::GetMutableArrayFor(
+    const ByteString& key) {
+  return pdfium::WrapRetain(const_cast<CPDF_Array*>(GetArrayFor(key)));
 }
 
 const CPDF_Stream* CPDF_Dictionary::GetStreamFor(const ByteString& key) const {
diff --git a/core/fpdfapi/parser/cpdf_dictionary.h b/core/fpdfapi/parser/cpdf_dictionary.h
index dab24b4..7f187b7 100644
--- a/core/fpdfapi/parser/cpdf_dictionary.h
+++ b/core/fpdfapi/parser/cpdf_dictionary.h
@@ -68,10 +68,8 @@
   int GetIntegerFor(const ByteString& key, int default_int) const;
   float GetNumberFor(const ByteString& key) const;
   const CPDF_Dictionary* GetDictFor(const ByteString& key) const;
-  CPDF_Dictionary* GetDictFor(const ByteString& key);  // Prefer next form.
   RetainPtr<CPDF_Dictionary> GetMutableDictFor(const ByteString& key);
   const CPDF_Array* GetArrayFor(const ByteString& key) const;
-  CPDF_Array* GetArrayFor(const ByteString& key);  // Prefer next form.
   RetainPtr<CPDF_Array> GetMutableArrayFor(const ByteString& key);
   const CPDF_Stream* GetStreamFor(const ByteString& key) const;
   CPDF_Stream* GetStreamFor(const ByteString& key);
diff --git a/core/fpdfapi/parser/cpdf_document.cpp b/core/fpdfapi/parser/cpdf_document.cpp
index e493fdc..d090efa 100644
--- a/core/fpdfapi/parser/cpdf_document.cpp
+++ b/core/fpdfapi/parser/cpdf_document.cpp
@@ -32,7 +32,7 @@
   int count = pPages->GetIntegerFor("Count");
   if (count > 0 && count < CPDF_Document::kPageMaxNum)
     return count;
-  CPDF_Array* pKidList = pPages->GetArrayFor("Kids");
+  RetainPtr<CPDF_Array> pKidList = pPages->GetMutableArrayFor("Kids");
   if (!pKidList)
     return 0;
   count = 0;
@@ -189,7 +189,7 @@
     return nullptr;
 
   CPDF_Dictionary* pPages = m_pTreeTraversal[level].first;
-  CPDF_Array* pKidList = pPages->GetArrayFor("Kids");
+  RetainPtr<CPDF_Array> pKidList = pPages->GetMutableArrayFor("Kids");
   if (!pKidList) {
     m_pTreeTraversal.pop_back();
     if (*nPagesToGo != 1)
@@ -406,7 +406,7 @@
                                         CPDF_Dictionary* pPageDict,
                                         bool bInsert,
                                         std::set<CPDF_Dictionary*>* pVisited) {
-  CPDF_Array* pKidList = pPages->GetArrayFor("Kids");
+  RetainPtr<CPDF_Array> pKidList = pPages->GetMutableArrayFor("Kids");
   if (!pKidList)
     return false;
 
@@ -451,7 +451,10 @@
 
 bool CPDF_Document::InsertNewPage(int iPage, CPDF_Dictionary* pPageDict) {
   CPDF_Dictionary* pRoot = GetRoot();
-  CPDF_Dictionary* pPages = pRoot ? pRoot->GetDictFor("Pages") : nullptr;
+  if (!pRoot)
+    return false;
+
+  RetainPtr<CPDF_Dictionary> pPages = pRoot->GetMutableDictFor("Pages");
   if (!pPages)
     return false;
 
@@ -460,14 +463,14 @@
     return false;
 
   if (iPage == nPages) {
-    CPDF_Array* pPagesList = GetOrCreateArray(pPages, "Kids");
+    CPDF_Array* pPagesList = GetOrCreateArray(pPages.Get(), "Kids");
     pPagesList->AppendNew<CPDF_Reference>(this, pPageDict->GetObjNum());
     pPages->SetNewFor<CPDF_Number>("Count", nPages + 1);
     pPageDict->SetNewFor<CPDF_Reference>("Parent", this, pPages->GetObjNum());
     ResetTraversal();
   } else {
-    std::set<CPDF_Dictionary*> stack = {pPages};
-    if (!InsertDeletePDFPage(pPages, iPage, pPageDict, true, &stack))
+    std::set<CPDF_Dictionary*> stack = {pPages.Get()};
+    if (!InsertDeletePDFPage(pPages.Get(), iPage, pPageDict, true, &stack))
       return false;
   }
   m_PageList.insert(m_PageList.begin() + iPage, pPageDict->GetObjNum());
diff --git a/core/fpdfapi/parser/fpdf_parser_utility.cpp b/core/fpdfapi/parser/fpdf_parser_utility.cpp
index 086ef3d..b19e15d 100644
--- a/core/fpdfapi/parser/fpdf_parser_utility.cpp
+++ b/core/fpdfapi/parser/fpdf_parser_utility.cpp
@@ -94,16 +94,16 @@
 }
 
 CPDF_Array* GetOrCreateArray(CPDF_Dictionary* dict, const ByteString& key) {
-  CPDF_Array* result = dict->GetArrayFor(key);
+  RetainPtr<CPDF_Array> result = dict->GetMutableArrayFor(key);
   if (result)
-    return result;
+    return result.Get();
   return dict->SetNewFor<CPDF_Array>(key);
 }
 
 CPDF_Dictionary* GetOrCreateDict(CPDF_Dictionary* dict, const ByteString& key) {
-  CPDF_Dictionary* result = dict->GetDictFor(key);
+  RetainPtr<CPDF_Dictionary> result = dict->GetMutableDictFor(key);
   if (result)
-    return result;
+    return result.Get();
   return dict->SetNewFor<CPDF_Dictionary>(key);
 }
 
diff --git a/core/fpdfdoc/cpdf_annot.cpp b/core/fpdfdoc/cpdf_annot.cpp
index 49bcf2e..fa6d59b 100644
--- a/core/fpdfdoc/cpdf_annot.cpp
+++ b/core/fpdfdoc/cpdf_annot.cpp
@@ -59,7 +59,8 @@
 CPDF_Stream* GetAnnotAPInternal(CPDF_Dictionary* pAnnotDict,
                                 CPDF_Annot::AppearanceMode eMode,
                                 bool bFallbackToNormal) {
-  CPDF_Dictionary* pAP = pAnnotDict->GetDictFor(pdfium::annotation::kAP);
+  RetainPtr<CPDF_Dictionary> pAP =
+      pAnnotDict->GetMutableDictFor(pdfium::annotation::kAP);
   if (!pAP)
     return nullptr;
 
@@ -442,12 +443,12 @@
   if (!bPrinting && (annot_flags & pdfium::annotation_flags::kNoView)) {
     return;
   }
-  CPDF_Dictionary* pBS = m_pAnnotDict->GetDictFor("BS");
+  const CPDF_Dictionary* pBS = m_pAnnotDict->GetDictFor("BS");
   char style_char;
   float width;
   const CPDF_Array* pDashArray = nullptr;
   if (!pBS) {
-    CPDF_Array* pBorderArray =
+    const CPDF_Array* pBorderArray =
         m_pAnnotDict->GetArrayFor(pdfium::annotation::kBorder);
     style_char = 'S';
     if (pBorderArray) {
@@ -482,7 +483,7 @@
   if (width <= 0) {
     return;
   }
-  CPDF_Array* pColor = m_pAnnotDict->GetArrayFor(pdfium::annotation::kC);
+  const CPDF_Array* pColor = m_pAnnotDict->GetArrayFor(pdfium::annotation::kC);
   uint32_t argb = 0xff000000;
   if (pColor) {
     int R = static_cast<int32_t>(pColor->GetNumberAt(0) * 255);
diff --git a/core/fpdfdoc/cpdf_annotlist.cpp b/core/fpdfdoc/cpdf_annotlist.cpp
index ebf69a7..f2866fa 100644
--- a/core/fpdfdoc/cpdf_annotlist.cpp
+++ b/core/fpdfdoc/cpdf_annotlist.cpp
@@ -157,7 +157,7 @@
   if (pAnnotDict->KeyExist(pdfium::annotation::kAS))
     return;
 
-  CPDF_Dictionary* pParentDict =
+  const CPDF_Dictionary* pParentDict =
       pAnnotDict->GetDictFor(pdfium::form_fields::kParent);
   if (!pParentDict || !pParentDict->KeyExist(pdfium::annotation::kAS))
     return;
@@ -171,7 +171,8 @@
 
 CPDF_AnnotList::CPDF_AnnotList(CPDF_Page* pPage)
     : m_pDocument(pPage->GetDocument()) {
-  CPDF_Array* pAnnots = pPage->GetDict()->GetArrayFor("Annots");
+  RetainPtr<CPDF_Array> pAnnots =
+      pPage->GetDict()->GetMutableArrayFor("Annots");
   if (!pAnnots)
     return;
 
diff --git a/core/fpdfdoc/cpdf_apsettings.cpp b/core/fpdfdoc/cpdf_apsettings.cpp
index 121d516..747e822 100644
--- a/core/fpdfdoc/cpdf_apsettings.cpp
+++ b/core/fpdfdoc/cpdf_apsettings.cpp
@@ -31,7 +31,7 @@
   if (!m_pDict)
     return {CFX_Color::Type::kTransparent, 0};
 
-  CPDF_Array* pEntry = m_pDict->GetArrayFor(csEntry);
+  const CPDF_Array* pEntry = m_pDict->GetArrayFor(csEntry);
   if (!pEntry)
     return {CFX_Color::Type::kTransparent, 0};
 
@@ -65,7 +65,7 @@
   if (!m_pDict)
     return 0;
 
-  CPDF_Array* pEntry = m_pDict->GetArrayFor(csEntry);
+  const CPDF_Array* pEntry = m_pDict->GetArrayFor(csEntry);
   return pEntry ? pEntry->GetNumberAt(index) : 0;
 }
 
@@ -73,7 +73,7 @@
   if (!m_pDict)
     return CFX_Color();
 
-  CPDF_Array* pEntry = m_pDict->GetArrayFor(csEntry);
+  const CPDF_Array* pEntry = m_pDict->GetArrayFor(csEntry);
   if (!pEntry)
     return CFX_Color();
 
diff --git a/core/fpdfdoc/cpdf_bafontmap.cpp b/core/fpdfdoc/cpdf_bafontmap.cpp
index 0b363d1..272cef8 100644
--- a/core/fpdfdoc/cpdf_bafontmap.cpp
+++ b/core/fpdfdoc/cpdf_bafontmap.cpp
@@ -227,13 +227,13 @@
 }
 
 RetainPtr<CPDF_Font> CPDF_BAFontMap::GetAnnotDefaultFont(ByteString* sAlias) {
-  CPDF_Dictionary* pAcroFormDict = nullptr;
+  RetainPtr<CPDF_Dictionary> pAcroFormDict;
   const bool bWidget =
       (m_pAnnotDict->GetNameFor(pdfium::annotation::kSubtype) == "Widget");
   if (bWidget) {
     CPDF_Dictionary* pRootDict = m_pDocument->GetRoot();
     if (pRootDict)
-      pAcroFormDict = pRootDict->GetDictFor("AcroForm");
+      pAcroFormDict = pRootDict->GetMutableDictFor("AcroForm");
   }
 
   ByteString sDA;
@@ -244,7 +244,7 @@
 
   if (bWidget) {
     if (sDA.IsEmpty()) {
-      pObj = CPDF_FormField::GetFieldAttr(pAcroFormDict, "DA");
+      pObj = CPDF_FormField::GetFieldAttr(pAcroFormDict.Get(), "DA");
       sDA = pObj ? pObj->GetString() : ByteString();
     }
   }
@@ -257,20 +257,26 @@
   *sAlias = font.value_or(ByteString());
 
   RetainPtr<CPDF_Dictionary> pFontDict;
-  if (CPDF_Dictionary* pAPDict =
-          m_pAnnotDict->GetDictFor(pdfium::annotation::kAP)) {
-    if (CPDF_Dictionary* pNormalDict = pAPDict->GetDictFor("N")) {
-      if (CPDF_Dictionary* pNormalResDict =
-              pNormalDict->GetDictFor("Resources")) {
-        if (CPDF_Dictionary* pResFontDict = pNormalResDict->GetDictFor("Font"))
-          pFontDict = pResFontDict->GetDictFor(*sAlias);
+  if (RetainPtr<CPDF_Dictionary> pAPDict =
+          m_pAnnotDict->GetMutableDictFor(pdfium::annotation::kAP)) {
+    if (RetainPtr<CPDF_Dictionary> pNormalDict =
+            pAPDict->GetMutableDictFor("N")) {
+      if (RetainPtr<CPDF_Dictionary> pNormalResDict =
+              pNormalDict->GetMutableDictFor("Resources")) {
+        if (RetainPtr<CPDF_Dictionary> pResFontDict =
+                pNormalResDict->GetMutableDictFor("Font")) {
+          pFontDict = pResFontDict->GetMutableDictFor(*sAlias);
+        }
       }
     }
   }
   if (bWidget && !pFontDict && pAcroFormDict) {
-    if (CPDF_Dictionary* pDRDict = pAcroFormDict->GetDictFor("DR")) {
-      if (CPDF_Dictionary* pDRFontDict = pDRDict->GetDictFor("Font"))
-        pFontDict = pDRFontDict->GetDictFor(*sAlias);
+    if (RetainPtr<CPDF_Dictionary> pDRDict =
+            pAcroFormDict->GetMutableDictFor("DR")) {
+      if (RetainPtr<CPDF_Dictionary> pDRFontDict =
+              pDRDict->GetMutableDictFor("Font")) {
+        pFontDict = pDRFontDict->GetMutableDictFor(*sAlias);
+      }
     }
   }
   if (!pFontDict)
@@ -306,7 +312,8 @@
   }
 
   CPDF_Dictionary* pStreamResList = GetOrCreateDict(pStreamDict, "Resources");
-  CPDF_Dictionary* pStreamResFontList = pStreamResList->GetDictFor("Font");
+  RetainPtr<CPDF_Dictionary> pStreamResFontList =
+      pStreamResList->GetMutableDictFor("Font");
   if (!pStreamResFontList) {
     pStreamResFontList = m_pDocument->NewIndirect<CPDF_Dictionary>();
     pStreamResList->SetNewFor<CPDF_Reference>("Font", m_pDocument.Get(),
diff --git a/core/fpdfdoc/cpdf_filespec_unittest.cpp b/core/fpdfdoc/cpdf_filespec_unittest.cpp
index 2458677..4b394a0 100644
--- a/core/fpdfdoc/cpdf_filespec_unittest.cpp
+++ b/core/fpdfdoc/cpdf_filespec_unittest.cpp
@@ -198,8 +198,8 @@
     const char* const keys[] = {"Unix", "Mac", "DOS", "F", "UF"};
     const char* const streams[] = {"test1", "test2", "test3", "test4", "test5"};
     static_assert(std::size(keys) == std::size(streams), "size mismatch");
-    CPDF_Dictionary* file_dict =
-        file_spec.GetObj()->AsDictionary()->GetDictFor("EF");
+    RetainPtr<CPDF_Dictionary> file_dict =
+        file_spec.GetObj()->AsDictionary()->GetMutableDictFor("EF");
 
     // Keys in reverse order of precedence to retrieve the file content stream.
     for (size_t i = 0; i < std::size(keys); ++i) {
@@ -244,8 +244,8 @@
     EXPECT_FALSE(file_spec.GetParamsDict());
 
     // Add a file stream to the embedded files dictionary.
-    CPDF_Dictionary* file_dict =
-        file_spec.GetObj()->AsDictionary()->GetDictFor("EF");
+    RetainPtr<CPDF_Dictionary> file_dict =
+        file_spec.GetObj()->AsDictionary()->GetMutableDictFor("EF");
     auto pDict = pdfium::MakeRetain<CPDF_Dictionary>();
     std::unique_ptr<uint8_t, FxFreeDeleter> buf(FX_AllocUninit(uint8_t, 6));
     memcpy(buf.get(), "hello", 6);
@@ -259,7 +259,8 @@
     EXPECT_TRUE(file_spec.GetParamsDict());
 
     // Add a parameter to the params dictionary.
-    CPDF_Dictionary* params_dict = stream_dict->GetDictFor("Params");
+    RetainPtr<CPDF_Dictionary> params_dict =
+        stream_dict->GetMutableDictFor("Params");
     params_dict->SetNewFor<CPDF_Number>("Size", 6);
     EXPECT_EQ(6, file_spec.GetParamsDict()->GetIntegerFor("Size"));
   }
diff --git a/core/fpdfdoc/cpdf_formcontrol.cpp b/core/fpdfdoc/cpdf_formcontrol.cpp
index 53f01d7..a2ce749 100644
--- a/core/fpdfdoc/cpdf_formcontrol.cpp
+++ b/core/fpdfdoc/cpdf_formcontrol.cpp
@@ -54,11 +54,11 @@
 ByteString CPDF_FormControl::GetOnStateName() const {
   DCHECK(GetType() == CPDF_FormField::kCheckBox ||
          GetType() == CPDF_FormField::kRadioButton);
-  CPDF_Dictionary* pAP = m_pWidgetDict->GetDictFor("AP");
+  const CPDF_Dictionary* pAP = m_pWidgetDict->GetDictFor("AP");
   if (!pAP)
     return ByteString();
 
-  CPDF_Dictionary* pN = pAP->GetDictFor("N");
+  const CPDF_Dictionary* pN = pAP->GetDictFor("N");
   if (!pN)
     return ByteString();
 
@@ -138,7 +138,7 @@
 }
 
 CPDF_ApSettings CPDF_FormControl::GetMK() const {
-  return CPDF_ApSettings(m_pWidgetDict->GetDictFor("MK"));
+  return CPDF_ApSettings(m_pWidgetDict->GetMutableDictFor("MK").Get());
 }
 
 bool CPDF_FormControl::HasMKEntry(const ByteString& csEntry) const {
@@ -206,8 +206,8 @@
 
   CPDF_Object* pObj = CPDF_FormField::GetFieldAttr(m_pWidgetDict.Get(), "DR");
   if (CPDF_Dictionary* pDict = ToDictionary(pObj)) {
-    CPDF_Dictionary* pFonts = pDict->GetDictFor("Font");
-    if (ValidateFontResourceDict(pFonts)) {
+    RetainPtr<CPDF_Dictionary> pFonts = pDict->GetMutableDictFor("Font");
+    if (ValidateFontResourceDict(pFonts.Get())) {
       RetainPtr<CPDF_Dictionary> pElement =
           pFonts->GetMutableDictFor(csFontNameTag.value());
       if (pElement) {
@@ -222,14 +222,14 @@
   if (pFormFont)
     return pFormFont;
 
-  CPDF_Dictionary* pPageDict = m_pWidgetDict->GetDictFor("P");
+  RetainPtr<CPDF_Dictionary> pPageDict = m_pWidgetDict->GetMutableDictFor("P");
   CPDF_Dictionary* pDict =
-      ToDictionary(CPDF_FormField::GetFieldAttr(pPageDict, "Resources"));
+      ToDictionary(CPDF_FormField::GetFieldAttr(pPageDict.Get(), "Resources"));
   if (!pDict)
     return nullptr;
 
-  CPDF_Dictionary* pFonts = pDict->GetDictFor("Font");
-  if (!ValidateFontResourceDict(pFonts))
+  RetainPtr<CPDF_Dictionary> pFonts = pDict->GetMutableDictFor("Font");
+  if (!ValidateFontResourceDict(pFonts.Get()))
     return nullptr;
 
   RetainPtr<CPDF_Dictionary> pElement =
diff --git a/core/fpdfdoc/cpdf_formfield.cpp b/core/fpdfdoc/cpdf_formfield.cpp
index 1395c4b..d76cf89 100644
--- a/core/fpdfdoc/cpdf_formfield.cpp
+++ b/core/fpdfdoc/cpdf_formfield.cpp
@@ -852,12 +852,12 @@
   if (DA.IsEmpty())
     return;
 
-  CPDF_Dictionary* pDR = pFormDict->GetDictFor("DR");
+  RetainPtr<CPDF_Dictionary> pDR = pFormDict->GetMutableDictFor("DR");
   if (!pDR)
     return;
 
-  CPDF_Dictionary* pFont = pDR->GetDictFor("Font");
-  if (!ValidateFontResourceDict(pFont))
+  RetainPtr<CPDF_Dictionary> pFont = pDR->GetMutableDictFor("Font");
+  if (!ValidateFontResourceDict(pFont.Get()))
     return;
 
   CPDF_DefaultAppearance appearance(DA);
diff --git a/core/fpdfdoc/cpdf_generateap.cpp b/core/fpdfdoc/cpdf_generateap.cpp
index 6ec9471..5108df6 100644
--- a/core/fpdfdoc/cpdf_generateap.cpp
+++ b/core/fpdfdoc/cpdf_generateap.cpp
@@ -308,7 +308,7 @@
   return ByteString(sAppStream);
 }
 
-ByteString GetColorStringWithDefault(CPDF_Array* pColor,
+ByteString GetColorStringWithDefault(const CPDF_Array* pColor,
                                      const CFX_Color& crDefaultColor,
                                      PaintOperation nOperation) {
   if (pColor) {
@@ -535,7 +535,7 @@
   ByteString sExtGSDictName = "GS";
   sAppStream << "/" << sExtGSDictName << " gs ";
 
-  CPDF_Array* pInteriorColor = pAnnotDict->GetArrayFor("IC");
+  const CPDF_Array* pInteriorColor = pAnnotDict->GetArrayFor("IC");
   sAppStream << GetColorStringWithDefault(
       pInteriorColor, CFX_Color(CFX_Color::Type::kTransparent),
       PaintOperation::kFill);
@@ -612,7 +612,7 @@
       pAnnotDict->GetArrayFor(pdfium::annotation::kC),
       CFX_Color(CFX_Color::Type::kRGB, 1, 1, 0), PaintOperation::kFill);
 
-  CPDF_Array* pArray = pAnnotDict->GetArrayFor("QuadPoints");
+  const CPDF_Array* pArray = pAnnotDict->GetArrayFor("QuadPoints");
   if (pArray) {
     size_t nQuadPointCount = CPDF_Annot::QuadPointCount(pArray);
     for (size_t i = 0; i < nQuadPointCount; ++i) {
@@ -642,7 +642,7 @@
   if (!bIsStroke)
     return false;
 
-  CPDF_Array* pInkList = pAnnotDict->GetArrayFor("InkList");
+  const CPDF_Array* pInkList = pAnnotDict->GetArrayFor("InkList");
   if (!pInkList || pInkList->IsEmpty())
     return false;
 
@@ -719,7 +719,7 @@
       pAnnotDict->GetArrayFor(pdfium::annotation::kC),
       CFX_Color(CFX_Color::Type::kRGB, 0, 0, 0), PaintOperation::kStroke);
 
-  CPDF_Array* pArray = pAnnotDict->GetArrayFor("QuadPoints");
+  const CPDF_Array* pArray = pAnnotDict->GetArrayFor("QuadPoints");
   if (pArray) {
     static constexpr float kLineWidth = 1.0f;
     sAppStream << kLineWidth << " w ";
@@ -786,7 +786,7 @@
   ByteString sExtGSDictName = "GS";
   sAppStream << "/" << sExtGSDictName << " gs ";
 
-  CPDF_Array* pInteriorColor = pAnnotDict->GetArrayFor("IC");
+  const CPDF_Array* pInteriorColor = pAnnotDict->GetArrayFor("IC");
   sAppStream << GetColorStringWithDefault(
       pInteriorColor, CFX_Color(CFX_Color::Type::kTransparent),
       PaintOperation::kFill);
@@ -837,7 +837,7 @@
       pAnnotDict->GetArrayFor(pdfium::annotation::kC),
       CFX_Color(CFX_Color::Type::kRGB, 0, 0, 0), PaintOperation::kStroke);
 
-  CPDF_Array* pArray = pAnnotDict->GetArrayFor("QuadPoints");
+  const CPDF_Array* pArray = pAnnotDict->GetArrayFor("QuadPoints");
   if (pArray) {
     static constexpr float kLineWidth = 1.0f;
     static constexpr float kDelta = 2.0f;
@@ -887,7 +887,7 @@
       pAnnotDict->GetArrayFor(pdfium::annotation::kC),
       CFX_Color(CFX_Color::Type::kRGB, 0, 0, 0), PaintOperation::kStroke);
 
-  CPDF_Array* pArray = pAnnotDict->GetArrayFor("QuadPoints");
+  const CPDF_Array* pArray = pAnnotDict->GetArrayFor("QuadPoints");
   if (pArray) {
     static constexpr float kLineWidth = 1.0f;
     size_t nQuadPointCount = CPDF_Annot::QuadPointCount(pArray);
@@ -920,7 +920,8 @@
   if (!pRootDict)
     return;
 
-  CPDF_Dictionary* pFormDict = pRootDict->GetDictFor("AcroForm");
+  RetainPtr<CPDF_Dictionary> pFormDict =
+      pRootDict->GetMutableDictFor("AcroForm");
   if (!pFormDict)
     return;
 
@@ -942,12 +943,12 @@
   ByteString font_name = font.value();
 
   CFX_Color crText = fpdfdoc::CFXColorFromString(DA);
-  CPDF_Dictionary* pDRDict = pFormDict->GetDictFor("DR");
+  RetainPtr<CPDF_Dictionary> pDRDict = pFormDict->GetMutableDictFor("DR");
   if (!pDRDict)
     return;
 
-  CPDF_Dictionary* pDRFontDict = pDRDict->GetDictFor("Font");
-  if (!ValidateFontResourceDict(pDRFontDict))
+  RetainPtr<CPDF_Dictionary> pDRFontDict = pDRDict->GetMutableDictFor("Font");
+  if (!ValidateFontResourceDict(pDRFontDict.Get()))
     return;
 
   RetainPtr<CPDF_Dictionary> pFontDict =
@@ -968,7 +969,7 @@
     return;
 
   CFX_FloatRect rcAnnot = pAnnotDict->GetRectFor(pdfium::annotation::kRect);
-  CPDF_Dictionary* pMKDict = pAnnotDict->GetDictFor("MK");
+  const CPDF_Dictionary* pMKDict = pAnnotDict->GetDictFor("MK");
   int32_t nRotate =
       pMKDict ? pMKDict->GetIntegerFor(pdfium::appearance::kR) : 0;
 
@@ -1002,11 +1003,11 @@
   CPVT_Dash dsBorder(3, 0, 0);
   CFX_Color crLeftTop;
   CFX_Color crRightBottom;
-  if (CPDF_Dictionary* pBSDict = pAnnotDict->GetDictFor("BS")) {
+  if (const CPDF_Dictionary* pBSDict = pAnnotDict->GetDictFor("BS")) {
     if (pBSDict->KeyExist("W"))
       fBorderWidth = pBSDict->GetNumberFor("W");
 
-    if (CPDF_Array* pArray = pBSDict->GetArrayFor("D")) {
+    if (const CPDF_Array* pArray = pBSDict->GetArrayFor("D")) {
       dsBorder = CPVT_Dash(pArray->GetIntegerAt(0), pArray->GetIntegerAt(1),
                            pArray->GetIntegerAt(2));
     }
@@ -1039,7 +1040,7 @@
   CFX_Color crBorder;
   CFX_Color crBG;
   if (pMKDict) {
-    CPDF_Array* pArray = pMKDict->GetArrayFor(pdfium::appearance::kBC);
+    const CPDF_Array* pArray = pMKDict->GetArrayFor(pdfium::appearance::kBC);
     if (pArray)
       crBorder = fpdfdoc::CFXColorFromArray(*pArray);
     pArray = pMKDict->GetArrayFor(pdfium::appearance::kBG);
@@ -1074,11 +1075,13 @@
   }
   CPDF_Dictionary* pStreamDict = pNormalStream->GetDict();
   if (pStreamDict) {
-    CPDF_Dictionary* pStreamResList = pStreamDict->GetDictFor("Resources");
+    RetainPtr<CPDF_Dictionary> pStreamResList =
+        pStreamDict->GetMutableDictFor("Resources");
     if (pStreamResList) {
-      CPDF_Dictionary* pStreamResFontList = pStreamResList->GetDictFor("Font");
+      RetainPtr<CPDF_Dictionary> pStreamResFontList =
+          pStreamResList->GetMutableDictFor("Font");
       if (pStreamResFontList) {
-        if (!ValidateFontResourceDict(pStreamResFontList))
+        if (!ValidateFontResourceDict(pStreamResFontList.Get()))
           return;
       } else {
         pStreamResFontList = pStreamResList->SetNewFor<CPDF_Dictionary>("Font");
@@ -1106,9 +1109,11 @@
       const CPDF_Object* pMaxLen =
           CPDF_FormField::GetFieldAttr(pAnnotDict, "MaxLen");
       uint32_t dwMaxLen = pMaxLen ? pMaxLen->GetInteger() : 0;
-      CPVT_FontMap map(
-          pDoc, pStreamDict ? pStreamDict->GetDictFor("Resources") : nullptr,
-          pDefFont, font_name);
+      CPVT_FontMap map(pDoc,
+                       pStreamDict
+                           ? pStreamDict->GetMutableDictFor("Resources").Get()
+                           : nullptr,
+                       pDefFont, font_name);
       CPVT_VariableText::Provider prd(&map);
       CPVT_VariableText vt;
       vt.SetProvider(&prd);
@@ -1166,9 +1171,11 @@
       const CPDF_Object* pV =
           CPDF_FormField::GetFieldAttr(pAnnotDict, pdfium::form_fields::kV);
       WideString swValue = pV ? pV->GetUnicodeText() : WideString();
-      CPVT_FontMap map(
-          pDoc, pStreamDict ? pStreamDict->GetDictFor("Resources") : nullptr,
-          pDefFont, font_name);
+      CPVT_FontMap map(pDoc,
+                       pStreamDict
+                           ? pStreamDict->GetMutableDictFor("Resources").Get()
+                           : nullptr,
+                       pDefFont, font_name);
       CPVT_VariableText::Provider prd(&map);
       CPVT_VariableText vt;
       vt.SetProvider(&prd);
@@ -1235,9 +1242,11 @@
       break;
     }
     case CPDF_GenerateAP::kListBox: {
-      CPVT_FontMap map(
-          pDoc, pStreamDict ? pStreamDict->GetDictFor("Resources") : nullptr,
-          pDefFont, font_name);
+      CPVT_FontMap map(pDoc,
+                       pStreamDict
+                           ? pStreamDict->GetMutableDictFor("Resources").Get()
+                           : nullptr,
+                       pDefFont, font_name);
       CPVT_VariableText::Provider prd(&map);
       CPDF_Array* pOpts =
           ToArray(CPDF_FormField::GetFieldAttr(pAnnotDict, "Opt"));
@@ -1329,15 +1338,17 @@
 
   pStreamDict->SetMatrixFor("Matrix", matrix);
   pStreamDict->SetRectFor("BBox", rcBBox);
-  CPDF_Dictionary* pStreamResList = pStreamDict->GetDictFor("Resources");
+  RetainPtr<CPDF_Dictionary> pStreamResList =
+      pStreamDict->GetMutableDictFor("Resources");
   if (!pStreamResList) {
     pStreamDict->SetFor("Resources", pFormDict->GetDictFor("DR")->Clone());
     return;
   }
 
-  CPDF_Dictionary* pStreamResFontList = pStreamResList->GetDictFor("Font");
+  RetainPtr<CPDF_Dictionary> pStreamResFontList =
+      pStreamResList->GetMutableDictFor("Font");
   if (pStreamResFontList) {
-    if (!ValidateFontResourceDict(pStreamResFontList))
+    if (!ValidateFontResourceDict(pStreamResFontList.Get()))
       return;
   } else {
     pStreamResFontList = pStreamResList->SetNewFor<CPDF_Dictionary>("Font");
diff --git a/core/fpdfdoc/cpdf_interactiveform.cpp b/core/fpdfdoc/cpdf_interactiveform.cpp
index 99f4a4c..7e636fa 100644
--- a/core/fpdfdoc/cpdf_interactiveform.cpp
+++ b/core/fpdfdoc/cpdf_interactiveform.cpp
@@ -171,14 +171,14 @@
   return nullptr;
 }
 
-bool FindFont(CPDF_Dictionary* pFormDict,
+bool FindFont(const CPDF_Dictionary* pFormDict,
               const CPDF_Font* pFont,
               ByteString* csNameTag) {
-  CPDF_Dictionary* pDR = pFormDict->GetDictFor("DR");
+  const CPDF_Dictionary* pDR = pFormDict->GetDictFor("DR");
   if (!pDR)
     return false;
 
-  CPDF_Dictionary* pFonts = pDR->GetDictFor("Font");
+  const CPDF_Dictionary* pFonts = pDR->GetDictFor("Font");
   if (!ValidateFontResourceDict(pFonts))
     return false;
 
@@ -196,7 +196,7 @@
   return false;
 }
 
-bool FindFontFromDoc(CPDF_Dictionary* pFormDict,
+bool FindFontFromDoc(const CPDF_Dictionary* pFormDict,
                      CPDF_Document* pDocument,
                      ByteString csFontName,
                      RetainPtr<CPDF_Font>& pFont,
@@ -204,11 +204,11 @@
   if (csFontName.IsEmpty())
     return false;
 
-  CPDF_Dictionary* pDR = pFormDict->GetDictFor("DR");
+  const CPDF_Dictionary* pDR = pFormDict->GetDictFor("DR");
   if (!pDR)
     return false;
 
-  CPDF_Dictionary* pFonts = pDR->GetDictFor("Font");
+  const CPDF_Dictionary* pFonts = pDR->GetDictFor("Font");
   if (!ValidateFontResourceDict(pFonts))
     return false;
 
@@ -293,15 +293,15 @@
   return pFormDict;
 }
 
-RetainPtr<CPDF_Font> GetNativeFont(CPDF_Dictionary* pFormDict,
+RetainPtr<CPDF_Font> GetNativeFont(const CPDF_Dictionary* pFormDict,
                                    CPDF_Document* pDocument,
                                    FX_Charset charSet,
                                    ByteString* csNameTag) {
-  CPDF_Dictionary* pDR = pFormDict->GetDictFor("DR");
+  const CPDF_Dictionary* pDR = pFormDict->GetDictFor("DR");
   if (!pDR)
     return nullptr;
 
-  CPDF_Dictionary* pFonts = pDR->GetDictFor("Font");
+  const CPDF_Dictionary* pFonts = pDR->GetDictFor("Font");
   if (!ValidateFontResourceDict(pFonts))
     return nullptr;
 
@@ -532,11 +532,11 @@
   if (!pRoot)
     return;
 
-  m_pFormDict.Reset(pRoot->GetDictFor("AcroForm"));
+  m_pFormDict = pRoot->GetMutableDictFor("AcroForm");
   if (!m_pFormDict)
     return;
 
-  CPDF_Array* pFields = m_pFormDict->GetArrayFor("Fields");
+  RetainPtr<CPDF_Array> pFields = m_pFormDict->GetMutableArrayFor("Fields");
   if (!pFields)
     return;
 
@@ -565,27 +565,28 @@
   DCHECK(pDocument);
   DCHECK(csNameTag);
 
-  CPDF_Dictionary* pFormDict = pDocument->GetRoot()->GetDictFor("AcroForm");
+  RetainPtr<CPDF_Dictionary> pFormDict =
+      pDocument->GetRoot()->GetMutableDictFor("AcroForm");
   if (!pFormDict)
     pFormDict = InitDict(pDocument);
 
   FX_Charset charSet = GetNativeCharSet();
   ByteString csTemp;
   RetainPtr<CPDF_Font> pFont =
-      GetNativeFont(pFormDict, pDocument, charSet, &csTemp);
+      GetNativeFont(pFormDict.Get(), pDocument, charSet, &csTemp);
   if (pFont) {
     *csNameTag = std::move(csTemp);
     return pFont;
   }
   ByteString csFontName = GetNativeFontName(charSet, nullptr);
-  if (FindFontFromDoc(pFormDict, pDocument, csFontName, pFont, csNameTag))
+  if (FindFontFromDoc(pFormDict.Get(), pDocument, csFontName, pFont, csNameTag))
     return pFont;
 
   pFont = AddNativeFont(charSet, pDocument);
   if (!pFont)
     return nullptr;
 
-  AddFont(pFormDict, pDocument, pFont, csNameTag);
+  AddFont(pFormDict.Get(), pDocument, pFont, csNameTag);
   return pFont;
 }
 
@@ -659,7 +660,7 @@
   if (!m_pFormDict)
     return 0;
 
-  CPDF_Array* pArray = m_pFormDict->GetArrayFor("CO");
+  const CPDF_Array* pArray = m_pFormDict->GetArrayFor("CO");
   return pArray ? fxcrt::CollectionSize<int>(*pArray) : 0;
 }
 
@@ -667,7 +668,7 @@
   if (!m_pFormDict || index < 0)
     return nullptr;
 
-  CPDF_Array* pArray = m_pFormDict->GetArrayFor("CO");
+  const CPDF_Array* pArray = m_pFormDict->GetArrayFor("CO");
   if (!pArray)
     return nullptr;
 
@@ -681,7 +682,7 @@
   if (!m_pFormDict)
     return -1;
 
-  CPDF_Array* pArray = m_pFormDict->GetArrayFor("CO");
+  const CPDF_Array* pArray = m_pFormDict->GetArrayFor("CO");
   if (!pArray)
     return -1;
 
@@ -698,12 +699,12 @@
   if (!m_pFormDict || csAlias.IsEmpty())
     return nullptr;
 
-  CPDF_Dictionary* pDR = m_pFormDict->GetDictFor("DR");
+  RetainPtr<CPDF_Dictionary> pDR = m_pFormDict->GetMutableDictFor("DR");
   if (!pDR)
     return nullptr;
 
-  CPDF_Dictionary* pFonts = pDR->GetDictFor("Font");
-  if (!ValidateFontResourceDict(pFonts))
+  RetainPtr<CPDF_Dictionary> pFonts = pDR->GetMutableDictFor("Font");
+  if (!ValidateFontResourceDict(pFonts.Get()))
     return nullptr;
 
   RetainPtr<CPDF_Dictionary> pElement = pFonts->GetMutableDictFor(csAlias);
@@ -756,7 +757,8 @@
     return;
 
   uint32_t dwParentObjNum = pFieldDict->GetObjNum();
-  CPDF_Array* pKids = pFieldDict->GetArrayFor(pdfium::form_fields::kKids);
+  RetainPtr<CPDF_Array> pKids =
+      pFieldDict->GetMutableArrayFor(pdfium::form_fields::kKids);
   if (!pKids) {
     AddTerminalField(pFieldDict);
     return;
@@ -781,7 +783,8 @@
 }
 
 void CPDF_InteractiveForm::FixPageFields(CPDF_Page* pPage) {
-  CPDF_Array* pAnnots = pPage->GetDict()->GetArrayFor("Annots");
+  RetainPtr<CPDF_Array> pAnnots =
+      pPage->GetDict()->GetMutableArrayFor("Annots");
   if (!pAnnots)
     return;
 
@@ -795,7 +798,7 @@
 void CPDF_InteractiveForm::AddTerminalField(CPDF_Dictionary* pFieldDict) {
   if (!pFieldDict->KeyExist(pdfium::form_fields::kFT)) {
     // Key "FT" is required for terminal fields, it is also inheritable.
-    CPDF_Dictionary* pParentDict =
+    const CPDF_Dictionary* pParentDict =
         pFieldDict->GetDictFor(pdfium::form_fields::kParent);
     if (!pParentDict || !pParentDict->KeyExist(pdfium::form_fields::kFT))
       return;
@@ -809,10 +812,10 @@
   CPDF_FormField* pField = nullptr;
   pField = m_pFieldTree->GetField(csWName);
   if (!pField) {
-    CPDF_Dictionary* pParent = pFieldDict;
+    RetainPtr<CPDF_Dictionary> pParent(pFieldDict);
     if (!pFieldDict->KeyExist(pdfium::form_fields::kT) &&
         pFieldDict->GetNameFor("Subtype") == "Widget") {
-      pParent = pFieldDict->GetDictFor(pdfium::form_fields::kParent);
+      pParent = pFieldDict->GetMutableDictFor(pdfium::form_fields::kParent);
       if (!pParent)
         pParent = pFieldDict;
     }
@@ -834,7 +837,7 @@
       }
     }
 
-    auto newField = std::make_unique<CPDF_FormField>(this, pParent);
+    auto newField = std::make_unique<CPDF_FormField>(this, pParent.Get());
     pField = newField.get();
     CPDF_Object* pTObj = pDict->GetObjectFor(pdfium::form_fields::kT);
     if (ToReference(pTObj)) {
@@ -848,7 +851,8 @@
       return;
   }
 
-  CPDF_Array* pKids = pFieldDict->GetArrayFor(pdfium::form_fields::kKids);
+  RetainPtr<CPDF_Array> pKids =
+      pFieldDict->GetMutableArrayFor(pdfium::form_fields::kKids);
   if (pKids) {
     for (size_t i = 0; i < pKids->size(); i++) {
       RetainPtr<CPDF_Dictionary> pKid = pKids->GetMutableDictAt(i);
@@ -927,7 +931,8 @@
   if (!pDoc)
     return nullptr;
 
-  CPDF_Dictionary* pMainDict = pDoc->GetRoot()->GetDictFor("FDF");
+  RetainPtr<CPDF_Dictionary> pMainDict =
+      pDoc->GetRoot()->GetMutableDictFor("FDF");
   if (!pdf_path.IsEmpty()) {
     auto pNewDict = pDoc->New<CPDF_Dictionary>();
     pNewDict->SetNewFor<CPDF_Name>("Type", "Filespec");
diff --git a/core/fpdfdoc/cpdf_linklist.cpp b/core/fpdfdoc/cpdf_linklist.cpp
index 568e363..2bccd7f 100644
--- a/core/fpdfdoc/cpdf_linklist.cpp
+++ b/core/fpdfdoc/cpdf_linklist.cpp
@@ -52,7 +52,8 @@
 
   // std::map::operator[] forces the creation of a map entry.
   auto* page_link_list = &m_PageMap[objnum];
-  CPDF_Array* pAnnotList = pPage->GetDict()->GetArrayFor("Annots");
+  RetainPtr<CPDF_Array> pAnnotList =
+      pPage->GetDict()->GetMutableArrayFor("Annots");
   if (!pAnnotList)
     return page_link_list;
 
diff --git a/core/fpdfdoc/cpdf_nametree.cpp b/core/fpdfdoc/cpdf_nametree.cpp
index f95e15c..37d7fee 100644
--- a/core/fpdfdoc/cpdf_nametree.cpp
+++ b/core/fpdfdoc/cpdf_nametree.cpp
@@ -52,11 +52,11 @@
     return false;
 
   if (pNode->GetArrayFor("Names") == pFind) {
-    pLimits->push_back(pNode->GetArrayFor("Limits"));
+    pLimits->push_back(pNode->GetMutableArrayFor("Limits").Get());
     return true;
   }
 
-  CPDF_Array* pKids = pNode->GetArrayFor("Kids");
+  RetainPtr<CPDF_Array> pKids = pNode->GetMutableArrayFor("Kids");
   if (!pKids)
     return false;
 
@@ -66,7 +66,7 @@
       continue;
 
     if (GetNodeAncestorsLimitsInternal(pKid, pFind, nLevel + 1, pLimits)) {
-      pLimits->push_back(pNode->GetArrayFor("Limits"));
+      pLimits->push_back(pNode->GetMutableArrayFor("Limits").Get());
       return true;
     }
   }
@@ -93,13 +93,13 @@
   if (nLevel > kNameTreeMaxRecursion)
     return false;
 
-  CPDF_Array* pLimits = pNode->GetArrayFor("Limits");
+  RetainPtr<CPDF_Array> pLimits = pNode->GetMutableArrayFor("Limits");
   WideString csLeft;
   WideString csRight;
   if (pLimits)
-    std::tie(csLeft, csRight) = GetNodeLimitsAndSanitize(pLimits);
+    std::tie(csLeft, csRight) = GetNodeLimitsAndSanitize(pLimits.Get());
 
-  CPDF_Array* pNames = pNode->GetArrayFor("Names");
+  const CPDF_Array* pNames = pNode->GetArrayFor("Names");
   if (pNames) {
     if (pNames != pFind)
       return false;
@@ -124,7 +124,7 @@
     return true;
   }
 
-  CPDF_Array* pKids = pNode->GetArrayFor("Kids");
+  RetainPtr<CPDF_Array> pKids = pNode->GetMutableArrayFor("Kids");
   if (!pKids)
     return false;
 
@@ -260,7 +260,7 @@
   WideString key;
   RetainPtr<CPDF_Object> value;
   // The leaf node that holds `key` and `value`.
-  CPDF_Array* container;
+  RetainPtr<CPDF_Array> container;
   // The index for `key` in `container`. Must be even.
   size_t index;
 };
@@ -276,7 +276,7 @@
   if (nLevel > kNameTreeMaxRecursion)
     return absl::nullopt;
 
-  CPDF_Array* pNames = pNode->GetArrayFor("Names");
+  RetainPtr<CPDF_Array> pNames = pNode->GetMutableArrayFor("Names");
   if (pNames) {
     size_t nCount = pNames->size() / 2;
     if (nTargetPairIndex >= *nCurPairIndex + nCount) {
@@ -292,12 +292,12 @@
     IndexSearchResult result;
     result.key = pNames->GetUnicodeTextAt(index);
     result.value = std::move(value);
-    result.container = pNames;
+    result.container = pNames.Get();  // TODO(tsepez): retain result.
     result.index = index;
     return result;
   }
 
-  CPDF_Array* pKids = pNode->GetArrayFor("Kids");
+  RetainPtr<CPDF_Array> pKids = pNode->GetMutableArrayFor("Kids");
   if (!pKids)
     return absl::nullopt;
 
@@ -361,13 +361,14 @@
     return array;
   CPDF_Dictionary* dict = obj->AsDictionary();
   if (dict)
-    return dict->GetArrayFor("D");
+    return dict->GetMutableArrayFor("D").Get();  // TODO(tsepez): retain.
   return nullptr;
 }
 
 CPDF_Array* LookupOldStyleNamedDest(CPDF_Document* pDoc,
                                     const ByteString& name) {
-  CPDF_Dictionary* pDests = pDoc->GetRoot()->GetDictFor("Dests");
+  RetainPtr<CPDF_Dictionary> pDests =
+      pDoc->GetRoot()->GetMutableDictFor("Dests");
   if (!pDests)
     return nullptr;
   return GetNamedDestFromObject(pDests->GetDirectObjectFor(name));
@@ -375,7 +376,8 @@
 
 }  // namespace
 
-CPDF_NameTree::CPDF_NameTree(CPDF_Dictionary* pRoot) : m_pRoot(pRoot) {
+CPDF_NameTree::CPDF_NameTree(RetainPtr<CPDF_Dictionary> pRoot)
+    : m_pRoot(std::move(pRoot)) {
   DCHECK(m_pRoot);
 }
 
@@ -389,15 +391,16 @@
   if (!pRoot)
     return nullptr;
 
-  CPDF_Dictionary* pNames = pRoot->GetDictFor("Names");
+  RetainPtr<CPDF_Dictionary> pNames = pRoot->GetMutableDictFor("Names");
   if (!pNames)
     return nullptr;
 
-  CPDF_Dictionary* pCategory = pNames->GetDictFor(category);
+  RetainPtr<CPDF_Dictionary> pCategory = pNames->GetMutableDictFor(category);
   if (!pCategory)
     return nullptr;
 
-  return pdfium::WrapUnique(new CPDF_NameTree(pCategory));  // Private ctor.
+  return pdfium::WrapUnique(
+      new CPDF_NameTree(std::move(pCategory)));  // Private ctor.
 }
 
 // static
@@ -409,14 +412,14 @@
     return nullptr;
 
   // Retrieve the document's Names dictionary; create it if missing.
-  CPDF_Dictionary* pNames = pRoot->GetDictFor("Names");
+  RetainPtr<CPDF_Dictionary> pNames = pRoot->GetMutableDictFor("Names");
   if (!pNames) {
     pNames = pDoc->NewIndirect<CPDF_Dictionary>();
     pRoot->SetNewFor<CPDF_Reference>("Names", pDoc, pNames->GetObjNum());
   }
 
   // Create the |category| dictionary if missing.
-  CPDF_Dictionary* pCategory = pNames->GetDictFor(category);
+  RetainPtr<CPDF_Dictionary> pCategory = pNames->GetMutableDictFor(category);
   if (!pCategory) {
     pCategory = pDoc->NewIndirect<CPDF_Dictionary>();
     pCategory->SetNewFor<CPDF_Array>("Names");
@@ -429,7 +432,8 @@
 // static
 std::unique_ptr<CPDF_NameTree> CPDF_NameTree::CreateForTesting(
     CPDF_Dictionary* pRoot) {
-  return pdfium::WrapUnique(new CPDF_NameTree(pRoot));  // Private ctor.
+  return pdfium::WrapUnique(
+      new CPDF_NameTree(pdfium::WrapRetain(pRoot)));  // Private ctor.
 }
 
 // static
@@ -514,12 +518,13 @@
   }
 
   // Remove the name and the object from the leaf array |pFind|.
-  CPDF_Array* pFind = result.value().container;
+  RetainPtr<CPDF_Array> pFind = result.value().container;
   pFind->RemoveAt(result.value().index + 1);
   pFind->RemoveAt(result.value().index);
 
   // Delete empty nodes and update the limits of |pFind|'s ancestors as needed.
-  UpdateNodesAndLimitsUponDeletion(m_pRoot.Get(), pFind, result.value().key, 0);
+  UpdateNodesAndLimitsUponDeletion(m_pRoot.Get(), pFind.Get(),
+                                   result.value().key, 0);
   return true;
 }
 
diff --git a/core/fpdfdoc/cpdf_nametree.h b/core/fpdfdoc/cpdf_nametree.h
index 140cadb..144fd9e 100644
--- a/core/fpdfdoc/cpdf_nametree.h
+++ b/core/fpdfdoc/cpdf_nametree.h
@@ -51,11 +51,11 @@
   CPDF_Dictionary* GetRootForTesting() const { return m_pRoot.Get(); }
 
  private:
-  explicit CPDF_NameTree(CPDF_Dictionary* pRoot);
+  explicit CPDF_NameTree(RetainPtr<CPDF_Dictionary> pRoot);
 
   CPDF_Array* LookupNewStyleNamedDest(const ByteString& name);
 
-  const RetainPtr<CPDF_Dictionary> m_pRoot;
+  RetainPtr<CPDF_Dictionary> const m_pRoot;
 };
 
 #endif  // CORE_FPDFDOC_CPDF_NAMETREE_H_
diff --git a/core/fpdfdoc/cpdf_nametree_unittest.cpp b/core/fpdfdoc/cpdf_nametree_unittest.cpp
index 9add6d3..2c34ac7 100644
--- a/core/fpdfdoc/cpdf_nametree_unittest.cpp
+++ b/core/fpdfdoc/cpdf_nametree_unittest.cpp
@@ -131,9 +131,9 @@
   auto pRootDict = pdfium::MakeRetain<CPDF_Dictionary>();
   FillNameTreeDict(pRootDict.Get());
   RetainPtr<CPDF_Dictionary> pKid1 =
-      pRootDict->GetArrayFor("Kids")->GetMutableDictAt(0);
+      pRootDict->GetMutableArrayFor("Kids")->GetMutableDictAt(0);
   RetainPtr<CPDF_Dictionary> pGrandKid3 =
-      pKid1->GetArrayFor("Kids")->GetMutableDictAt(1);
+      pKid1->GetMutableArrayFor("Kids")->GetMutableDictAt(1);
   RetainPtr<CPDF_Array> pLimits = pGrandKid3->GetMutableArrayFor("Limits");
   ASSERT_EQ(2u, pLimits->size());
   pLimits->AppendNew<CPDF_Number>(5);
diff --git a/core/fpdfdoc/cpdf_viewerpreferences.cpp b/core/fpdfdoc/cpdf_viewerpreferences.cpp
index acba28d..fc60dac 100644
--- a/core/fpdfdoc/cpdf_viewerpreferences.cpp
+++ b/core/fpdfdoc/cpdf_viewerpreferences.cpp
@@ -6,6 +6,7 @@
 
 #include "core/fpdfdoc/cpdf_viewerpreferences.h"
 
+#include "core/fpdfapi/parser/cpdf_array.h"
 #include "core/fpdfapi/parser/cpdf_dictionary.h"
 #include "core/fpdfapi/parser/cpdf_document.h"
 #include "core/fpdfapi/parser/cpdf_name.h"
@@ -32,7 +33,7 @@
 
 CPDF_Array* CPDF_ViewerPreferences::PrintPageRange() const {
   CPDF_Dictionary* pDict = GetViewerPreferences();
-  return pDict ? pDict->GetArrayFor("PrintPageRange") : nullptr;
+  return pDict ? pDict->GetMutableArrayFor("PrintPageRange").Get() : nullptr;
 }
 
 ByteString CPDF_ViewerPreferences::Duplex() const {
@@ -55,5 +56,5 @@
 
 CPDF_Dictionary* CPDF_ViewerPreferences::GetViewerPreferences() const {
   CPDF_Dictionary* pDict = m_pDoc->GetRoot();
-  return pDict ? pDict->GetDictFor("ViewerPreferences") : nullptr;
+  return pDict ? pDict->GetMutableDictFor("ViewerPreferences").Get() : nullptr;
 }
diff --git a/core/fpdfdoc/cpvt_fontmap.cpp b/core/fpdfdoc/cpvt_fontmap.cpp
index 74240a0..e02d854 100644
--- a/core/fpdfdoc/cpvt_fontmap.cpp
+++ b/core/fpdfdoc/cpvt_fontmap.cpp
@@ -39,8 +39,8 @@
   if (!pPDFFont)
     return;
 
-  CPDF_Dictionary* pFontList = m_pResDict->GetDictFor("Font");
-  if (ValidateFontResourceDict(pFontList) &&
+  RetainPtr<CPDF_Dictionary> pFontList = m_pResDict->GetMutableDictFor("Font");
+  if (ValidateFontResourceDict(pFontList.Get()) &&
       !pFontList->KeyExist(m_sSysFontAlias)) {
     pFontList->SetNewFor<CPDF_Reference>(m_sSysFontAlias, m_pDocument.Get(),
                                          pPDFFont->GetFontDict()->GetObjNum());
diff --git a/fpdfsdk/cpdfsdk_appstream.cpp b/fpdfsdk/cpdfsdk_appstream.cpp
index b2fa82b..805394f 100644
--- a/fpdfsdk/cpdfsdk_appstream.cpp
+++ b/fpdfsdk/cpdfsdk_appstream.cpp
@@ -1877,7 +1877,8 @@
     pStreamDict->SetNewFor<CPDF_Number>("FormType", 1);
 
     if (pOrigStreamDict) {
-      CPDF_Dictionary* pResources = pOrigStreamDict->GetDictFor("Resources");
+      const CPDF_Dictionary* pResources =
+          pOrigStreamDict->GetDictFor("Resources");
       if (pResources)
         pStreamDict->SetFor("Resources", pResources->Clone());
     }
diff --git a/fpdfsdk/cpdfsdk_baannot.cpp b/fpdfsdk/cpdfsdk_baannot.cpp
index c660a08..b3d9779 100644
--- a/fpdfsdk/cpdfsdk_baannot.cpp
+++ b/fpdfsdk/cpdfsdk_baannot.cpp
@@ -117,8 +117,8 @@
 }
 
 void CPDFSDK_BAAnnot::SetBorderWidth(int nWidth) {
-  CPDF_Array* pBorder =
-      GetAnnotDict()->GetArrayFor(pdfium::annotation::kBorder);
+  RetainPtr<CPDF_Array> pBorder =
+      GetAnnotDict()->GetMutableArrayFor(pdfium::annotation::kBorder);
   if (pBorder) {
     pBorder->SetNewAt<CPDF_Number>(2, nWidth);
   } else {
@@ -128,12 +128,13 @@
 }
 
 int CPDFSDK_BAAnnot::GetBorderWidth() const {
-  if (const CPDF_Array* pBorder =
-          GetAnnotDict()->GetArrayFor(pdfium::annotation::kBorder)) {
+  const CPDF_Array* pBorder =
+      GetAnnotDict()->GetArrayFor(pdfium::annotation::kBorder);
+  if (pBorder)
     return pBorder->GetIntegerAt(2);
-  }
 
-  if (CPDF_Dictionary* pBSDict = GetAnnotDict()->GetDictFor("BS"))
+  const CPDF_Dictionary* pBSDict = GetAnnotDict()->GetDictFor("BS");
+  if (pBSDict)
     return pBSDict->GetIntegerFor("W", 1);
 
   return 1;
@@ -165,7 +166,7 @@
 }
 
 BorderStyle CPDFSDK_BAAnnot::GetBorderStyle() const {
-  CPDF_Dictionary* pBSDict = GetAnnotDict()->GetDictFor("BS");
+  const CPDF_Dictionary* pBSDict = GetAnnotDict()->GetDictFor("BS");
   if (pBSDict) {
     ByteString sBorderStyle = pBSDict->GetStringFor("S", "S");
     if (sBorderStyle == "S")
diff --git a/fpdfsdk/cpdfsdk_formfillenvironment.cpp b/fpdfsdk/cpdfsdk_formfillenvironment.cpp
index 3492b99..b064635 100644
--- a/fpdfsdk/cpdfsdk_formfillenvironment.cpp
+++ b/fpdfsdk/cpdfsdk_formfillenvironment.cpp
@@ -655,7 +655,7 @@
   if (!pRoot)
     return false;
 
-  CPDF_Object* pOpenAction = pRoot->GetDictFor("OpenAction");
+  const CPDF_Object* pOpenAction = pRoot->GetDictFor("OpenAction");
   if (!pOpenAction)
     pOpenAction = pRoot->GetArrayFor("OpenAction");
   if (!pOpenAction)
@@ -664,7 +664,7 @@
   if (pOpenAction->IsArray())
     return true;
 
-  CPDF_Dictionary* pDict = pOpenAction->AsDictionary();
+  const CPDF_Dictionary* pDict = pOpenAction->AsDictionary();
   if (!pDict)
     return false;
 
diff --git a/fpdfsdk/cpdfsdk_helpers.cpp b/fpdfsdk/cpdfsdk_helpers.cpp
index d7bd086..05e32ee 100644
--- a/fpdfsdk/cpdfsdk_helpers.cpp
+++ b/fpdfsdk/cpdfsdk_helpers.cpp
@@ -225,8 +225,10 @@
   return dict->GetArrayFor("QuadPoints");
 }
 
-CPDF_Array* GetQuadPointsArrayFromDictionary(CPDF_Dictionary* dict) {
-  return dict->GetArrayFor("QuadPoints");
+RetainPtr<CPDF_Array> GetMutableQuadPointsArrayFromDictionary(
+    CPDF_Dictionary* dict) {
+  return pdfium::WrapRetain(
+      const_cast<CPDF_Array*>(GetQuadPointsArrayFromDictionary(dict)));
 }
 
 CPDF_Array* AddQuadPointsArrayToDictionary(CPDF_Dictionary* dict) {
diff --git a/fpdfsdk/cpdfsdk_helpers.h b/fpdfsdk/cpdfsdk_helpers.h
index b585ad1..c339b80 100644
--- a/fpdfsdk/cpdfsdk_helpers.h
+++ b/fpdfsdk/cpdfsdk_helpers.h
@@ -249,7 +249,8 @@
 #endif  // PDF_ENABLE_XFA
 
 const CPDF_Array* GetQuadPointsArrayFromDictionary(const CPDF_Dictionary* dict);
-CPDF_Array* GetQuadPointsArrayFromDictionary(CPDF_Dictionary* dict);
+RetainPtr<CPDF_Array> GetMutableQuadPointsArrayFromDictionary(
+    CPDF_Dictionary* dict);
 CPDF_Array* AddQuadPointsArrayToDictionary(CPDF_Dictionary* dict);
 bool IsValidQuadPointsIndex(const CPDF_Array* array, size_t index);
 bool GetQuadPointsAtIndex(const CPDF_Array* array,
diff --git a/fpdfsdk/cpdfsdk_interactiveform.cpp b/fpdfsdk/cpdfsdk_interactiveform.cpp
index 1bddf19..768bbfc 100644
--- a/fpdfsdk/cpdfsdk_interactiveform.cpp
+++ b/fpdfsdk/cpdfsdk_interactiveform.cpp
@@ -75,11 +75,11 @@
   if (!pFDF)
     return true;
 
-  CPDF_Dictionary* pMainDict = pFDF->GetRoot()->GetDictFor("FDF");
+  const CPDF_Dictionary* pMainDict = pFDF->GetRoot()->GetDictFor("FDF");
   if (!pMainDict)
     return false;
 
-  CPDF_Array* pFields = pMainDict->GetArrayFor("Fields");
+  const CPDF_Array* pFields = pMainDict->GetArrayFor("Fields");
   if (!pFields)
     return false;
 
@@ -137,7 +137,7 @@
   CPDF_Document* pDocument = m_pFormFillEnv->GetPDFDocument();
   CPDFSDK_PageView* pPage = nullptr;
 
-  if (CPDF_Dictionary* pPageDict = pControlDict->GetDictFor("P")) {
+  if (const CPDF_Dictionary* pPageDict = pControlDict->GetDictFor("P")) {
     int nPageIndex = pDocument->GetPageIndex(pPageDict->GetObjNum());
     if (nPageIndex >= 0)
       pPage = m_pFormFillEnv->GetPageViewAtIndex(nPageIndex);
@@ -185,7 +185,7 @@
     if (!pPageDict)
       continue;
 
-    CPDF_Array* pAnnots = pPageDict->GetArrayFor("Annots");
+    const CPDF_Array* pAnnots = pPageDict->GetArrayFor("Annots");
     if (!pAnnots)
       continue;
 
diff --git a/fpdfsdk/fpdf_annot.cpp b/fpdfsdk/fpdf_annot.cpp
index cf94674..ee909ff 100644
--- a/fpdfsdk/fpdf_annot.cpp
+++ b/fpdfsdk/fpdf_annot.cpp
@@ -378,7 +378,8 @@
   if (!pPage || index < 0)
     return nullptr;
 
-  CPDF_Array* pAnnots = pPage->GetDict()->GetArrayFor("Annots");
+  RetainPtr<CPDF_Array> pAnnots =
+      pPage->GetDict()->GetMutableArrayFor("Annots");
   if (!pAnnots || static_cast<size_t>(index) >= pAnnots->size())
     return nullptr;
 
@@ -430,7 +431,8 @@
   if (!pPage || index < 0)
     return false;
 
-  CPDF_Array* pAnnots = pPage->GetDict()->GetArrayFor("Annots");
+  RetainPtr<CPDF_Array> pAnnots =
+      pPage->GetDict()->GetMutableArrayFor("Annots");
   if (!pAnnots || static_cast<size_t>(index) >= pAnnots->size())
     return false;
 
@@ -660,7 +662,7 @@
 
   // Set the color of the annotation.
   ByteString key = type == FPDFANNOT_COLORTYPE_InteriorColor ? "IC" : "C";
-  CPDF_Array* pColor = pAnnotDict->GetArrayFor(key);
+  RetainPtr<CPDF_Array> pColor = pAnnotDict->GetMutableArrayFor(key);
   if (pColor)
     pColor->Clear();
   else
@@ -757,12 +759,12 @@
 
   RetainPtr<CPDF_Dictionary> pAnnotDict =
       CPDFAnnotContextFromFPDFAnnotation(annot)->GetMutableAnnotDict();
-  CPDF_Array* pQuadPointsArray =
-      GetQuadPointsArrayFromDictionary(pAnnotDict.Get());
-  if (!IsValidQuadPointsIndex(pQuadPointsArray, quad_index))
+  RetainPtr<CPDF_Array> pQuadPointsArray =
+      GetMutableQuadPointsArrayFromDictionary(pAnnotDict.Get());
+  if (!IsValidQuadPointsIndex(pQuadPointsArray.Get(), quad_index))
     return false;
 
-  SetQuadPointsAtIndex(pQuadPointsArray, quad_index, quad_points);
+  SetQuadPointsAtIndex(pQuadPointsArray.Get(), quad_index, quad_points);
   UpdateBBox(pAnnotDict.Get());
   return true;
 }
@@ -775,11 +777,11 @@
 
   RetainPtr<CPDF_Dictionary> pAnnotDict =
       CPDFAnnotContextFromFPDFAnnotation(annot)->GetMutableAnnotDict();
-  CPDF_Array* pQuadPointsArray =
-      GetQuadPointsArrayFromDictionary(pAnnotDict.Get());
+  RetainPtr<CPDF_Array> pQuadPointsArray =
+      GetMutableQuadPointsArrayFromDictionary(pAnnotDict.Get());
   if (!pQuadPointsArray)
     pQuadPointsArray = AddQuadPointsArrayToDictionary(pAnnotDict.Get());
-  AppendQuadPoints(pQuadPointsArray, quad_points);
+  AppendQuadPoints(pQuadPointsArray.Get(), quad_points);
   UpdateBBox(pAnnotDict.Get());
   return true;
 }
diff --git a/fpdfsdk/fpdf_doc.cpp b/fpdfsdk/fpdf_doc.cpp
index 322e1b4..959bf62 100644
--- a/fpdfsdk/fpdf_doc.cpp
+++ b/fpdfsdk/fpdf_doc.cpp
@@ -366,7 +366,8 @@
   CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
   if (!pPage)
     return false;
-  CPDF_Array* pAnnots = pPage->GetDict()->GetArrayFor("Annots");
+  RetainPtr<CPDF_Array> pAnnots =
+      pPage->GetDict()->GetMutableArrayFor("Annots");
   if (!pAnnots)
     return false;
   for (size_t i = *start_pos; i < pAnnots->size(); i++) {
diff --git a/fpdfsdk/fpdf_editpage.cpp b/fpdfsdk/fpdf_editpage.cpp
index 985823a..4ee35f8 100644
--- a/fpdfsdk/fpdf_editpage.cpp
+++ b/fpdfsdk/fpdf_editpage.cpp
@@ -702,7 +702,7 @@
     CFX_FloatRect rect = matrix.TransformRect(pAnnot->GetRect());
 
     CPDF_Dictionary* pAnnotDict = pAnnot->GetAnnotDict();
-    CPDF_Array* pRectArray = pAnnotDict->GetArrayFor("Rect");
+    RetainPtr<CPDF_Array> pRectArray = pAnnotDict->GetMutableArrayFor("Rect");
     if (pRectArray)
       pRectArray->Clear();
     else
diff --git a/fpdfsdk/fpdf_flatten.cpp b/fpdfsdk/fpdf_flatten.cpp
index 0dcc58f..9eb3179 100644
--- a/fpdfsdk/fpdf_flatten.cpp
+++ b/fpdfsdk/fpdf_flatten.cpp
@@ -93,7 +93,7 @@
     return FLATTEN_FAIL;
 
   GetContentsRect(pSourceDoc, pPageDic, pRectArray);
-  CPDF_Array* pAnnots = pPageDic->GetArrayFor("Annots");
+  const CPDF_Array* pAnnots = pPageDic->GetArrayFor("Annots");
   if (!pAnnots)
     return FLATTEN_NOTHINGTODO;
 
@@ -190,8 +190,8 @@
 void SetPageContents(const ByteString& key,
                      CPDF_Dictionary* pPage,
                      CPDF_Document* pDocument) {
-  CPDF_Array* pContentsArray =
-      pPage->GetArrayFor(pdfium::page_object::kContents);
+  RetainPtr<CPDF_Array> pContentsArray =
+      pPage->GetMutableArrayFor(pdfium::page_object::kContents);
   CPDF_Stream* pContentsStream =
       pPage->GetStreamFor(pdfium::page_object::kContents);
   if (!pContentsStream && !pContentsArray) {
@@ -337,13 +337,14 @@
     rcAnnot.Normalize();
 
     ByteString sAnnotState = pAnnotDict->GetStringFor("AS");
-    CPDF_Dictionary* pAnnotAP = pAnnotDict->GetDictFor(pdfium::annotation::kAP);
+    RetainPtr<CPDF_Dictionary> pAnnotAP =
+        pAnnotDict->GetMutableDictFor(pdfium::annotation::kAP);
     if (!pAnnotAP)
       continue;
 
     CPDF_Stream* pAPStream = pAnnotAP->GetStreamFor("N");
     if (!pAPStream) {
-      CPDF_Dictionary* pAPDict = pAnnotAP->GetDictFor("N");
+      RetainPtr<CPDF_Dictionary> pAPDict = pAnnotAP->GetMutableDictFor("N");
       if (!pAPDict)
         continue;
 
@@ -351,7 +352,7 @@
         pAPStream = pAPDict->GetStreamFor(sAnnotState);
       } else {
         if (pAPDict->size() > 0) {
-          CPDF_DictionaryLocker locker(pAPDict);
+          CPDF_DictionaryLocker locker(pAPDict.Get());
           CPDF_Object* pFirstObj = locker.begin()->second.Get();
           if (pFirstObj) {
             if (pFirstObj->IsReference())
diff --git a/fpdfsdk/fpdf_save.cpp b/fpdfsdk/fpdf_save.cpp
index 4fb5d3f..bcaea81 100644
--- a/fpdfsdk/fpdf_save.cpp
+++ b/fpdfsdk/fpdf_save.cpp
@@ -50,7 +50,7 @@
   if (!pRoot)
     return false;
 
-  CPDF_Dictionary* pAcroForm = pRoot->GetDictFor("AcroForm");
+  RetainPtr<CPDF_Dictionary> pAcroForm = pRoot->GetMutableDictFor("AcroForm");
   if (!pAcroForm)
     return false;
 
diff --git a/fpdfsdk/fpdf_signature.cpp b/fpdfsdk/fpdf_signature.cpp
index 00f0a6c..7134988 100644
--- a/fpdfsdk/fpdf_signature.cpp
+++ b/fpdfsdk/fpdf_signature.cpp
@@ -65,11 +65,12 @@
 FPDFSignatureObj_GetContents(FPDF_SIGNATURE signature,
                              void* buffer,
                              unsigned long length) {
-  CPDF_Dictionary* signature_dict = CPDFDictionaryFromFPDFSignature(signature);
+  const CPDF_Dictionary* signature_dict =
+      CPDFDictionaryFromFPDFSignature(signature);
   if (!signature_dict)
     return 0;
 
-  CPDF_Dictionary* value_dict = signature_dict->GetDictFor("V");
+  const CPDF_Dictionary* value_dict = signature_dict->GetDictFor("V");
   if (!value_dict)
     return 0;
 
@@ -86,11 +87,12 @@
 FPDFSignatureObj_GetByteRange(FPDF_SIGNATURE signature,
                               int* buffer,
                               unsigned long length) {
-  CPDF_Dictionary* signature_dict = CPDFDictionaryFromFPDFSignature(signature);
+  const CPDF_Dictionary* signature_dict =
+      CPDFDictionaryFromFPDFSignature(signature);
   if (!signature_dict)
     return 0;
 
-  CPDF_Dictionary* value_dict = signature_dict->GetDictFor("V");
+  const CPDF_Dictionary* value_dict = signature_dict->GetDictFor("V");
   if (!value_dict)
     return 0;
 
@@ -115,7 +117,7 @@
   if (!signature_dict)
     return 0;
 
-  CPDF_Dictionary* value_dict = signature_dict->GetDictFor("V");
+  const CPDF_Dictionary* value_dict = signature_dict->GetDictFor("V");
   if (!value_dict || !value_dict->KeyExist("SubFilter"))
     return 0;
 
@@ -131,7 +133,7 @@
   if (!signature_dict)
     return 0;
 
-  CPDF_Dictionary* value_dict = signature_dict->GetDictFor("V");
+  const CPDF_Dictionary* value_dict = signature_dict->GetDictFor("V");
   if (!value_dict)
     return 0;
 
@@ -151,7 +153,7 @@
   if (!signature_dict)
     return 0;
 
-  CPDF_Dictionary* value_dict = signature_dict->GetDictFor("V");
+  const CPDF_Dictionary* value_dict = signature_dict->GetDictFor("V");
   if (!value_dict)
     return 0;
 
@@ -169,11 +171,11 @@
   if (!signature_dict)
     return permission;
 
-  CPDF_Dictionary* value_dict = signature_dict->GetDictFor("V");
+  const CPDF_Dictionary* value_dict = signature_dict->GetDictFor("V");
   if (!value_dict)
     return permission;
 
-  CPDF_Array* references = value_dict->GetArrayFor("Reference");
+  const CPDF_Array* references = value_dict->GetArrayFor("Reference");
   if (!references)
     return permission;
 
@@ -187,7 +189,7 @@
     if (transform_method != "DocMDP")
       continue;
 
-    CPDF_Dictionary* transform_params =
+    const CPDF_Dictionary* transform_params =
         reference_dict->GetDictFor("TransformParams");
     if (!transform_params)
       continue;
diff --git a/fpdfsdk/fpdf_transformpage.cpp b/fpdfsdk/fpdf_transformpage.cpp
index 7da04e4..d8709a9 100644
--- a/fpdfsdk/fpdf_transformpage.cpp
+++ b/fpdfsdk/fpdf_transformpage.cpp
@@ -255,12 +255,12 @@
   }
 
   // Need to transform the patterns as well.
-  CPDF_Dictionary* pRes =
+  const CPDF_Dictionary* pRes =
       pPageDict->GetDictFor(pdfium::page_object::kResources);
   if (!pRes)
     return true;
 
-  CPDF_Dictionary* pPatternDict = pRes->GetDictFor("Pattern");
+  const CPDF_Dictionary* pPatternDict = pRes->GetDictFor("Pattern");
   if (!pPatternDict)
     return true;
 
diff --git a/fpdfsdk/fpdf_view.cpp b/fpdfsdk/fpdf_view.cpp
index 161340b..cc9d17b 100644
--- a/fpdfsdk/fpdf_view.cpp
+++ b/fpdfsdk/fpdf_view.cpp
@@ -1135,7 +1135,7 @@
 
   auto name_tree = CPDF_NameTree::Create(pDoc, "Dests");
   size_t name_tree_count = name_tree ? name_tree->GetCount() : 0;
-  CPDF_Object* pDestObj = nullptr;
+  const CPDF_Object* pDestObj = nullptr;
   WideString wsName;
   if (static_cast<size_t>(index) >= name_tree_count) {
     // If |index| is out of bounds, then try to retrieve the Nth old style named
@@ -1166,7 +1166,7 @@
   }
   if (!pDestObj)
     return nullptr;
-  if (CPDF_Dictionary* pDict = pDestObj->AsDictionary()) {
+  if (const CPDF_Dictionary* pDict = pDestObj->AsDictionary()) {
     pDestObj = pDict->GetArrayFor("D");
     if (!pDestObj)
       return nullptr;
diff --git a/xfa/fgas/font/cfgas_pdffontmgr.cpp b/xfa/fgas/font/cfgas_pdffontmgr.cpp
index 20e011a..55697ea 100644
--- a/xfa/fgas/font/cfgas_pdffontmgr.cpp
+++ b/xfa/fgas/font/cfgas_pdffontmgr.cpp
@@ -40,7 +40,7 @@
                                                    bool bBold,
                                                    bool bItalic,
                                                    bool bStrictMatch) {
-  CPDF_Dictionary* pFontSetDict =
+  const CPDF_Dictionary* pFontSetDict =
       m_pDoc->GetRoot()->GetDictFor("AcroForm")->GetDictFor("DR");
   if (!pFontSetDict)
     return nullptr;