Replace Vector2D with Fx2DSizeOrDie().
Just pass the Fx2DSizeOrDie() to the vector ctor instead of having a
helper function for constructing the vector. Switch some std::vectors to
DataVector along the way.
Change-Id: I7f51e20fc28a8413f3320a4c541fb210f6de46a2
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/97892
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/core/fpdfapi/page/cpdf_colorspace.cpp b/core/fpdfapi/page/cpdf_colorspace.cpp
index c31c8ef..e83706a 100644
--- a/core/fpdfapi/page/cpdf_colorspace.cpp
+++ b/core/fpdfapi/page/cpdf_colorspace.cpp
@@ -33,6 +33,7 @@
 #include "core/fpdfapi/parser/fpdf_parser_utility.h"
 #include "core/fxcodec/fx_codec.h"
 #include "core/fxcrt/data_vector.h"
+#include "core/fxcrt/fx_memory.h"
 #include "core/fxcrt/fx_memory_wrappers.h"
 #include "core/fxcrt/fx_safe_types.h"
 #include "core/fxcrt/maybe_owned.h"
@@ -998,10 +999,8 @@
     return;
   }
   if (m_pCache.empty()) {
-    m_pCache =
-        fxcrt::Vector2D<uint8_t, FxAllocAllocator<uint8_t>>(nMaxColors, 3);
-    auto temp_src = fxcrt::Vector2D<uint8_t, FxAllocAllocator<uint8_t>>(
-        nMaxColors, nComponents);
+    m_pCache.resize(Fx2DSizeOrDie(nMaxColors, 3));
+    DataVector<uint8_t> temp_src(Fx2DSizeOrDie(nMaxColors, nComponents));
     size_t src_index = 0;
     for (int i = 0; i < nMaxColors; i++) {
       uint32_t color = i;
diff --git a/core/fpdfapi/page/cpdf_expintfunc.cpp b/core/fpdfapi/page/cpdf_expintfunc.cpp
index c0ac4cd..167ac7f 100644
--- a/core/fpdfapi/page/cpdf_expintfunc.cpp
+++ b/core/fpdfapi/page/cpdf_expintfunc.cpp
@@ -11,6 +11,8 @@
 #include "core/fpdfapi/parser/cpdf_array.h"
 #include "core/fpdfapi/parser/cpdf_dictionary.h"
 #include "core/fpdfapi/parser/cpdf_number.h"
+#include "core/fxcrt/data_vector.h"
+#include "core/fxcrt/fx_memory.h"
 #include "core/fxcrt/fx_safe_types.h"
 #include "core/fxcrt/stl_util.h"
 
@@ -38,8 +40,8 @@
     m_nOutputs = 1;
 
   const CPDF_Array* pArray1 = pDict->GetArrayFor("C1");
-  m_BeginValues = fxcrt::Vector2D<float>(m_nOutputs, 2);
-  m_EndValues = fxcrt::Vector2D<float>(m_nOutputs, 2);
+  m_BeginValues = DataVector<float>(Fx2DSizeOrDie(m_nOutputs, 2));
+  m_EndValues = DataVector<float>(m_BeginValues.size());
   for (uint32_t i = 0; i < m_nOutputs; i++) {
     m_BeginValues[i] = pArray0 ? pArray0->GetFloatAt(i) : 0.0f;
     m_EndValues[i] = pArray1 ? pArray1->GetFloatAt(i) : 1.0f;
diff --git a/core/fpdfapi/page/cpdf_expintfunc.h b/core/fpdfapi/page/cpdf_expintfunc.h
index d829456..4dd2529 100644
--- a/core/fpdfapi/page/cpdf_expintfunc.h
+++ b/core/fpdfapi/page/cpdf_expintfunc.h
@@ -8,9 +8,9 @@
 #define CORE_FPDFAPI_PAGE_CPDF_EXPINTFUNC_H_
 
 #include <set>
-#include <vector>
 
 #include "core/fpdfapi/page/cpdf_function.h"
+#include "core/fxcrt/data_vector.h"
 
 #if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
 #include "third_party/base/span.h"
@@ -38,8 +38,8 @@
  private:
   uint32_t m_nOrigOutputs = 0;
   float m_Exponent = 0.0f;
-  std::vector<float> m_BeginValues;
-  std::vector<float> m_EndValues;
+  DataVector<float> m_BeginValues;
+  DataVector<float> m_EndValues;
 };
 
 #endif  // CORE_FPDFAPI_PAGE_CPDF_EXPINTFUNC_H_
diff --git a/core/fpdfapi/page/cpdf_indexedcs.cpp b/core/fpdfapi/page/cpdf_indexedcs.cpp
index bc112a5..73a3820 100644
--- a/core/fpdfapi/page/cpdf_indexedcs.cpp
+++ b/core/fpdfapi/page/cpdf_indexedcs.cpp
@@ -5,6 +5,7 @@
 #include "core/fpdfapi/page/cpdf_indexedcs.h"
 
 #include <set>
+#include <vector>
 
 #include "core/fpdfapi/page/cpdf_colorspace.h"
 #include "core/fpdfapi/page/cpdf_docpagedata.h"
@@ -14,9 +15,10 @@
 #include "core/fpdfapi/parser/cpdf_stream.h"
 #include "core/fpdfapi/parser/cpdf_stream_acc.h"
 #include "core/fpdfapi/parser/cpdf_string.h"
+#include "core/fxcrt/data_vector.h"
+#include "core/fxcrt/fx_memory.h"
 #include "core/fxcrt/fx_safe_types.h"
 #include "core/fxcrt/retain_ptr.h"
-#include "core/fxcrt/stl_util.h"
 #include "third_party/base/check_op.h"
 #include "third_party/base/span.h"
 
@@ -51,7 +53,7 @@
     return 0;
 
   m_nBaseComponents = m_pBaseCS->CountComponents();
-  m_pCompMinMax = fxcrt::Vector2D<float>(m_nBaseComponents, 2);
+  m_pCompMinMax = DataVector<float>(Fx2DSizeOrDie(m_nBaseComponents, 2));
   float defvalue;
   for (uint32_t i = 0; i < m_nBaseComponents; i++) {
     m_pBaseCS->GetDefaultValue(i, &defvalue, &m_pCompMinMax[i * 2],
diff --git a/core/fpdfapi/page/cpdf_indexedcs.h b/core/fpdfapi/page/cpdf_indexedcs.h
index 52a7527..ea2b7b8 100644
--- a/core/fpdfapi/page/cpdf_indexedcs.h
+++ b/core/fpdfapi/page/cpdf_indexedcs.h
@@ -6,10 +6,10 @@
 #define CORE_FPDFAPI_PAGE_CPDF_INDEXEDCS_H_
 
 #include <set>
-#include <vector>
 
 #include "core/fpdfapi/page/cpdf_basedcs.h"
 #include "core/fxcrt/bytestring.h"
+#include "core/fxcrt/data_vector.h"
 #include "core/fxcrt/retain_ptr.h"
 
 class CPDF_Document;
@@ -37,7 +37,7 @@
   uint32_t m_nBaseComponents = 0;
   int m_MaxIndex = 0;
   ByteString m_Table;
-  std::vector<float> m_pCompMinMax;
+  DataVector<float> m_pCompMinMax;
 };
 
 #endif  // CORE_FPDFAPI_PAGE_CPDF_INDEXEDCS_H_
diff --git a/core/fxcodec/fax/faxmodule.cpp b/core/fxcodec/fax/faxmodule.cpp
index b85c6a3..542a7dd 100644
--- a/core/fxcodec/fax/faxmodule.cpp
+++ b/core/fxcodec/fax/faxmodule.cpp
@@ -16,7 +16,7 @@
 #include "core/fxcodec/scanlinedecoder.h"
 #include "core/fxcrt/binary_buffer.h"
 #include "core/fxcrt/data_vector.h"
-#include "core/fxcrt/stl_util.h"
+#include "core/fxcrt/fx_memory.h"
 #include "core/fxge/calculate_pitch.h"
 #include "third_party/base/check.h"
 #include "third_party/base/check_op.h"
@@ -708,8 +708,7 @@
       m_Rows(height),
       m_Pitch(pitch),
       m_InitialRefLine(m_Pitch, 0xff),
-      m_LineBuf(
-          fxcrt::Vector2D<uint8_t, FxAllocAllocator<uint8_t>>(8, m_Pitch)),
+      m_LineBuf(Fx2DSizeOrDie(8, m_Pitch)),
       m_RefLineSpan(m_InitialRefLine) {
   m_DestBuf.SetAllocStep(10240);
 }
diff --git a/core/fxcrt/fx_memory.cpp b/core/fxcrt/fx_memory.cpp
index 542da04..ed5d111 100644
--- a/core/fxcrt/fx_memory.cpp
+++ b/core/fxcrt/fx_memory.cpp
@@ -13,6 +13,7 @@
 #include "build/build_config.h"
 #include "core/fxcrt/fx_safe_types.h"
 #include "third_party/base/debug/alias.h"
+#include "third_party/base/numerics/safe_math.h"
 
 #if BUILDFLAG(IS_WIN)
 #include <windows.h>
@@ -101,3 +102,9 @@
 }
 }  // namespace internal
 }  // namespace pdfium
+
+size_t Fx2DSizeOrDie(size_t w, size_t h) {
+  pdfium::base::CheckedNumeric<size_t> safe_size = w;
+  safe_size *= h;
+  return safe_size.ValueOrDie();
+}
diff --git a/core/fxcrt/fx_memory.h b/core/fxcrt/fx_memory.h
index f110a14..89b609c 100644
--- a/core/fxcrt/fx_memory.h
+++ b/core/fxcrt/fx_memory.h
@@ -106,6 +106,8 @@
   return (size + (N - 1)) & ~(N - 1);
 }
 
+size_t Fx2DSizeOrDie(size_t w, size_t h);
+
 #endif  // __cplusplus
 
 #endif  // CORE_FXCRT_FX_MEMORY_H_
diff --git a/core/fxcrt/stl_util.h b/core/fxcrt/stl_util.h
index 37156af..45a980e 100644
--- a/core/fxcrt/stl_util.h
+++ b/core/fxcrt/stl_util.h
@@ -6,10 +6,8 @@
 #define CORE_FXCRT_STL_UTIL_H_
 
 #include <memory>
-#include <vector>
 
 #include "third_party/base/numerics/safe_conversions.h"
-#include "third_party/base/numerics/safe_math.h"
 
 namespace fxcrt {
 
@@ -36,14 +34,6 @@
   return index >= 0 && index < CollectionSize<IndexType>(collection);
 }
 
-// Safely allocate a 1-dim vector big enough for |w| by |h| or die.
-template <typename T, typename A = std::allocator<T>>
-std::vector<T, A> Vector2D(size_t w, size_t h) {
-  pdfium::base::CheckedNumeric<size_t> safe_size = w;
-  safe_size *= h;
-  return std::vector<T, A>(safe_size.ValueOrDie());
-}
-
 }  // namespace fxcrt
 
 #endif  // CORE_FXCRT_STL_UTIL_H_
diff --git a/core/fxge/skia/fx_skia_device.cpp b/core/fxge/skia/fx_skia_device.cpp
index d153bc4..c9b9592 100644
--- a/core/fxge/skia/fx_skia_device.cpp
+++ b/core/fxge/skia/fx_skia_device.cpp
@@ -25,6 +25,7 @@
 #include "core/fpdfapi/parser/cpdf_stream_acc.h"
 #include "core/fxcrt/cfx_bitstream.h"
 #include "core/fxcrt/data_vector.h"
+#include "core/fxcrt/fx_memory.h"
 #include "core/fxcrt/fx_memory_wrappers.h"
 #include "core/fxcrt/fx_system.h"
 #include "core/fxcrt/stl_util.h"
@@ -419,8 +420,8 @@
   if (pFunc->GetOrigOutputs() != 3)
     return false;
 
-  auto begin_values = pFunc->GetBeginValues();
-  auto end_values = pFunc->GetEndValues();
+  pdfium::span<const float> begin_values = pFunc->GetBeginValues();
+  pdfium::span<const float> end_values = pFunc->GetEndValues();
   if (is_encode_reversed)
     std::swap(begin_values, end_values);
 
@@ -677,8 +678,7 @@
   int rowBytes = pSource->GetPitch();
   switch (pSource->GetBPP()) {
     case 1: {
-      dst8_storage =
-          fxcrt::Vector2D<uint8_t, FxAllocAllocator<uint8_t>>(width, height);
+      dst8_storage = DataVector<uint8_t>(Fx2DSizeOrDie(width, height));
       pdfium::span<uint8_t> dst8_pixels(dst8_storage);
       // By default, the two colors for grayscale are 0xFF and 0x00 unless they
       // are specified in the palette.
@@ -708,8 +708,7 @@
     case 8:
       // we upscale ctables to 32bit.
       if (pSource->HasPalette()) {
-        dst32_storage = fxcrt::Vector2D<uint32_t, FxAllocAllocator<uint32_t>>(
-            width, height);
+        dst32_storage = DataVector<uint32_t>(Fx2DSizeOrDie(width, height));
         pdfium::span<SkPMColor> dst32_pixels(dst32_storage);
         const size_t src_palette_size = pSource->GetRequiredPaletteSize();
         pdfium::span<const uint32_t> src_palette = pSource->GetPaletteSpan();
@@ -732,8 +731,7 @@
       }
       break;
     case 24: {
-      dst32_storage =
-          fxcrt::Vector2D<uint32_t, FxAllocAllocator<uint32_t>>(width, height);
+      dst32_storage = DataVector<uint32_t>(Fx2DSizeOrDie(width, height));
       pdfium::span<uint32_t> dst32_pixels(dst32_storage);
       for (int y = 0; y < height; ++y) {
         const uint8_t* srcRow =