Remove most direct CheckedNumeric usage.

Use types defined in fx_safe_types.h instead.

Change-Id: I35f21a7066da0a2ba2a2993fd26a5b0c0ac59ef1
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/69071
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/core/fpdfapi/font/cpdf_cidfont.cpp b/core/fpdfapi/font/cpdf_cidfont.cpp
index 0c02b9e..306655b 100644
--- a/core/fpdfapi/font/cpdf_cidfont.cpp
+++ b/core/fpdfapi/font/cpdf_cidfont.cpp
@@ -21,8 +21,8 @@
 #include "core/fpdfapi/parser/cpdf_array.h"
 #include "core/fpdfapi/parser/cpdf_dictionary.h"
 #include "core/fpdfapi/parser/cpdf_stream_acc.h"
+#include "core/fxcrt/fx_safe_types.h"
 #include "core/fxge/fx_font.h"
-#include "third_party/base/numerics/safe_math.h"
 #include "third_party/base/ptr_util.h"
 #include "third_party/base/span.h"
 #include "third_party/base/stl_util.h"
@@ -753,7 +753,7 @@
 }
 
 void CPDF_CIDFont::LoadSubstFont() {
-  pdfium::base::CheckedNumeric<int> safeStemV(m_StemV);
+  FX_SAFE_INT32 safeStemV(m_StemV);
   safeStemV *= 5;
   m_Font.LoadSubst(m_BaseFontName, !m_bType1, m_Flags,
                    safeStemV.ValueOrDefault(FXFONT_FW_NORMAL), m_ItalicAngle,
diff --git a/core/fpdfapi/font/cpdf_font.cpp b/core/fpdfapi/font/cpdf_font.cpp
index 326bc6e..df7ef69 100644
--- a/core/fpdfapi/font/cpdf_font.cpp
+++ b/core/fpdfapi/font/cpdf_font.cpp
@@ -26,6 +26,7 @@
 #include "core/fpdfapi/parser/cpdf_stream.h"
 #include "core/fpdfapi/parser/cpdf_stream_acc.h"
 #include "core/fxcrt/fx_memory.h"
+#include "core/fxcrt/fx_safe_types.h"
 #include "core/fxge/cfx_fontmapper.h"
 #include "core/fxge/fx_font.h"
 #include "core/fxge/fx_freetype.h"
@@ -380,7 +381,7 @@
 uint32_t CPDF_Font::FallbackFontFromCharcode(uint32_t charcode) {
   if (m_FontFallbacks.empty()) {
     m_FontFallbacks.push_back(pdfium::MakeUnique<CFX_Font>());
-    pdfium::base::CheckedNumeric<int> safeWeight = m_StemV;
+    FX_SAFE_INT32 safeWeight = m_StemV;
     safeWeight *= 5;
     m_FontFallbacks[0]->LoadSubst("Arial", IsTrueTypeFont(), m_Flags,
                                   safeWeight.ValueOrDefault(FXFONT_FW_NORMAL),
@@ -437,7 +438,7 @@
 }
 
 int CPDF_Font::GetFontWeight() const {
-  pdfium::base::CheckedNumeric<int> safeStemV(m_StemV);
+  FX_SAFE_INT32 safeStemV(m_StemV);
   if (m_StemV < 140)
     safeStemV *= 5;
   else
diff --git a/core/fpdfapi/parser/fpdf_parser_decode.cpp b/core/fpdfapi/parser/fpdf_parser_decode.cpp
index f1bcb28..9a12065 100644
--- a/core/fpdfapi/parser/fpdf_parser_decode.cpp
+++ b/core/fpdfapi/parser/fpdf_parser_decode.cpp
@@ -23,7 +23,6 @@
 #include "core/fxcodec/scanlinedecoder.h"
 #include "core/fxcrt/fx_extension.h"
 #include "core/fxcrt/fx_safe_types.h"
-#include "third_party/base/numerics/safe_math.h"
 #include "third_party/base/stl_util.h"
 
 namespace {
@@ -42,7 +41,7 @@
   if (Colors < 0 || BitsPerComponent < 0 || Columns < 0)
     return false;
 
-  pdfium::base::CheckedNumeric<int> check = Columns;
+  FX_SAFE_INT32 check = Columns;
   check *= Colors;
   check *= BitsPerComponent;
   if (!check.IsValid())
diff --git a/core/fpdfapi/render/cpdf_rendershading.cpp b/core/fpdfapi/render/cpdf_rendershading.cpp
index 427c9d5..03adb88 100644
--- a/core/fpdfapi/render/cpdf_rendershading.cpp
+++ b/core/fpdfapi/render/cpdf_rendershading.cpp
@@ -603,7 +603,7 @@
 };
 
 int Interpolate(int p1, int p2, int delta1, int delta2, bool* overflow) {
-  pdfium::base::CheckedNumeric<int> p = p2;
+  FX_SAFE_INT32 p = p2;
   p -= p1;
   p *= delta1;
   p /= delta2;
diff --git a/core/fpdfapi/render/cpdf_renderstatus.cpp b/core/fpdfapi/render/cpdf_renderstatus.cpp
index b9294ff..58ce613 100644
--- a/core/fpdfapi/render/cpdf_renderstatus.cpp
+++ b/core/fpdfapi/render/cpdf_renderstatus.cpp
@@ -63,7 +63,6 @@
 #include "core/fxge/text_glyph_pos.h"
 #include "third_party/base/compiler_specific.h"
 #include "third_party/base/logging.h"
-#include "third_party/base/numerics/safe_math.h"
 #include "third_party/base/ptr_util.h"
 #include "third_party/base/stl_util.h"
 
@@ -1361,10 +1360,8 @@
         CFX_PointF original = mtPattern2Device.Transform(
             CFX_PointF(col * pPattern->x_step(), row * pPattern->y_step()));
 
-        pdfium::base::CheckedNumeric<int> safeStartX =
-            FXSYS_roundf(original.x + left_offset);
-        pdfium::base::CheckedNumeric<int> safeStartY =
-            FXSYS_roundf(original.y + top_offset);
+        FX_SAFE_INT32 safeStartX = FXSYS_roundf(original.x + left_offset);
+        FX_SAFE_INT32 safeStartY = FXSYS_roundf(original.y + top_offset);
 
         safeStartX -= clip_box.left;
         safeStartY -= clip_box.top;
diff --git a/core/fxcodec/jbig2/JBig2_ArithIntDecoder.cpp b/core/fxcodec/jbig2/JBig2_ArithIntDecoder.cpp
index d006e74..efef77e 100644
--- a/core/fxcodec/jbig2/JBig2_ArithIntDecoder.cpp
+++ b/core/fxcodec/jbig2/JBig2_ArithIntDecoder.cpp
@@ -9,7 +9,7 @@
 #include <vector>
 
 #include "core/fxcrt/fx_memory.h"
-#include "third_party/base/numerics/safe_math.h"
+#include "core/fxcrt/fx_safe_types.h"
 
 namespace {
 
@@ -68,8 +68,7 @@
       PREV = (PREV & 511) | 256;
     nTemp = ShiftOr(nTemp, D);
   }
-  pdfium::base::CheckedNumeric<int> safeValue =
-      g_ArithIntDecodeData[nDecodeDataIndex].nValue;
+  FX_SAFE_INT32 safeValue = g_ArithIntDecodeData[nDecodeDataIndex].nValue;
   safeValue += nTemp;
 
   // Value does not fit in int.
diff --git a/core/fxcodec/jbig2/JBig2_Context.cpp b/core/fxcodec/jbig2/JBig2_Context.cpp
index d9c9ee8..cfdeec4 100644
--- a/core/fxcodec/jbig2/JBig2_Context.cpp
+++ b/core/fxcodec/jbig2/JBig2_Context.cpp
@@ -1221,7 +1221,7 @@
   LENCOUNT[0] = 0;
 
   for (int i = 1; i <= LENMAX; ++i) {
-    pdfium::base::CheckedNumeric<int> shifted = FIRSTCODE[i - 1];
+    FX_SAFE_INT32 shifted = FIRSTCODE[i - 1];
     shifted += LENCOUNT[i - 1];
     shifted <<= 1;
     if (!shifted.IsValid())
diff --git a/core/fxcodec/jbig2/JBig2_HuffmanTable.cpp b/core/fxcodec/jbig2/JBig2_HuffmanTable.cpp
index 392b718..547cfd4 100644
--- a/core/fxcodec/jbig2/JBig2_HuffmanTable.cpp
+++ b/core/fxcodec/jbig2/JBig2_HuffmanTable.cpp
@@ -12,7 +12,7 @@
 #include "core/fxcodec/jbig2/JBig2_BitStream.h"
 #include "core/fxcodec/jbig2/JBig2_Context.h"
 #include "core/fxcrt/fx_memory.h"
-#include "third_party/base/numerics/safe_math.h"
+#include "core/fxcrt/fx_safe_types.h"
 
 namespace {
 
@@ -180,7 +180,7 @@
     return false;
 
   ExtendBuffers(false);
-  pdfium::base::CheckedNumeric<int> cur_low = low;
+  FX_SAFE_INT32 cur_low = low;
   do {
     if ((pStream->readNBits(HTPS, &CODES[NTEMP].codelen) == -1) ||
         (pStream->readNBits(HTRS, &RANGELEN[NTEMP]) == -1) ||
diff --git a/core/fxcodec/progressivedecoder.cpp b/core/fxcodec/progressivedecoder.cpp
index 73fdf2a..5682f01 100644
--- a/core/fxcodec/progressivedecoder.cpp
+++ b/core/fxcodec/progressivedecoder.cpp
@@ -20,7 +20,6 @@
 #include "core/fxge/dib/cfx_dibitmap.h"
 #include "core/fxge/fx_dib.h"
 #include "third_party/base/logging.h"
-#include "third_party/base/numerics/safe_math.h"
 #include "third_party/base/ptr_util.h"
 
 namespace fxcodec {
@@ -608,7 +607,7 @@
   uint32_t dest_ScanOffet = m_startX * dest_Bpp;
   int dest_top = m_startY;
   int dest_bottom = m_startY + m_sizeY;
-  pdfium::base::CheckedNumeric<int> check_dest_row_1 = dest_row;
+  FX_SAFE_INT32 check_dest_row_1 = dest_row;
   check_dest_row_1 += pdfium::base::checked_cast<int>(scale_y);
   int dest_row_1 = check_dest_row_1.ValueOrDie();
   if (dest_row_1 >= dest_bottom - 1) {
@@ -929,7 +928,7 @@
   int dest_top = m_startY;
   pdfium::base::CheckedNumeric<double> scale_y2 = scale_y;
   scale_y2 *= 2;
-  pdfium::base::CheckedNumeric<int> check_dest_row_1 = dest_row;
+  FX_SAFE_INT32 check_dest_row_1 = dest_row;
   check_dest_row_1 -= scale_y2.ValueOrDie();
   int dest_row_1 = check_dest_row_1.ValueOrDie();
   dest_row_1 = std::max(dest_row_1, dest_top);
@@ -2028,7 +2027,7 @@
   int dest_Bpp = pDeviceBitmap->GetBPP() >> 3;
   uint32_t dest_ScanOffet = m_startX * dest_Bpp;
   int dest_top = m_startY;
-  pdfium::base::CheckedNumeric<int> check_dest_row_1 = dest_row;
+  FX_SAFE_INT32 check_dest_row_1 = dest_row;
   check_dest_row_1 -= pdfium::base::checked_cast<int>(scale_y);
   int dest_row_1 = check_dest_row_1.ValueOrDie();
   if (dest_row_1 < dest_top) {
diff --git a/core/fxcrt/fileaccess_iface.h b/core/fxcrt/fileaccess_iface.h
index c744b54..8493ecb 100644
--- a/core/fxcrt/fileaccess_iface.h
+++ b/core/fxcrt/fileaccess_iface.h
@@ -9,7 +9,6 @@
 
 #include <memory>
 
-#include "core/fxcrt/fx_safe_types.h"
 #include "core/fxcrt/fx_string.h"
 
 class FileAccessIface {
diff --git a/core/fxcrt/fx_coordinates.cpp b/core/fxcrt/fx_coordinates.cpp
index 7afd9f3..ccbc1f6 100644
--- a/core/fxcrt/fx_coordinates.cpp
+++ b/core/fxcrt/fx_coordinates.cpp
@@ -12,7 +12,6 @@
 #include "core/fxcrt/fx_extension.h"
 #include "core/fxcrt/fx_memory.h"
 #include "core/fxcrt/fx_safe_types.h"
-#include "third_party/base/numerics/safe_conversions.h"
 
 namespace {
 
@@ -56,6 +55,14 @@
 
 }  // namespace
 
+bool FX_RECT::Valid() const {
+  FX_SAFE_INT32 w = right;
+  FX_SAFE_INT32 h = bottom;
+  w -= left;
+  h -= top;
+  return w.IsValid() && h.IsValid();
+}
+
 void FX_RECT::Normalize() {
   if (left > right)
     std::swap(left, right);
diff --git a/core/fxcrt/fx_coordinates.h b/core/fxcrt/fx_coordinates.h
index e8d1a8a..2eb0279 100644
--- a/core/fxcrt/fx_coordinates.h
+++ b/core/fxcrt/fx_coordinates.h
@@ -10,7 +10,6 @@
 #include <algorithm>
 
 #include "core/fxcrt/fx_system.h"
-#include "third_party/base/numerics/safe_math.h"
 
 #ifndef NDEBUG
 #include <ostream>
@@ -188,13 +187,7 @@
   int Height() const { return bottom - top; }
   bool IsEmpty() const { return right <= left || bottom <= top; }
 
-  bool Valid() const {
-    pdfium::base::CheckedNumeric<int> w = right;
-    pdfium::base::CheckedNumeric<int> h = bottom;
-    w -= left;
-    h -= top;
-    return w.IsValid() && h.IsValid();
-  }
+  bool Valid() const;
 
   void Normalize();
   void Intersect(const FX_RECT& src);
diff --git a/core/fxcrt/string_data_template.cpp b/core/fxcrt/string_data_template.cpp
index d884869..825dcd6 100644
--- a/core/fxcrt/string_data_template.cpp
+++ b/core/fxcrt/string_data_template.cpp
@@ -7,8 +7,8 @@
 #include "core/fxcrt/string_data_template.h"
 
 #include "core/fxcrt/fx_memory.h"
+#include "core/fxcrt/fx_safe_types.h"
 #include "third_party/base/allocator/partition_allocator/partition_alloc.h"
-#include "third_party/base/numerics/safe_math.h"
 
 namespace fxcrt {
 
@@ -21,7 +21,7 @@
   // Calculate space needed for the fixed portion of the struct plus the
   // NUL char that is not included in |m_nAllocLength|.
   int overhead = offsetof(StringDataTemplate, m_String) + sizeof(CharType);
-  pdfium::base::CheckedNumeric<size_t> nSize = nLen;
+  FX_SAFE_SIZE_T nSize = nLen;
   nSize *= sizeof(CharType);
   nSize += overhead;
 
diff --git a/core/fxge/dib/cfx_dibbase.cpp b/core/fxge/dib/cfx_dibbase.cpp
index 4a4223a..2ad7465 100644
--- a/core/fxge/dib/cfx_dibbase.cpp
+++ b/core/fxge/dib/cfx_dibbase.cpp
@@ -11,6 +11,7 @@
 #include <utility>
 #include <vector>
 
+#include "core/fxcrt/fx_safe_types.h"
 #include "core/fxge/cfx_cliprgn.h"
 #include "core/fxge/dib/cfx_bitmapstorer.h"
 #include "core/fxge/dib/cfx_cmyk_to_srgb.h"
@@ -902,13 +903,13 @@
   dest_left = dest_rect.left;
   dest_top = dest_rect.top;
 
-  pdfium::base::CheckedNumeric<int> safe_src_left = dest_left;
+  FX_SAFE_INT32 safe_src_left = dest_left;
   safe_src_left -= x_offset;
   if (!safe_src_left.IsValid())
     return false;
   src_left = safe_src_left.ValueOrDie();
 
-  pdfium::base::CheckedNumeric<int> safe_src_top = dest_top;
+  FX_SAFE_INT32 safe_src_top = dest_top;
   safe_src_top -= y_offset;
   if (!safe_src_top.IsValid())
     return false;
diff --git a/core/fxge/win32/cfx_psrenderer.cpp b/core/fxge/win32/cfx_psrenderer.cpp
index f9f7de2..f215de6 100644
--- a/core/fxge/win32/cfx_psrenderer.cpp
+++ b/core/fxge/win32/cfx_psrenderer.cpp
@@ -7,6 +7,7 @@
 #include "core/fxge/win32/cfx_psrenderer.h"
 
 #include <algorithm>
+#include <cmath>
 #include <memory>
 #include <sstream>
 #include <utility>
diff --git a/fpdfsdk/fpdf_structtree.cpp b/fpdfsdk/fpdf_structtree.cpp
index cfd57a8..e6d92df 100644
--- a/fpdfsdk/fpdf_structtree.cpp
+++ b/fpdfsdk/fpdf_structtree.cpp
@@ -10,6 +10,7 @@
 #include "core/fpdfapi/parser/cpdf_dictionary.h"
 #include "core/fpdfdoc/cpdf_structelement.h"
 #include "core/fpdfdoc/cpdf_structtree.h"
+#include "core/fxcrt/fx_safe_types.h"
 #include "fpdfsdk/cpdfsdk_helpers.h"
 
 namespace {
@@ -53,7 +54,7 @@
   if (!tree)
     return -1;
 
-  pdfium::base::CheckedNumeric<int> tmp_size = tree->CountTopElements();
+  FX_SAFE_INT32 tmp_size = tree->CountTopElements();
   return tmp_size.ValueOrDefault(-1);
 }
 
@@ -113,7 +114,7 @@
   if (!elem)
     return -1;
 
-  pdfium::base::CheckedNumeric<int> tmp_size = elem->CountKids();
+  FX_SAFE_INT32 tmp_size = elem->CountKids();
   return tmp_size.ValueOrDefault(-1);
 }
 
diff --git a/fpdfsdk/fpdf_view.cpp b/fpdfsdk/fpdf_view.cpp
index 7266301..9bfd15d 100644
--- a/fpdfsdk/fpdf_view.cpp
+++ b/fpdfsdk/fpdf_view.cpp
@@ -29,6 +29,7 @@
 #include "core/fpdfdoc/cpdf_nametree.h"
 #include "core/fpdfdoc/cpdf_viewerpreferences.h"
 #include "core/fxcrt/cfx_readonlymemorystream.h"
+#include "core/fxcrt/fx_safe_types.h"
 #include "core/fxcrt/fx_stream.h"
 #include "core/fxcrt/fx_system.h"
 #include "core/fxcrt/unowned_ptr.h"
@@ -1003,8 +1004,7 @@
     return 0;
 
   auto name_tree = CPDF_NameTree::Create(pDoc, "Dests");
-  pdfium::base::CheckedNumeric<FPDF_DWORD> count =
-      name_tree ? name_tree->GetCount() : 0;
+  FX_SAFE_UINT32 count = name_tree ? name_tree->GetCount() : 0;
   const CPDF_Dictionary* pDest = pRoot->GetDictFor("Dests");
   if (pDest)
     count += pDest->size();
@@ -1115,7 +1115,7 @@
     if (!pDest)
       return nullptr;
 
-    pdfium::base::CheckedNumeric<int> checked_count = count;
+    FX_SAFE_INT32 checked_count = count;
     checked_count += pDest->size();
     if (!checked_count.IsValid() || index >= checked_count.ValueOrDie())
       return nullptr;
diff --git a/xfa/fxfa/parser/cxfa_box.cpp b/xfa/fxfa/parser/cxfa_box.cpp
index 5ba01d3..d0b52e9 100644
--- a/xfa/fxfa/parser/cxfa_box.cpp
+++ b/xfa/fxfa/parser/cxfa_box.cpp
@@ -7,6 +7,7 @@
 #include "xfa/fxfa/parser/cxfa_box.h"
 
 #include <algorithm>
+#include <cmath>
 #include <utility>
 
 #include "fxjs/xfa/cjx_object.h"
diff --git a/xfa/fxfa/parser/cxfa_rectangle.cpp b/xfa/fxfa/parser/cxfa_rectangle.cpp
index 632141d..f000022 100644
--- a/xfa/fxfa/parser/cxfa_rectangle.cpp
+++ b/xfa/fxfa/parser/cxfa_rectangle.cpp
@@ -6,6 +6,7 @@
 
 #include "xfa/fxfa/parser/cxfa_rectangle.h"
 
+#include <cmath>
 #include <utility>
 
 #include "core/fxge/render_defines.h"