Combine some CPDF_DIB logic as ExtractDictWidthFail().

As a consequence, we get a null ptr check for free that was
theoretically missing.

Change-Id: I297ea50bac517a6b512e8fb9238f382c0084a648
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/92332
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fpdfapi/page/cpdf_dib.cpp b/core/fpdfapi/page/cpdf_dib.cpp
index 9d1fe4f..0149b2e 100644
--- a/core/fpdfapi/page/cpdf_dib.cpp
+++ b/core/fpdfapi/page/cpdf_dib.cpp
@@ -162,16 +162,7 @@
 CPDF_DIB::JpxSMaskInlineData::~JpxSMaskInlineData() = default;
 
 bool CPDF_DIB::Load() {
-  if (!m_pStream)
-    return false;
-
-  m_pDict.Reset(m_pStream->GetDict());
-  if (!m_pDict)
-    return false;
-
-  m_Width = m_pDict->GetIntegerFor("Width");
-  m_Height = m_pDict->GetIntegerFor("Height");
-  if (!IsValidDimension(m_Width) || !IsValidDimension(m_Height))
+  if (!ExtractDictWidthHeight())
     return false;
 
   m_GroupFamily = CPDF_ColorSpace::Family::kUnknown;
@@ -262,19 +253,14 @@
     bool bStdCS,
     CPDF_ColorSpace::Family GroupFamily,
     bool bLoadMask) {
-  if (!m_pStream)
+  if (!ExtractDictWidthHeight())
     return LoadState::kFail;
 
-  m_pDict.Reset(m_pStream->GetDict());
   m_bStdCS = bStdCS;
   m_bHasMask = bHasMask;
-  m_Width = m_pDict->GetIntegerFor("Width");
-  m_Height = m_pDict->GetIntegerFor("Height");
-  if (!IsValidDimension(m_Width) || !IsValidDimension(m_Height))
-    return LoadState::kFail;
-
   m_GroupFamily = GroupFamily;
   m_bLoadMask = bLoadMask;
+
   if (!LoadColorInfo(m_pStream->IsInline() ? pFormResources : nullptr,
                      pPageResources)) {
     return LoadState::kFail;
@@ -767,6 +753,19 @@
   return result_bitmap;
 }
 
+bool CPDF_DIB::ExtractDictWidthHeight() {
+  if (!m_pStream)
+    return false;
+
+  m_pDict.Reset(m_pStream->GetDict());
+  if (!m_pDict)
+    return false;
+
+  m_Width = m_pDict->GetIntegerFor("Width");
+  m_Height = m_pDict->GetIntegerFor("Height");
+  return IsValidDimension(m_Width) && IsValidDimension(m_Height);
+}
+
 CPDF_DIB::LoadState CPDF_DIB::StartLoadMask() {
   m_MatteColor = 0XFFFFFFFF;
 
diff --git a/core/fpdfapi/page/cpdf_dib.h b/core/fpdfapi/page/cpdf_dib.h
index ca7b941..893945a 100644
--- a/core/fpdfapi/page/cpdf_dib.h
+++ b/core/fpdfapi/page/cpdf_dib.h
@@ -75,6 +75,7 @@
     std::vector<uint8_t, FxAllocAllocator<uint8_t>> data;
   };
 
+  bool ExtractDictWidthHeight();
   LoadState StartLoadMask();
   LoadState StartLoadMaskDIB(RetainPtr<const CPDF_Stream> mask_stream);
   bool ContinueToLoadMask();