Pass/return retained args from CPDF_NumberTree.

Change-Id: Ice3c121f2fa0e56995e6f08209de429c98f4ff40
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/98593
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fpdfdoc/cpdf_numbertree.cpp b/core/fpdfdoc/cpdf_numbertree.cpp
index 71cb3ef..d597a7d 100644
--- a/core/fpdfdoc/cpdf_numbertree.cpp
+++ b/core/fpdfdoc/cpdf_numbertree.cpp
@@ -6,13 +6,15 @@
 
 #include "core/fpdfdoc/cpdf_numbertree.h"
 
+#include <utility>
+
 #include "core/fpdfapi/parser/cpdf_array.h"
 #include "core/fpdfapi/parser/cpdf_dictionary.h"
 
 namespace {
 
-// TODO(tsepez): return retained objects.
-const CPDF_Object* SearchNumberNode(const CPDF_Dictionary* pNode, int num) {
+RetainPtr<const CPDF_Object> SearchNumberNode(const CPDF_Dictionary* pNode,
+                                              int num) {
   RetainPtr<const CPDF_Array> pLimits = pNode->GetArrayFor("Limits");
   if (pLimits &&
       (num < pLimits->GetIntegerAt(0) || num > pLimits->GetIntegerAt(1))) {
@@ -23,7 +25,7 @@
     for (size_t i = 0; i < pNumbers->size() / 2; i++) {
       int index = pNumbers->GetIntegerAt(i * 2);
       if (num == index)
-        return pNumbers->GetDirectObjectAt(i * 2 + 1).Get();
+        return pNumbers->GetDirectObjectAt(i * 2 + 1);
       if (index > num)
         break;
     }
@@ -39,7 +41,7 @@
     if (!pKid)
       continue;
 
-    const CPDF_Object* pFound = SearchNumberNode(pKid.Get(), num);
+    RetainPtr<const CPDF_Object> pFound = SearchNumberNode(pKid.Get(), num);
     if (pFound)
       return pFound;
   }
@@ -48,11 +50,11 @@
 
 }  // namespace
 
-CPDF_NumberTree::CPDF_NumberTree(const CPDF_Dictionary* pRoot)
-    : m_pRoot(pRoot) {}
+CPDF_NumberTree::CPDF_NumberTree(RetainPtr<const CPDF_Dictionary> pRoot)
+    : m_pRoot(std::move(pRoot)) {}
 
 CPDF_NumberTree::~CPDF_NumberTree() = default;
 
-const CPDF_Object* CPDF_NumberTree::LookupValue(int num) const {
+RetainPtr<const CPDF_Object> CPDF_NumberTree::LookupValue(int num) const {
   return SearchNumberNode(m_pRoot.Get(), num);
 }
diff --git a/core/fpdfdoc/cpdf_numbertree.h b/core/fpdfdoc/cpdf_numbertree.h
index 2a3fa6e..cefd58e 100644
--- a/core/fpdfdoc/cpdf_numbertree.h
+++ b/core/fpdfdoc/cpdf_numbertree.h
@@ -14,10 +14,10 @@
 
 class CPDF_NumberTree {
  public:
-  explicit CPDF_NumberTree(const CPDF_Dictionary* pRoot);
+  explicit CPDF_NumberTree(RetainPtr<const CPDF_Dictionary> pRoot);
   ~CPDF_NumberTree();
 
-  const CPDF_Object* LookupValue(int num) const;
+  RetainPtr<const CPDF_Object> LookupValue(int num) const;
 
  protected:
   RetainPtr<const CPDF_Dictionary> const m_pRoot;
diff --git a/core/fpdfdoc/cpdf_pagelabel.cpp b/core/fpdfdoc/cpdf_pagelabel.cpp
index e67aee6..e9591cb 100644
--- a/core/fpdfdoc/cpdf_pagelabel.cpp
+++ b/core/fpdfdoc/cpdf_pagelabel.cpp
@@ -6,6 +6,8 @@
 
 #include "core/fpdfdoc/cpdf_pagelabel.h"
 
+#include <utility>
+
 #include "core/fpdfapi/parser/cpdf_dictionary.h"
 #include "core/fpdfapi/parser/cpdf_document.h"
 #include "core/fpdfapi/parser/fpdf_parser_decode.h"
@@ -94,13 +96,11 @@
   if (!pLabels)
     return absl::nullopt;
 
-  // TODO(tsepez): pass retained object.
-  CPDF_NumberTree numberTree(pLabels.Get());
+  CPDF_NumberTree numberTree(std::move(pLabels));
   RetainPtr<const CPDF_Object> pValue;
   int n = nPage;
   while (n >= 0) {
-    // TODO(tsepez): make LookupValue() return retained object.
-    pValue = pdfium::WrapRetain(numberTree.LookupValue(n));
+    pValue = numberTree.LookupValue(n);
     if (pValue)
       break;
     n--;
diff --git a/core/fpdfdoc/cpdf_structtree.cpp b/core/fpdfdoc/cpdf_structtree.cpp
index 7a67ea1..acfa405 100644
--- a/core/fpdfdoc/cpdf_structtree.cpp
+++ b/core/fpdfdoc/cpdf_structtree.cpp
@@ -64,13 +64,13 @@
 
   m_Kids.clear();
   m_Kids.resize(dwKids);
+
   RetainPtr<const CPDF_Dictionary> pParentTree =
       m_pTreeRoot->GetDictFor("ParentTree");
   if (!pParentTree)
     return;
 
-  // TODO(tsepez): pass retained object.
-  CPDF_NumberTree parent_tree(pParentTree.Get());
+  CPDF_NumberTree parent_tree(std::move(pParentTree));
   int parents_id = pPageDict->GetIntegerFor("StructParents", -1);
   if (parents_id < 0)
     return;