Consolidate dimension checking code inside cpdf_dib.cpp.

Create IsValidDimension() and use it within CPDF_DIB in multiple places
that need to do the same check.

Change-Id: Id2a67f0e890905e3abfcfcc919891011e2447261
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/65612
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/core/fpdfapi/page/cpdf_dib.cpp b/core/fpdfapi/page/cpdf_dib.cpp
index 9e73072..2d9919d 100644
--- a/core/fpdfapi/page/cpdf_dib.cpp
+++ b/core/fpdfapi/page/cpdf_dib.cpp
@@ -37,7 +37,10 @@
 
 namespace {
 
-constexpr int kMaxImageDimension = 0x01FFFF;
+bool IsValidDimension(int value) {
+  constexpr int kMaxImageDimension = 0x01FFFF;
+  return value > 0 && value <= kMaxImageDimension;
+}
 
 unsigned int GetBits8(const uint8_t* pData, uint64_t bitpos, size_t nbits) {
   ASSERT(nbits == 1 || nbits == 2 || nbits == 4 || nbits == 8 || nbits == 16);
@@ -160,10 +163,9 @@
   m_pStream.Reset(pStream);
   m_Width = m_pDict->GetIntegerFor("Width");
   m_Height = m_pDict->GetIntegerFor("Height");
-  if (m_Width <= 0 || m_Height <= 0 || m_Width > kMaxImageDimension ||
-      m_Height > kMaxImageDimension) {
+  if (!IsValidDimension(m_Width) || !IsValidDimension(m_Height))
     return false;
-  }
+
   m_GroupFamily = 0;
   m_bLoadMask = false;
   if (!LoadColorInfo(nullptr, nullptr))
@@ -259,10 +261,9 @@
   m_bHasMask = bHasMask;
   m_Width = m_pDict->GetIntegerFor("Width");
   m_Height = m_pDict->GetIntegerFor("Height");
-  if (m_Width <= 0 || m_Height <= 0 || m_Width > kMaxImageDimension ||
-      m_Height > kMaxImageDimension) {
+  if (!IsValidDimension(m_Width) || !IsValidDimension(m_Height))
     return LoadState::kFail;
-  }
+
   m_GroupFamily = GroupFamily;
   m_bLoadMask = bLoadMask;
   if (!LoadColorInfo(m_pStream->IsInline() ? pFormResources : nullptr,