Use CPDF_Dictionary::IsValidKey() in CPDFSDK_AppStream::AddImage().

Use the IsValidKey() check to avoid passing invalid keys into
CPDF_Dictionary.

Bug: chromium:1237898
Change-Id: I0c9d988b762a153a8e6e3f867d729f727d359bfa
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/83995
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/fpdfsdk/cpdfsdk_appstream.cpp b/fpdfsdk/cpdfsdk_appstream.cpp
index cc7d866..b1edb0e 100644
--- a/fpdfsdk/cpdfsdk_appstream.cpp
+++ b/fpdfsdk/cpdfsdk_appstream.cpp
@@ -1835,10 +1835,11 @@
   CPDF_Dictionary* pStreamDict = pStream->GetDict();
   ByteString sImageAlias = "IMG";
 
-  if (CPDF_Dictionary* pImageDict = pImage->GetDict()) {
-    sImageAlias = pImageDict->GetStringFor("Name");
-    if (sImageAlias.IsEmpty())
-      sImageAlias = "IMG";
+  CPDF_Dictionary* pImageDict = pImage->GetDict();
+  if (pImageDict) {
+    ByteString image_name = pImageDict->GetStringFor("Name");
+    if (CPDF_Dictionary::IsValidKey(image_name))
+      sImageAlias = std::move(image_name);
   }
 
   CPDF_Dictionary* pStreamResList = pStreamDict->GetDictFor("Resources");