Check for errors first in LoadSimpleFont() and LoadCompositeFont()

Check the input font and handle invalid cases first. Mark some variables
as const along the way.

Change-Id: If43ba8f5d9d55a9133d128e6107e1431c2d973aa
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/115830
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Reviewed-by: Thomas Sepez <tsepez@google.com>
diff --git a/fpdfsdk/fpdf_edittext.cpp b/fpdfsdk/fpdf_edittext.cpp
index aa1e18b..5a3238d 100644
--- a/fpdfsdk/fpdf_edittext.cpp
+++ b/fpdfsdk/fpdf_edittext.cpp
@@ -303,13 +303,6 @@
                                     std::unique_ptr<CFX_Font> font,
                                     pdfium::span<const uint8_t> font_data,
                                     int font_type) {
-  auto font_dict = doc->NewIndirect<CPDF_Dictionary>();
-  font_dict->SetNewFor<CPDF_Name>("Type", "Font");
-  font_dict->SetNewFor<CPDF_Name>(
-      "Subtype", font_type == FPDF_FONT_TYPE1 ? "Type1" : "TrueType");
-  ByteString name = BaseFontNameForType(font.get(), font_type);
-  font_dict->SetNewFor<CPDF_Name>("BaseFont", name);
-
   // If it doesn't have a single char, just fail.
   RetainPtr<CFX_Face> face = font->GetFace();
   if (face->GetGlyphCount() <= 0) {
@@ -324,6 +317,13 @@
     return nullptr;
   }
 
+  auto font_dict = doc->NewIndirect<CPDF_Dictionary>();
+  font_dict->SetNewFor<CPDF_Name>("Type", "Font");
+  font_dict->SetNewFor<CPDF_Name>(
+      "Subtype", font_type == FPDF_FONT_TYPE1 ? "Type1" : "TrueType");
+  const ByteString name = BaseFontNameForType(font.get(), font_type);
+  font_dict->SetNewFor<CPDF_Name>("BaseFont", name);
+
   font_dict->SetNewFor<CPDF_Number>(
       "FirstChar", static_cast<int>(char_codes_and_indices[0].char_code));
   auto widths_array = doc->NewIndirect<CPDF_Array>();
@@ -353,13 +353,25 @@
                                        std::unique_ptr<CFX_Font> font,
                                        pdfium::span<const uint8_t> font_data,
                                        int font_type) {
+  // If it doesn't have a single char, just fail.
+  RetainPtr<CFX_Face> face = font->GetFace();
+  if (face->GetGlyphCount() <= 0) {
+    return nullptr;
+  }
+
+  auto char_codes_and_indices =
+      face->GetCharCodesAndIndices(pdfium::kMaximumSupplementaryCodePoint);
+  if (char_codes_and_indices.empty()) {
+    return nullptr;
+  }
+
   auto font_dict = doc->NewIndirect<CPDF_Dictionary>();
   font_dict->SetNewFor<CPDF_Name>("Type", "Font");
   font_dict->SetNewFor<CPDF_Name>("Subtype", "Type0");
   // TODO(npm): Get the correct encoding, if it's not identity.
   ByteString encoding = "Identity-H";
   font_dict->SetNewFor<CPDF_Name>("Encoding", encoding);
-  ByteString name = BaseFontNameForType(font.get(), font_type);
+  const ByteString name = BaseFontNameForType(font.get(), font_type);
   font_dict->SetNewFor<CPDF_Name>(
       "BaseFont", font_type == FPDF_FONT_TYPE1 ? name + "-" + encoding : name);
 
@@ -384,18 +396,6 @@
   cid_font_dict->SetNewFor<CPDF_Reference>("FontDescriptor", doc,
                                            font_descriptor_dict->GetObjNum());
 
-  // If it doesn't have a single char, just fail.
-  RetainPtr<CFX_Face> face = font->GetFace();
-  if (face->GetGlyphCount() <= 0) {
-    return nullptr;
-  }
-
-  auto char_codes_and_indices =
-      face->GetCharCodesAndIndices(pdfium::kMaximumSupplementaryCodePoint);
-  if (char_codes_and_indices.empty()) {
-    return nullptr;
-  }
-
   std::multimap<uint32_t, uint32_t> to_unicode;
   std::map<uint32_t, uint32_t> widths;
   for (const auto& item : char_codes_and_indices) {