Avoid using raw pointers as keys in maps.

Convert to an equivalent smart pointer type as possible.

Change-Id: I4e7621fe7238192c36613ccb372f6b1062a84f49
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/98453
Commit-Queue: Tom Sepez <tsepez@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
diff --git a/core/fpdfapi/page/cpdf_occontext.cpp b/core/fpdfapi/page/cpdf_occontext.cpp
index cc652df..b8edb07 100644
--- a/core/fpdfapi/page/cpdf_occontext.cpp
+++ b/core/fpdfapi/page/cpdf_occontext.cpp
@@ -167,7 +167,7 @@
     return it->second;
 
   bool bState = LoadOCGState(pOCGDict);
-  m_OGCStateCache[pOCGDict] = bState;
+  m_OGCStateCache[pdfium::WrapRetain(pOCGDict)] = bState;
   return bState;
 }
 
diff --git a/core/fpdfapi/page/cpdf_occontext.h b/core/fpdfapi/page/cpdf_occontext.h
index c8a5dad..9c70068 100644
--- a/core/fpdfapi/page/cpdf_occontext.h
+++ b/core/fpdfapi/page/cpdf_occontext.h
@@ -7,6 +7,7 @@
 #ifndef CORE_FPDFAPI_PAGE_CPDF_OCCONTEXT_H_
 #define CORE_FPDFAPI_PAGE_CPDF_OCCONTEXT_H_
 
+#include <functional>
 #include <map>
 
 #include "core/fxcrt/bytestring.h"
@@ -40,7 +41,8 @@
 
   UnownedPtr<CPDF_Document> const m_pDocument;
   const UsageType m_eUsageType;
-  mutable std::map<const CPDF_Dictionary*, bool> m_OGCStateCache;
+  mutable std::map<RetainPtr<const CPDF_Dictionary>, bool, std::less<>>
+      m_OGCStateCache;
 };
 
 #endif  // CORE_FPDFAPI_PAGE_CPDF_OCCONTEXT_H_
diff --git a/core/fpdfapi/parser/cpdf_data_avail.h b/core/fpdfapi/parser/cpdf_data_avail.h
index 87ab321..5cbd279 100644
--- a/core/fpdfapi/parser/cpdf_data_avail.h
+++ b/core/fpdfapi/parser/cpdf_data_avail.h
@@ -7,6 +7,7 @@
 #ifndef CORE_FPDFAPI_PARSER_CPDF_DATA_AVAIL_H_
 #define CORE_FPDFAPI_PARSER_CPDF_DATA_AVAIL_H_
 
+#include <functional>
 #include <map>
 #include <memory>
 #include <set>
@@ -183,14 +184,16 @@
   std::vector<RetainPtr<CPDF_Object>> m_PagesArray;
   bool m_bTotalLoadPageTree = false;
   bool m_bCurPageDictLoadOK = false;
+  bool m_bHeaderAvail = false;
   PageNode m_PageNode;
   std::set<uint32_t> m_pageMapCheckState;
   std::set<uint32_t> m_pagesLoadState;
   std::unique_ptr<CPDF_HintTables> m_pHintTables;
   std::map<uint32_t, std::unique_ptr<CPDF_PageObjectAvail>> m_PagesObjAvail;
-  std::map<const CPDF_Object*, std::unique_ptr<CPDF_PageObjectAvail>>
+  std::map<RetainPtr<const CPDF_Object>,
+           std::unique_ptr<CPDF_PageObjectAvail>,
+           std::less<>>
       m_PagesResourcesAvail;
-  bool m_bHeaderAvail = false;
 };
 
 #endif  // CORE_FPDFAPI_PARSER_CPDF_DATA_AVAIL_H_
diff --git a/core/fpdfapi/render/cpdf_docrenderdata.cpp b/core/fpdfapi/render/cpdf_docrenderdata.cpp
index a2e0654..701dede 100644
--- a/core/fpdfapi/render/cpdf_docrenderdata.cpp
+++ b/core/fpdfapi/render/cpdf_docrenderdata.cpp
@@ -63,7 +63,7 @@
     return pdfium::WrapRetain(it->second.Get());
 
   auto pFunc = CreateTransferFunc(pObj);
-  m_TransferFuncMap[pObj].Reset(pFunc.Get());
+  m_TransferFuncMap[pdfium::WrapRetain(pObj)].Reset(pFunc.Get());
   return pFunc;
 }
 
diff --git a/core/fpdfapi/render/cpdf_docrenderdata.h b/core/fpdfapi/render/cpdf_docrenderdata.h
index 6a500c6..c280fe7 100644
--- a/core/fpdfapi/render/cpdf_docrenderdata.h
+++ b/core/fpdfapi/render/cpdf_docrenderdata.h
@@ -7,6 +7,7 @@
 #ifndef CORE_FPDFAPI_RENDER_CPDF_DOCRENDERDATA_H_
 #define CORE_FPDFAPI_RENDER_CPDF_DOCRENDERDATA_H_
 
+#include <functional>
 #include <map>
 
 #include "build/build_config.h"
@@ -51,8 +52,11 @@
       const CPDF_Object* pObj) const;
 
  private:
+  // TODO(tsepez): investigate this map outliving its font keys.
   std::map<CPDF_Font*, ObservedPtr<CPDF_Type3Cache>> m_Type3FaceMap;
-  std::map<const CPDF_Object*, ObservedPtr<CPDF_TransferFunc>>
+  std::map<RetainPtr<const CPDF_Object>,
+           ObservedPtr<CPDF_TransferFunc>,
+           std::less<>>
       m_TransferFuncMap;
 
 #if BUILDFLAG(IS_WIN)
diff --git a/core/fpdfapi/render/cpdf_pagerendercache.cpp b/core/fpdfapi/render/cpdf_pagerendercache.cpp
index 4a49778..d7497de 100644
--- a/core/fpdfapi/render/cpdf_pagerendercache.cpp
+++ b/core/fpdfapi/render/cpdf_pagerendercache.cpp
@@ -56,7 +56,7 @@
   uint32_t nTimeCount = m_nTimeCount;
   if (nTimeCount + 1 < nTimeCount) {
     for (uint32_t i = 0; i < nCount; i++)
-      m_ImageCache[cache_info[i].pStream]->SetTimeCount(i);
+      m_ImageCache[pdfium::WrapRetain(cache_info[i].pStream)]->SetTimeCount(i);
     m_nTimeCount = nCount;
   }
 
@@ -97,7 +97,7 @@
 
   m_nTimeCount++;
   if (!m_bCurFindCache)
-    m_ImageCache[pStream] = m_pCurImageCacheEntry.Release();
+    m_ImageCache[pdfium::WrapRetain(pStream)] = m_pCurImageCacheEntry.Release();
 
   if (ret == CPDF_DIB::LoadState::kFail)
     m_nCacheSize += m_pCurImageCacheEntry->EstimateSize();
@@ -113,7 +113,9 @@
 
   m_nTimeCount++;
   if (!m_bCurFindCache) {
-    m_ImageCache[m_pCurImageCacheEntry->GetImage()->GetStream()] =
+    // TODO(tsepez): GetStream() should return retained reference.
+    m_ImageCache[pdfium::WrapRetain(
+        m_pCurImageCacheEntry->GetImage()->GetStream())] =
         m_pCurImageCacheEntry.Release();
   }
   m_nCacheSize += m_pCurImageCacheEntry->EstimateSize();
diff --git a/core/fpdfapi/render/cpdf_pagerendercache.h b/core/fpdfapi/render/cpdf_pagerendercache.h
index e98dba1..d9eddf0 100644
--- a/core/fpdfapi/render/cpdf_pagerendercache.h
+++ b/core/fpdfapi/render/cpdf_pagerendercache.h
@@ -9,6 +9,7 @@
 
 #include <stdint.h>
 
+#include <functional>
 #include <map>
 #include <memory>
 
@@ -91,7 +92,10 @@
   void ClearImageCacheEntry(const CPDF_Stream* pStream);
 
   UnownedPtr<CPDF_Page> const m_pPage;
-  std::map<const CPDF_Stream*, std::unique_ptr<ImageCacheEntry>> m_ImageCache;
+  std::map<RetainPtr<const CPDF_Stream>,
+           std::unique_ptr<ImageCacheEntry>,
+           std::less<>>
+      m_ImageCache;
   MaybeOwned<ImageCacheEntry> m_pCurImageCacheEntry;
   uint32_t m_nTimeCount = 0;
   uint32_t m_nCacheSize = 0;
diff --git a/core/fpdfdoc/cpdf_interactiveform.cpp b/core/fpdfdoc/cpdf_interactiveform.cpp
index a29be97..7721a5d 100644
--- a/core/fpdfdoc/cpdf_interactiveform.cpp
+++ b/core/fpdfdoc/cpdf_interactiveform.cpp
@@ -752,7 +752,7 @@
 
 const std::vector<UnownedPtr<CPDF_FormControl>>&
 CPDF_InteractiveForm::GetControlsForField(const CPDF_FormField* pField) {
-  return m_ControlLists[pField];
+  return m_ControlLists[pdfium::WrapUnowned(pField)];
 }
 
 void CPDF_InteractiveForm::LoadField(RetainPtr<CPDF_Dictionary> pFieldDict,
@@ -880,8 +880,8 @@
 
   auto pNew = std::make_unique<CPDF_FormControl>(pField, pWidgetDict);
   CPDF_FormControl* pControl = pNew.get();
-  m_ControlMap[pWidgetDict.Get()] = std::move(pNew);
-  m_ControlLists[pField].emplace_back(pControl);
+  m_ControlMap[pWidgetDict] = std::move(pNew);
+  m_ControlLists[pdfium::WrapUnowned(pField)].emplace_back(pControl);
   return pControl;
 }
 
diff --git a/core/fpdfdoc/cpdf_interactiveform.h b/core/fpdfdoc/cpdf_interactiveform.h
index e10deca..ee39243 100644
--- a/core/fpdfdoc/cpdf_interactiveform.h
+++ b/core/fpdfdoc/cpdf_interactiveform.h
@@ -10,6 +10,7 @@
 #include <stddef.h>
 #include <stdint.h>
 
+#include <functional>
 #include <map>
 #include <memory>
 #include <vector>
@@ -109,10 +110,14 @@
   UnownedPtr<CPDF_Document> const m_pDocument;
   RetainPtr<CPDF_Dictionary> m_pFormDict;
   std::unique_ptr<CFieldTree> m_pFieldTree;
-  std::map<const CPDF_Dictionary*, std::unique_ptr<CPDF_FormControl>>
+  std::map<RetainPtr<const CPDF_Dictionary>,
+           std::unique_ptr<CPDF_FormControl>,
+           std::less<>>
       m_ControlMap;
   // Points into |m_ControlMap|.
-  std::map<const CPDF_FormField*, std::vector<UnownedPtr<CPDF_FormControl>>>
+  std::map<UnownedPtr<const CPDF_FormField>,
+           std::vector<UnownedPtr<CPDF_FormControl>>,
+           std::less<>>
       m_ControlLists;
   UnownedPtr<NotifierIface> m_pFormNotify;
 };
diff --git a/core/fpdfdoc/cpdf_structtree.cpp b/core/fpdfdoc/cpdf_structtree.cpp
index 1294ba5..7a67ea1 100644
--- a/core/fpdfdoc/cpdf_structtree.cpp
+++ b/core/fpdfdoc/cpdf_structtree.cpp
@@ -99,12 +99,13 @@
   if (it != map->end())
     return it->second;
 
+  RetainPtr<const CPDF_Dictionary> key(pDict);
   auto pElement = pdfium::MakeRetain<CPDF_StructElement>(this, pDict);
-  (*map)[pDict] = pElement;
+  (*map)[key] = pElement;
   RetainPtr<const CPDF_Dictionary> pParent = pDict->GetDictFor("P");
   if (!pParent || pParent->GetNameFor("Type") == "StructTreeRoot") {
-    if (!AddTopLevelNode(pDict.Get(), pElement))
-      map->erase(pDict);
+    if (!AddTopLevelNode(pDict, pElement))
+      map->erase(key);
     return pElement;
   }
 
@@ -113,8 +114,8 @@
   if (!pParentElement)
     return pElement;
 
-  if (!pParentElement->UpdateKidIfElement(pDict.Get(), pElement.Get()))
-    map->erase(pDict);
+  if (!pParentElement->UpdateKidIfElement(pDict, pElement.Get()))
+    map->erase(key);
 
   pElement->SetParent(pParentElement.Get());
 
diff --git a/fpdfsdk/cpdfsdk_interactiveform.cpp b/fpdfsdk/cpdfsdk_interactiveform.cpp
index 0568c9f..c4f6477 100644
--- a/fpdfsdk/cpdfsdk_interactiveform.cpp
+++ b/fpdfsdk/cpdfsdk_interactiveform.cpp
@@ -198,11 +198,13 @@
 
 void CPDFSDK_InteractiveForm::AddMap(CPDF_FormControl* pControl,
                                      CPDFSDK_Widget* pWidget) {
-  m_Map[pControl] = pWidget;
+  m_Map[pdfium::WrapUnowned(pControl)] = pWidget;
 }
 
 void CPDFSDK_InteractiveForm::RemoveMap(CPDF_FormControl* pControl) {
-  m_Map.erase(pControl);
+  auto it = m_Map.find(pControl);
+  if (it != m_Map.end())
+    m_Map.erase(it);
 }
 
 void CPDFSDK_InteractiveForm::EnableCalculate(bool bEnabled) {
diff --git a/fpdfsdk/cpdfsdk_interactiveform.h b/fpdfsdk/cpdfsdk_interactiveform.h
index 2510d42..4de69b6 100644
--- a/fpdfsdk/cpdfsdk_interactiveform.h
+++ b/fpdfsdk/cpdfsdk_interactiveform.h
@@ -7,6 +7,7 @@
 #ifndef FPDFSDK_CPDFSDK_INTERACTIVEFORM_H_
 #define FPDFSDK_CPDFSDK_INTERACTIVEFORM_H_
 
+#include <functional>
 #include <map>
 #include <memory>
 #include <vector>
@@ -103,7 +104,10 @@
 
   UnownedPtr<CPDFSDK_FormFillEnvironment> const m_pFormFillEnv;
   std::unique_ptr<CPDF_InteractiveForm> const m_pInteractiveForm;
-  std::map<CPDF_FormControl*, UnownedPtr<CPDFSDK_Widget>> m_Map;
+  std::map<UnownedPtr<const CPDF_FormControl>,
+           UnownedPtr<CPDFSDK_Widget>,
+           std::less<>>
+      m_Map;
 #ifdef PDF_ENABLE_XFA
   bool m_bXfaCalculate = true;
   bool m_bXfaValidationsEnabled = true;