Return retained objects from CPDF_Image and CPDF_Page methods.

Change-Id: Ifa5a6708a18a68bc9a1557786b7da88f458b9dc0
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/98470
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fpdfapi/page/cpdf_image.cpp b/core/fpdfapi/page/cpdf_image.cpp
index c5f48d6..0f90145 100644
--- a/core/fpdfapi/page/cpdf_image.cpp
+++ b/core/fpdfapi/page/cpdf_image.cpp
@@ -80,9 +80,8 @@
   m_pDocument->AddIndirectObject(m_pStream);
 }
 
-const CPDF_Dictionary* CPDF_Image::GetDict() const {
-  // TODO(tsepez): return retained objects.
-  return m_pStream ? m_pStream->GetDict().Get() : nullptr;
+RetainPtr<const CPDF_Dictionary> CPDF_Image::GetDict() const {
+  return m_pStream ? m_pStream->GetDict() : nullptr;
 }
 
 RetainPtr<CPDF_Dictionary> CPDF_Image::InitJPEG(
diff --git a/core/fpdfapi/page/cpdf_image.h b/core/fpdfapi/page/cpdf_image.h
index 1de8209..471d010 100644
--- a/core/fpdfapi/page/cpdf_image.h
+++ b/core/fpdfapi/page/cpdf_image.h
@@ -33,7 +33,7 @@
 
   void ConvertStreamToIndirectObject();
 
-  const CPDF_Dictionary* GetDict() const;
+  RetainPtr<const CPDF_Dictionary> GetDict() const;
   const CPDF_Stream* GetStream() const { return m_pStream.Get(); }
   const CPDF_Dictionary* GetOC() const { return m_pOC.Get(); }
   CPDF_Document* GetDocument() const { return m_pDocument.Get(); }
diff --git a/core/fpdfapi/page/cpdf_page.cpp b/core/fpdfapi/page/cpdf_page.cpp
index 9e06c13..8be52b4 100644
--- a/core/fpdfapi/page/cpdf_page.cpp
+++ b/core/fpdfapi/page/cpdf_page.cpp
@@ -75,18 +75,18 @@
 }
 
 RetainPtr<CPDF_Object> CPDF_Page::GetMutablePageAttr(const ByteString& name) {
-  return pdfium::WrapRetain(const_cast<CPDF_Object*>(GetPageAttr(name)));
+  return pdfium::WrapRetain(const_cast<CPDF_Object*>(GetPageAttr(name).Get()));
 }
 
-const CPDF_Object* CPDF_Page::GetPageAttr(const ByteString& name) const {
+RetainPtr<const CPDF_Object> CPDF_Page::GetPageAttr(
+    const ByteString& name) const {
   std::set<const CPDF_Dictionary*> visited;
   const CPDF_Dictionary* pPageDict = GetDict();
   while (pPageDict && !pdfium::Contains(visited, pPageDict)) {
     RetainPtr<const CPDF_Object> pObj = pPageDict->GetDirectObjectFor(name);
-    if (pObj) {
-      // TODO(tsepez): return retained objects.
-      return pObj.Get();
-    }
+    if (pObj)
+      return pObj;
+
     visited.insert(pPageDict);
     pPageDict = pPageDict->GetDictFor(pdfium::page_object::kParent).Get();
   }
@@ -95,7 +95,7 @@
 
 CFX_FloatRect CPDF_Page::GetBox(const ByteString& name) const {
   CFX_FloatRect box;
-  const CPDF_Array* pBox = ToArray(GetPageAttr(name));
+  RetainPtr<const CPDF_Array> pBox = ToArray(GetPageAttr(name));
   if (pBox) {
     box = pBox->GetRect();
     box.Normalize();
@@ -177,7 +177,8 @@
 }
 
 int CPDF_Page::GetPageRotation() const {
-  const CPDF_Object* pRotate = GetPageAttr(pdfium::page_object::kRotate);
+  RetainPtr<const CPDF_Object> pRotate =
+      GetPageAttr(pdfium::page_object::kRotate);
   int rotate = pRotate ? (pRotate->GetInteger() / 90) % 4 : 0;
   return (rotate < 0) ? (rotate + 4) : rotate;
 }
diff --git a/core/fpdfapi/page/cpdf_page.h b/core/fpdfapi/page/cpdf_page.h
index dfc5052..0274fe2 100644
--- a/core/fpdfapi/page/cpdf_page.h
+++ b/core/fpdfapi/page/cpdf_page.h
@@ -104,7 +104,7 @@
   ~CPDF_Page() override;
 
   RetainPtr<CPDF_Object> GetMutablePageAttr(const ByteString& name);
-  const CPDF_Object* GetPageAttr(const ByteString& name) const;
+  RetainPtr<const CPDF_Object> GetPageAttr(const ByteString& name) const;
   CFX_FloatRect GetBox(const ByteString& name) const;
 
   CFX_SizeF m_PageSize;
diff --git a/core/fpdfdoc/cpdf_bookmark.cpp b/core/fpdfdoc/cpdf_bookmark.cpp
index 6d0a1a6..cb50d25 100644
--- a/core/fpdfdoc/cpdf_bookmark.cpp
+++ b/core/fpdfdoc/cpdf_bookmark.cpp
@@ -6,6 +6,7 @@
 
 #include "core/fpdfdoc/cpdf_bookmark.h"
 
+#include "core/fpdfapi/parser/cpdf_array.h"
 #include "core/fpdfapi/parser/cpdf_dictionary.h"
 #include "core/fpdfapi/parser/cpdf_string.h"
 #include "core/fxcrt/data_vector.h"
diff --git a/fpdfsdk/fpdf_editimg.cpp b/fpdfsdk/fpdf_editimg.cpp
index f4378a9..3c41243 100644
--- a/fpdfsdk/fpdf_editimg.cpp
+++ b/fpdfsdk/fpdf_editimg.cpp
@@ -312,7 +312,7 @@
   if (!pImg)
     return 0;
 
-  const CPDF_Dictionary* pDict = pImg->GetDict();
+  RetainPtr<const CPDF_Dictionary> pDict = pImg->GetDict();
   if (!pDict)
     return 0;
 
@@ -338,7 +338,8 @@
     return 0;
 
   CPDF_PageObject* pObj = CPDFPageObjectFromFPDFPageObject(image_object);
-  const CPDF_Dictionary* pDict = pObj->AsImage()->GetImage()->GetDict();
+  RetainPtr<const CPDF_Dictionary> pDict =
+      pObj->AsImage()->GetImage()->GetDict();
   RetainPtr<const CPDF_Object> pFilter = pDict->GetDirectObjectFor("Filter");
   ByteString bsFilter = pFilter->IsName()
                             ? pFilter->AsName()->GetString()