Use CPDF_Dictionary::GetNameFor() to tighten checks.

Make sure many dictionary lookups that expect names are not getting some
other types' string representations. Fix a test that used a string when
it should have used a name.

Change-Id: I76721be0e37c581dc81c44f400e9317d76708dca
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/70854
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fpdfapi/parser/cpdf_data_avail.cpp b/core/fpdfapi/parser/cpdf_data_avail.cpp
index bb3c18f..ee16202 100644
--- a/core/fpdfapi/parser/cpdf_data_avail.cpp
+++ b/core/fpdfapi/parser/cpdf_data_avail.cpp
@@ -315,7 +315,7 @@
     if (!pObj->IsDictionary())
       continue;
 
-    ByteString type = pObj->GetDict()->GetStringFor("Type");
+    ByteString type = pObj->GetDict()->GetNameFor("Type");
     if (type == "Pages") {
       m_PagesArray.push_back(std::move(pObj));
       continue;
@@ -579,7 +579,7 @@
 
   pPageNode->m_dwPageNo = dwPageNo;
   CPDF_Dictionary* pDict = pPage->GetDict();
-  const ByteString type = pDict->GetStringFor("Type");
+  const ByteString type = pDict->GetNameFor("Type");
   if (type == "Page") {
     pPageNode->m_type = PDF_PAGENODE_PAGE;
     return true;
diff --git a/core/fpdfapi/parser/cpdf_document.cpp b/core/fpdfapi/parser/cpdf_document.cpp
index 8e6d3d2..612dbe8 100644
--- a/core/fpdfapi/parser/cpdf_document.cpp
+++ b/core/fpdfapi/parser/cpdf_document.cpp
@@ -386,7 +386,7 @@
 
   for (size_t i = 0; i < pKidList->size(); i++) {
     CPDF_Dictionary* pKid = pKidList->GetDictAt(i);
-    if (pKid->GetStringFor("Type") == "Page") {
+    if (pKid->GetNameFor("Type") == "Page") {
       if (nPagesToGo != 0) {
         nPagesToGo--;
         continue;
diff --git a/core/fpdfapi/parser/cpdf_object_stream.cpp b/core/fpdfapi/parser/cpdf_object_stream.cpp
index c0728eb..4b1635e 100644
--- a/core/fpdfapi/parser/cpdf_object_stream.cpp
+++ b/core/fpdfapi/parser/cpdf_object_stream.cpp
@@ -28,7 +28,7 @@
   if (!stream_dict)
     return false;
 
-  if (stream_dict->GetStringFor("Type") != "ObjStm")
+  if (stream_dict->GetNameFor("Type") != "ObjStm")
     return false;
 
   const CPDF_Number* number_of_objects =
diff --git a/core/fpdfapi/parser/cpdf_page_object_avail.cpp b/core/fpdfapi/parser/cpdf_page_object_avail.cpp
index 157702c..c4333c8 100644
--- a/core/fpdfapi/parser/cpdf_page_object_avail.cpp
+++ b/core/fpdfapi/parser/cpdf_page_object_avail.cpp
@@ -13,5 +13,5 @@
     return true;
 
   return object->IsDictionary() &&
-         object->GetDict()->GetStringFor("Type") == "Page";
+         object->GetDict()->GetNameFor("Type") == "Page";
 }
diff --git a/core/fpdfapi/parser/cpdf_page_object_avail_unittest.cpp b/core/fpdfapi/parser/cpdf_page_object_avail_unittest.cpp
index 979dbe5..c7257c5 100644
--- a/core/fpdfapi/parser/cpdf_page_object_avail_unittest.cpp
+++ b/core/fpdfapi/parser/cpdf_page_object_avail_unittest.cpp
@@ -11,6 +11,7 @@
 #include "core/fpdfapi/parser/cpdf_array.h"
 #include "core/fpdfapi/parser/cpdf_dictionary.h"
 #include "core/fpdfapi/parser/cpdf_indirect_object_holder.h"
+#include "core/fpdfapi/parser/cpdf_name.h"
 #include "core/fpdfapi/parser/cpdf_read_validator.h"
 #include "core/fpdfapi/parser/cpdf_reference.h"
 #include "core/fpdfapi/parser/cpdf_string.h"
@@ -106,7 +107,7 @@
   holder.AddObject(3, pdfium::MakeRetain<CPDF_Dictionary>(),
                    TestHolder::ObjectState::Available);
   holder.GetTestObject(3)->GetDict()->SetFor(
-      "Type", pdfium::MakeRetain<CPDF_String>(nullptr, "Page", false));
+      "Type", pdfium::MakeRetain<CPDF_Name>(nullptr, "Page"));
   holder.GetTestObject(3)->GetDict()->SetNewFor<CPDF_Reference>("OtherPageData",
                                                                 &holder, 4);
   // Add unavailable object related to other page.
diff --git a/core/fpdfapi/parser/cpdf_parser.cpp b/core/fpdfapi/parser/cpdf_parser.cpp
index 2fbd78c..9c54ebc 100644
--- a/core/fpdfapi/parser/cpdf_parser.cpp
+++ b/core/fpdfapi/parser/cpdf_parser.cpp
@@ -242,7 +242,7 @@
   if (!pEncryptDict)
     return SUCCESS;
 
-  if (pEncryptDict->GetStringFor("Filter") != "Standard")
+  if (pEncryptDict->GetNameFor("Filter") != "Standard")
     return HANDLER_ERROR;
 
   auto pSecurityHandler = pdfium::MakeRetain<CPDF_SecurityHandler>();
@@ -623,7 +623,7 @@
           ToStream(m_pSyntax->GetIndirectObject(
               nullptr, CPDF_SyntaxParser::ParseType::kStrict));
 
-      if (pStream && pStream->GetDict()->GetStringFor("Type") == "XRef") {
+      if (pStream && pStream->GetDict()->GetNameFor("Type") == "XRef") {
         cross_ref_table = CPDF_CrossRefTable::MergeUp(
             std::move(cross_ref_table),
             std::make_unique<CPDF_CrossRefTable>(
diff --git a/core/fpdfapi/parser/cpdf_stream.cpp b/core/fpdfapi/parser/cpdf_stream.cpp
index 2d10f39..79ba56d 100644
--- a/core/fpdfapi/parser/cpdf_stream.cpp
+++ b/core/fpdfapi/parser/cpdf_stream.cpp
@@ -23,8 +23,8 @@
 namespace {
 
 bool IsMetaDataStreamDictionary(const CPDF_Dictionary* dict) {
-  return dict && dict->GetStringFor("Type") == "Metadata" &&
-         dict->GetStringFor("Subtype") == "XML";
+  return dict && dict->GetNameFor("Type") == "Metadata" &&
+         dict->GetNameFor("Subtype") == "XML";
 }
 
 }  // namespace
diff --git a/core/fpdfdoc/cba_fontmap.cpp b/core/fpdfdoc/cba_fontmap.cpp
index d8c2f9b..7ca0232 100644
--- a/core/fpdfdoc/cba_fontmap.cpp
+++ b/core/fpdfdoc/cba_fontmap.cpp
@@ -192,7 +192,7 @@
 
 RetainPtr<CPDF_Font> CBA_FontMap::FindFontSameCharset(ByteString* sFontAlias,
                                                       int32_t nCharset) {
-  if (m_pAnnotDict->GetStringFor(pdfium::annotation::kSubtype) != "Widget")
+  if (m_pAnnotDict->GetNameFor(pdfium::annotation::kSubtype) != "Widget")
     return nullptr;
 
   const CPDF_Dictionary* pRootDict = m_pDocument->GetRoot();
@@ -229,7 +229,7 @@
       continue;
 
     CPDF_Dictionary* pElement = ToDictionary(it.second->GetDirect());
-    if (!pElement || pElement->GetStringFor("Type") != "Font")
+    if (!pElement || pElement->GetNameFor("Type") != "Font")
       continue;
 
     auto* pData = CPDF_DocPageData::FromDocument(m_pDocument.Get());
@@ -252,7 +252,7 @@
 RetainPtr<CPDF_Font> CBA_FontMap::GetAnnotDefaultFont(ByteString* sAlias) {
   CPDF_Dictionary* pAcroFormDict = nullptr;
   const bool bWidget =
-      (m_pAnnotDict->GetStringFor(pdfium::annotation::kSubtype) == "Widget");
+      (m_pAnnotDict->GetNameFor(pdfium::annotation::kSubtype) == "Widget");
   if (bWidget) {
     CPDF_Dictionary* pRootDict = m_pDocument->GetRoot();
     if (pRootDict)
diff --git a/core/fpdfdoc/cpdf_interactiveform.cpp b/core/fpdfdoc/cpdf_interactiveform.cpp
index 4babd38..ff84ef2 100644
--- a/core/fpdfdoc/cpdf_interactiveform.cpp
+++ b/core/fpdfdoc/cpdf_interactiveform.cpp
@@ -133,7 +133,7 @@
     return nullptr;
 
   CPDF_Dictionary* pElement = pFonts->GetDictFor(csAlias);
-  if (!pElement || pElement->GetStringFor("Type") != "Font")
+  if (!pElement || pElement->GetNameFor("Type") != "Font")
     return nullptr;
 
   return CPDF_DocPageData::FromDocument(pDocument)->GetFont(pElement);
@@ -161,7 +161,7 @@
       continue;
 
     CPDF_Dictionary* pElement = ToDictionary(it.second->GetDirect());
-    if (!pElement || pElement->GetStringFor("Type") != "Font")
+    if (!pElement || pElement->GetNameFor("Type") != "Font")
       continue;
 
     auto* pData = CPDF_DocPageData::FromDocument(pDocument);
@@ -203,7 +203,7 @@
     CPDF_Dictionary* pElement = ToDictionary(it.second->GetDirect());
     if (!pElement)
       continue;
-    if (pElement->GetStringFor("Type") != "Font")
+    if (pElement->GetNameFor("Type") != "Font")
       continue;
     if (pFont->GetFontDict() == pElement) {
       *csNameTag = csKey;
@@ -239,7 +239,7 @@
       continue;
 
     CPDF_Dictionary* pElement = ToDictionary(it.second->GetDirect());
-    if (!pElement || pElement->GetStringFor("Type") != "Font")
+    if (!pElement || pElement->GetNameFor("Type") != "Font")
       continue;
 
     pFont = CPDF_DocPageData::FromDocument(pDocument)->GetFont(pElement);
@@ -870,7 +870,7 @@
 
   for (size_t i = 0; i < pAnnots->size(); i++) {
     CPDF_Dictionary* pAnnot = pAnnots->GetDictAt(i);
-    if (pAnnot && pAnnot->GetStringFor("Subtype") == "Widget")
+    if (pAnnot && pAnnot->GetNameFor("Subtype") == "Widget")
       LoadField(pAnnot, 0);
   }
 }
@@ -894,7 +894,7 @@
   if (!pField) {
     CPDF_Dictionary* pParent = pFieldDict;
     if (!pFieldDict->KeyExist(pdfium::form_fields::kT) &&
-        pFieldDict->GetStringFor("Subtype") == "Widget") {
+        pFieldDict->GetNameFor("Subtype") == "Widget") {
       pParent = pFieldDict->GetDictFor(pdfium::form_fields::kParent);
       if (!pParent)
         pParent = pFieldDict;
@@ -937,13 +937,13 @@
       CPDF_Dictionary* pKid = pKids->GetDictAt(i);
       if (!pKid)
         continue;
-      if (pKid->GetStringFor("Subtype") != "Widget")
+      if (pKid->GetNameFor("Subtype") != "Widget")
         continue;
 
       AddControl(pField, pKid);
     }
   } else {
-    if (pFieldDict->GetStringFor("Subtype") == "Widget")
+    if (pFieldDict->GetNameFor("Subtype") == "Widget")
       AddControl(pField, pFieldDict);
   }
 }
diff --git a/core/fpdfdoc/cpdf_structelement.cpp b/core/fpdfdoc/cpdf_structelement.cpp
index 3afa094..86bd847 100644
--- a/core/fpdfdoc/cpdf_structelement.cpp
+++ b/core/fpdfdoc/cpdf_structelement.cpp
@@ -21,9 +21,9 @@
 
 ByteString GetStructElementType(const CPDF_StructTree* pTree,
                                 const CPDF_Dictionary* pDict) {
-  ByteString type = pDict->GetStringFor("S");
+  ByteString type = pDict->GetNameFor("S");
   if (pTree->GetRoleMap()) {
-    ByteString mapped = pTree->GetRoleMap()->GetStringFor(type);
+    ByteString mapped = pTree->GetRoleMap()->GetNameFor(type);
     if (!mapped.IsEmpty())
       type = std::move(mapped);
   }
@@ -114,7 +114,7 @@
   if (const CPDF_Reference* pRef = ToReference(pKidDict->GetObjectFor("Pg")))
     PageObjNum = pRef->GetRefObjNum();
 
-  ByteString type = pKidDict->GetStringFor("Type");
+  ByteString type = pKidDict->GetNameFor("Type");
   if ((type == "MCR" || type == "OBJR") &&
       m_pTree->GetPage()->GetObjNum() != PageObjNum) {
     return;
diff --git a/core/fpdfdoc/cpdf_structtree.cpp b/core/fpdfdoc/cpdf_structtree.cpp
index d875b57..f9d8057 100644
--- a/core/fpdfdoc/cpdf_structtree.cpp
+++ b/core/fpdfdoc/cpdf_structtree.cpp
@@ -96,7 +96,7 @@
   auto pElement = pdfium::MakeRetain<CPDF_StructElement>(this, nullptr, pDict);
   (*map)[pDict] = pElement;
   const CPDF_Dictionary* pParent = pDict->GetDictFor("P");
-  if (!pParent || pParent->GetStringFor("Type") == "StructTreeRoot") {
+  if (!pParent || pParent->GetNameFor("Type") == "StructTreeRoot") {
     if (!AddTopLevelNode(pDict, pElement))
       map->erase(pDict);
     return pElement;
diff --git a/fpdfsdk/fpdf_annot.cpp b/fpdfsdk/fpdf_annot.cpp
index 6ac93cb..98b3e4f 100644
--- a/fpdfsdk/fpdf_annot.cpp
+++ b/fpdfsdk/fpdf_annot.cpp
@@ -410,7 +410,7 @@
     return FPDF_ANNOT_UNKNOWN;
 
   return static_cast<FPDF_ANNOTATION_SUBTYPE>(CPDF_Annot::StringToAnnotSubtype(
-      pAnnotDict->GetStringFor(pdfium::annotation::kSubtype)));
+      pAnnotDict->GetNameFor(pdfium::annotation::kSubtype)));
 }
 
 FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
@@ -659,7 +659,7 @@
     // Use default color. The default colors must be consistent with the ones
     // used to generate AP. See calls to GetColorStringWithDefault() in
     // CPVT_GenerateAP::Generate*AP().
-    if (pAnnotDict->GetStringFor(pdfium::annotation::kSubtype) == "Highlight") {
+    if (pAnnotDict->GetNameFor(pdfium::annotation::kSubtype) == "Highlight") {
       *R = 255;
       *G = 255;
       *B = 0;
@@ -968,7 +968,7 @@
     return nullptr;
 
   CPDF_Dictionary* pLinkedDict = pAnnot->GetAnnotDict()->GetDictFor(key);
-  if (!pLinkedDict || pLinkedDict->GetStringFor("Type") != "Annot")
+  if (!pLinkedDict || pLinkedDict->GetNameFor("Type") != "Annot")
     return nullptr;
 
   auto pLinkedAnnot =
diff --git a/xfa/fgas/font/cfgas_pdffontmgr.cpp b/xfa/fgas/font/cfgas_pdffontmgr.cpp
index 0b622ad..b55ca40 100644
--- a/xfa/fgas/font/cfgas_pdffontmgr.cpp
+++ b/xfa/fgas/font/cfgas_pdffontmgr.cpp
@@ -62,7 +62,7 @@
       continue;
     }
     CPDF_Dictionary* pFontDict = ToDictionary(pObj->GetDirect());
-    if (!pFontDict || pFontDict->GetStringFor("Type") != "Font")
+    if (!pFontDict || pFontDict->GetNameFor("Type") != "Font")
       return nullptr;
 
     RetainPtr<CPDF_Font> pPDFFont = pData->GetFont(pFontDict);