Use UnownedPtr<> keys in CPDF_FontGlobals.

Prove that CPDF_Document outlives the font globals.

Change-Id: I25e79c215e501bbf899a9ef4d3577d5751f1117e
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/98270
Commit-Queue: Tom Sepez <tsepez@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
diff --git a/core/fpdfapi/font/cpdf_fontglobals.cpp b/core/fpdfapi/font/cpdf_fontglobals.cpp
index 1873a01..afc57a5 100644
--- a/core/fpdfapi/font/cpdf_fontglobals.cpp
+++ b/core/fpdfapi/font/cpdf_fontglobals.cpp
@@ -77,13 +77,18 @@
 void CPDF_FontGlobals::Set(CPDF_Document* pDoc,
                            CFX_FontMapper::StandardFont index,
                            RetainPtr<CPDF_Font> pFont) {
-  if (!pdfium::Contains(m_StockMap, pDoc))
-    m_StockMap[pDoc] = std::make_unique<CFX_StockFontArray>();
-  m_StockMap[pDoc]->SetFont(index, std::move(pFont));
+  UnownedPtr<CPDF_Document> pKey(pDoc);
+  if (!pdfium::Contains(m_StockMap, pKey))
+    m_StockMap[pKey] = std::make_unique<CFX_StockFontArray>();
+  m_StockMap[pKey]->SetFont(index, std::move(pFont));
 }
 
 void CPDF_FontGlobals::Clear(CPDF_Document* pDoc) {
-  m_StockMap.erase(pDoc);
+  // Avoid constructing smart-pointer key as erase() doesn't invoke
+  // transparent lookup in the same way find() does.
+  auto it = m_StockMap.find(pDoc);
+  if (it != m_StockMap.end())
+    m_StockMap.erase(it);
 }
 
 void CPDF_FontGlobals::LoadEmbeddedGB1CMaps() {
diff --git a/core/fpdfapi/font/cpdf_fontglobals.h b/core/fpdfapi/font/cpdf_fontglobals.h
index e39555b..d86e853 100644
--- a/core/fpdfapi/font/cpdf_fontglobals.h
+++ b/core/fpdfapi/font/cpdf_fontglobals.h
@@ -7,6 +7,7 @@
 #ifndef CORE_FPDFAPI_FONT_CPDF_FONTGLOBALS_H_
 #define CORE_FPDFAPI_FONT_CPDF_FONTGLOBALS_H_
 
+#include <functional>
 #include <map>
 #include <memory>
 
@@ -65,7 +66,10 @@
   std::unique_ptr<CPDF_CID2UnicodeMap> m_CID2UnicodeMaps[CIDSET_NUM_SETS];
   pdfium::span<const FXCMAP_CMap> m_EmbeddedCharsets[CIDSET_NUM_SETS];
   pdfium::span<const uint16_t> m_EmbeddedToUnicodes[CIDSET_NUM_SETS];
-  std::map<CPDF_Document*, std::unique_ptr<CFX_StockFontArray>> m_StockMap;
+  std::map<UnownedPtr<CPDF_Document>,
+           std::unique_ptr<CFX_StockFontArray>,
+           std::less<>>
+      m_StockMap;
 };
 
 #endif  // CORE_FPDFAPI_FONT_CPDF_FONTGLOBALS_H_