Mark unsafe regions in fgas/layout files.

-- Convert 2d arrays using 'typedefs' for readability.

Bug: 42271175
Change-Id: Ie50ce43e8bce61024bf5641a4a6c37017f6fcc2a
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/119571
Reviewed-by: Lei Zhang <thestig@chromium.org>
Reviewed-by: Thomas Sepez <tsepez@google.com>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fxcrt/stl_util.h b/core/fxcrt/stl_util.h
index b956f7e..4925039 100644
--- a/core/fxcrt/stl_util.h
+++ b/core/fxcrt/stl_util.h
@@ -6,6 +6,7 @@
 #define CORE_FXCRT_STL_UTIL_H_
 
 #include <algorithm>
+#include <array>
 #include <memory>
 
 #include "core/fxcrt/compiler_specific.h"
diff --git a/xfa/fgas/layout/cfgas_char.cpp b/xfa/fgas/layout/cfgas_char.cpp
index 64edbfc..68b27c9 100644
--- a/xfa/fgas/layout/cfgas_char.cpp
+++ b/xfa/fgas/layout/cfgas_char.cpp
@@ -4,14 +4,10 @@
 
 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
 
-#if defined(UNSAFE_BUFFERS_BUILD)
-// TODO(crbug.com/pdfium/2154): resolve buffer safety issues.
-#pragma allow_unsafe_buffers
-#endif
-
 #include "xfa/fgas/layout/cfgas_char.h"
 
 #include <algorithm>
+#include <array>
 #include <iterator>
 
 #include "core/fxcrt/check.h"
@@ -103,17 +99,17 @@
 };
 #undef PACK_NIBBLES
 
-const FX_BIDICLASS kNTypes[] = {
-    FX_BIDICLASS::kN,   FX_BIDICLASS::kL,   FX_BIDICLASS::kR,
-    FX_BIDICLASS::kAN,  FX_BIDICLASS::kEN,  FX_BIDICLASS::kAL,
-    FX_BIDICLASS::kNSM, FX_BIDICLASS::kCS,  FX_BIDICLASS::kES,
-    FX_BIDICLASS::kET,  FX_BIDICLASS::kBN,  FX_BIDICLASS::kBN,
-    FX_BIDICLASS::kN,   FX_BIDICLASS::kB,   FX_BIDICLASS::kRLO,
-    FX_BIDICLASS::kRLE, FX_BIDICLASS::kLRO, FX_BIDICLASS::kLRE,
-    FX_BIDICLASS::kPDF, FX_BIDICLASS::kON,
-};
+constexpr auto kNTypes = fxcrt::ToArray<const FX_BIDICLASS>(
+    {FX_BIDICLASS::kN,   FX_BIDICLASS::kL,   FX_BIDICLASS::kR,
+     FX_BIDICLASS::kAN,  FX_BIDICLASS::kEN,  FX_BIDICLASS::kAL,
+     FX_BIDICLASS::kNSM, FX_BIDICLASS::kCS,  FX_BIDICLASS::kES,
+     FX_BIDICLASS::kET,  FX_BIDICLASS::kBN,  FX_BIDICLASS::kBN,
+     FX_BIDICLASS::kN,   FX_BIDICLASS::kB,   FX_BIDICLASS::kRLO,
+     FX_BIDICLASS::kRLE, FX_BIDICLASS::kLRO, FX_BIDICLASS::kLRE,
+     FX_BIDICLASS::kPDF, FX_BIDICLASS::kON});
 
-const FX_BIDIWEAKSTATE kWeakStates[20][10] = {
+using WeakStateRow = std::array<const FX_BIDIWEAKSTATE, 10>;
+constexpr std::array<const WeakStateRow, 20> kWeakStateTable = {{
     {FX_BWSao, FX_BWSxl, FX_BWSxr, FX_BWScn, FX_BWScn, FX_BWSxa, FX_BWSxa,
      FX_BWSao, FX_BWSao, FX_BWSao},
     {FX_BWSro, FX_BWSxl, FX_BWSxr, FX_BWSra, FX_BWSre, FX_BWSxa, FX_BWSxr,
@@ -154,9 +150,10 @@
      FX_BWSro, FX_BWSro, FX_BWSret},
     {FX_BWSlo, FX_BWSxl, FX_BWSxr, FX_BWSla, FX_BWSle, FX_BWSxa, FX_BWSlet,
      FX_BWSlo, FX_BWSlo, FX_BWSlet},
-};
+}};
 
-const FX_BIDIWEAKACTION kWeakActions[20][10] = {
+using WeakActionRow = std::array<const FX_BIDIWEAKACTION, 10>;
+constexpr std::array<const WeakActionRow, 20> kWeakActionTable = {{
     {FX_BWAxxx, FX_BWAxxx, FX_BWAxxx, FX_BWAxxx, FX_BWAxxA, FX_BWAxxR,
      FX_BWAxxR, FX_BWAxxN, FX_BWAxxN, FX_BWAxxN},
     {FX_BWAxxx, FX_BWAxxx, FX_BWAxxx, FX_BWAxxx, FX_BWAxxE, FX_BWAxxR,
@@ -197,30 +194,33 @@
      FX_BWAxxE, FX_BWAxxN, FX_BWAxxN, FX_BWAxxE},
     {FX_BWAxxx, FX_BWAxxx, FX_BWAxxx, FX_BWAxxx, FX_BWAxxL, FX_BWAxxR,
      FX_BWAxxL, FX_BWAxxN, FX_BWAxxN, FX_BWAxxL},
-};
+}};
 
-const FX_BIDINEUTRALSTATE kNeutralStates[6][5] = {
+using NeutralStateRow = std::array<const FX_BIDINEUTRALSTATE, 5>;
+constexpr std::array<const NeutralStateRow, 6> kNeutralStateTable = {{
     {FX_BNSrn, FX_BNSl, FX_BNSr, FX_BNSr, FX_BNSr},
     {FX_BNSln, FX_BNSl, FX_BNSr, FX_BNSa, FX_BNSl},
     {FX_BNSrn, FX_BNSl, FX_BNSr, FX_BNSr, FX_BNSr},
     {FX_BNSln, FX_BNSl, FX_BNSr, FX_BNSa, FX_BNSl},
     {FX_BNSna, FX_BNSl, FX_BNSr, FX_BNSa, FX_BNSl},
     {FX_BNSna, FX_BNSl, FX_BNSr, FX_BNSa, FX_BNSl},
-};
+}};
 
-const FX_BIDINEUTRALACTION kNeutralActions[6][5] = {
+using NeutralActionRow = std::array<const FX_BIDINEUTRALACTION, 5>;
+constexpr std::array<const NeutralActionRow, 6> kNeutralActionTable = {{
     {FX_BNAIn, FX_BNAZero, FX_BNAZero, FX_BNAZero, FX_BNAZero},
     {FX_BNAIn, FX_BNAZero, FX_BNAZero, FX_BNAZero, FX_BNAnL},
     {FX_BNAIn, FX_BNAEn, FX_BNARn, FX_BNARn, FX_BNARn},
     {FX_BNAIn, FX_BNALn, FX_BNAEn, FX_BNAEn, FX_BNALnL},
     {FX_BNAIn, FX_BNAZero, FX_BNAZero, FX_BNAZero, FX_BNAnL},
     {FX_BNAIn, FX_BNAEn, FX_BNARn, FX_BNARn, FX_BNAEn},
-};
+}};
 
-const uint8_t kAddLevel[2][4] = {
+using AddLevelRow = std::array<const uint8_t, 4>;
+constexpr std::array<const AddLevelRow, 2> kAddLevelTable = {{
     {0, 1, 2, 2},
     {1, 0, 1, 1},
-};
+}};
 
 FX_BIDICLASS Direction(int32_t val) {
   return FX_IsOdd(val) ? FX_BIDICLASS::kR : FX_BIDICLASS::kL;
@@ -244,31 +244,25 @@
 }
 
 FX_BIDIWEAKSTATE GetWeakState(FX_BIDIWEAKSTATE eState, FX_BIDICLASS eClass) {
-  DCHECK(static_cast<size_t>(eState) < std::size(kWeakStates));
-  DCHECK(static_cast<size_t>(eClass) < std::size(kWeakStates[0]));
-  return kWeakStates[static_cast<size_t>(eState)][static_cast<size_t>(eClass)];
+  return kWeakStateTable[static_cast<size_t>(eState)]
+                        [static_cast<size_t>(eClass)];
 }
 
 FX_BIDIWEAKACTION GetWeakAction(FX_BIDIWEAKSTATE eState, FX_BIDICLASS eClass) {
-  DCHECK(static_cast<size_t>(eState) < std::size(kWeakActions));
-  DCHECK(static_cast<size_t>(eClass) < std::size(kWeakActions[0]));
-  return kWeakActions[static_cast<size_t>(eState)][static_cast<size_t>(eClass)];
+  return kWeakActionTable[static_cast<size_t>(eState)]
+                         [static_cast<size_t>(eClass)];
 }
 
 FX_BIDINEUTRALSTATE GetNeutralState(FX_BIDINEUTRALSTATE eState,
                                     FX_BIDICLASS eClass) {
-  DCHECK(static_cast<size_t>(eState) < std::size(kNeutralStates));
-  DCHECK(static_cast<size_t>(eClass) < std::size(kNeutralStates[0]));
-  return kNeutralStates[static_cast<size_t>(eState)]
-                       [static_cast<size_t>(eClass)];
+  return kNeutralStateTable[static_cast<size_t>(eState)]
+                           [static_cast<size_t>(eClass)];
 }
 
 FX_BIDINEUTRALACTION GetNeutralAction(FX_BIDINEUTRALSTATE eState,
                                       FX_BIDICLASS eClass) {
-  DCHECK(static_cast<size_t>(eState) < std::size(kNeutralActions));
-  DCHECK(static_cast<size_t>(eClass) < std::size(kNeutralActions[0]));
-  return kNeutralActions[static_cast<size_t>(eState)]
-                        [static_cast<size_t>(eClass)];
+  return kNeutralActionTable[static_cast<size_t>(eState)]
+                            [static_cast<size_t>(eClass)];
 }
 
 void ReverseString(std::vector<CFGAS_Char>* chars,
@@ -447,8 +441,9 @@
         eCls >= FX_BIDICLASS::kAL) {
       continue;
     }
-    (*chars)[i].m_iBidiLevel += kAddLevel[FX_IsOdd((*chars)[i].m_iBidiLevel)]
-                                         [static_cast<size_t>(eCls) - 1];
+    (*chars)[i].m_iBidiLevel +=
+        kAddLevelTable[FX_IsOdd((*chars)[i].m_iBidiLevel)]
+                      [static_cast<size_t>(eCls) - 1];
   }
 }
 
diff --git a/xfa/fgas/layout/cfgas_rtfbreak.cpp b/xfa/fgas/layout/cfgas_rtfbreak.cpp
index 976766d..3e1a4c4 100644
--- a/xfa/fgas/layout/cfgas_rtfbreak.cpp
+++ b/xfa/fgas/layout/cfgas_rtfbreak.cpp
@@ -4,17 +4,13 @@
 
 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
 
-#if defined(UNSAFE_BUFFERS_BUILD)
-// TODO(crbug.com/pdfium/2154): resolve buffer safety issues.
-#pragma allow_unsafe_buffers
-#endif
-
 #include "xfa/fgas/layout/cfgas_rtfbreak.h"
 
 #include <algorithm>
 
 #include "build/build_config.h"
 #include "core/fxcrt/check.h"
+#include "core/fxcrt/compiler_specific.h"
 #include "core/fxcrt/containers/adapters.h"
 #include "core/fxcrt/fx_extension.h"
 #include "core/fxcrt/fx_safe_types.h"
@@ -377,7 +373,7 @@
   int32_t iLast = fxcrt::CollectionSize<int32_t>(m_pCurLine->m_LineChars) - 1;
   int32_t j = 0;
   for (int32_t i = 0; i <= iLast;) {
-    const CFGAS_Char* pTC = pCurChars + i;
+    const CFGAS_Char* pTC = UNSAFE_TODO(pCurChars + i);
     if (bNew) {
       tp.SetStartChar(i);
       tp.IncrementStartPos(tp.GetWidth());
@@ -595,7 +591,7 @@
   }
 
   CFGAS_Char* pCharArray = tca.data();
-  CFGAS_Char* pCur = pCharArray + iLength;
+  CFGAS_Char* pCur = UNSAFE_TODO(pCharArray + iLength);
   --iLength;
   if (bAllChars)
     pCur->m_eLineBreakType = FX_LINEBREAKTYPE::kUNKNOWN;
@@ -606,7 +602,7 @@
     *pEndPos -= iCharWidth;
 
   while (iLength >= 0) {
-    pCur = pCharArray + iLength;
+    pCur = UNSAFE_TODO(pCharArray + iLength);
     FX_BREAKPROPERTY nCur =
         pdfium::unicode::GetBreakProperty(pCur->char_code());
     bool bNeedBreak = false;
diff --git a/xfa/fgas/layout/cfgas_txtbreak.cpp b/xfa/fgas/layout/cfgas_txtbreak.cpp
index f10b9ff..79e9f17 100644
--- a/xfa/fgas/layout/cfgas_txtbreak.cpp
+++ b/xfa/fgas/layout/cfgas_txtbreak.cpp
@@ -4,17 +4,14 @@
 
 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
 
-#if defined(UNSAFE_BUFFERS_BUILD)
-// TODO(crbug.com/pdfium/2154): resolve buffer safety issues.
-#pragma allow_unsafe_buffers
-#endif
-
 #include "xfa/fgas/layout/cfgas_txtbreak.h"
 
 #include <algorithm>
+#include <array>
 
 #include "build/build_config.h"
 #include "core/fxcrt/check.h"
+#include "core/fxcrt/compiler_specific.h"
 #include "core/fxcrt/containers/adapters.h"
 #include "core/fxcrt/fx_codepage.h"
 #include "core/fxcrt/fx_extension.h"
@@ -675,8 +672,8 @@
       wch = pEngine->GetChar(iAbsolute);
       iWidth = pEngine->GetWidthOfChar(iAbsolute);
     } else {
-      wch = *pStr++;
-      iWidth = *pWidths++;
+      wch = UNSAFE_TODO(*pStr++);
+      iWidth = UNSAFE_TODO(*pWidths++);
     }
 
     FX_CHARTYPE chartype = pdfium::unicode::GetCharType(wch);
@@ -708,7 +705,7 @@
             if (i + j >= iLength)
               break;
 
-            wNext = pStr[j];
+            wNext = UNSAFE_TODO(pStr[j]);
           } while (pdfium::unicode::GetCharType(wNext) ==
                    FX_CHARTYPE::kCombination);
           if (i + j >= iLength)
@@ -788,27 +785,27 @@
       iCharWidth = -iCharWidth;
 
     iCharWidth /= iFontSize;
-    FX_FORMCHAR formChars[3];
-    formChars[0].wch = wch;
-    formChars[0].wForm = wForm;
-    formChars[0].iWidth = iCharWidth;
+    std::array<FX_FORMCHAR, 3> form_chars;
+    form_chars[0].wch = wch;
+    form_chars[0].wForm = wForm;
+    form_chars[0].iWidth = iCharWidth;
     if (bLam) {
-      formChars[1].wForm = pdfium::arabic::kArabicShadda;
-      formChars[1].iWidth =
+      form_chars[1].wForm = pdfium::arabic::kArabicShadda;
+      form_chars[1].iWidth =
           pFont->GetCharWidth(pdfium::arabic::kArabicShadda).value_or(0);
-      formChars[2].wForm = pdfium::arabic::kArabicLetterSuperscriptAlef;
-      formChars[2].iWidth =
+      form_chars[2].wForm = pdfium::arabic::kArabicLetterSuperscriptAlef;
+      form_chars[2].iWidth =
           pFont->GetCharWidth(pdfium::arabic::kArabicLetterSuperscriptAlef)
               .value_or(0);
     }
 
     for (int32_t j = 0; j < iForms; j++) {
-      wForm = (wchar_t)formChars[j].wForm;
-      iCharWidth = formChars[j].iWidth;
+      wForm = (wchar_t)form_chars[j].wForm;
+      iCharWidth = form_chars[j].iWidth;
       if (j > 0) {
         chartype = FX_CHARTYPE::kCombination;
         wch = wForm;
-        wLast = (wchar_t)formChars[j - 1].wForm;
+        wLast = (wchar_t)form_chars[j - 1].wForm;
       }
       if (!bEmptyChar || (bEmptyChar && !bSkipSpace)) {
         pCharPos->m_GlyphIndex = pFont->GetGlyphIndex(wForm);
@@ -870,11 +867,11 @@
           pCharPos->m_AdjustMatrix[3] =
               pCharPos->m_AdjustMatrix[3] * iVerScale / 100.0f;
         }
-        pCharPos++;
+        UNSAFE_TODO(pCharPos++);
       }
     }
     if (iWidth > 0)
-      wPrev = static_cast<wchar_t>(formChars[0].wch);
+      wPrev = static_cast<wchar_t>(form_chars[0].wch);
     wLast = wch;
   }
   return szCount;
@@ -903,8 +900,8 @@
       wch = pEngine->GetChar(iAbsolute);
       iCharSize = pEngine->GetWidthOfChar(iAbsolute);
     } else {
-      wch = *pStr++;
-      iCharSize = *pWidths++;
+      wch = UNSAFE_TODO(*pStr++);
+      iCharSize = UNSAFE_TODO(*pWidths++);
     }
     float fCharSize = static_cast<float>(iCharSize) / kConversionFactor;
     bool bRet = (!bSingleLine && IsCtrlCode(wch));
diff --git a/xfa/fgas/layout/fgas_arabic.cpp b/xfa/fgas/layout/fgas_arabic.cpp
index d9fc7be..aa056d8 100644
--- a/xfa/fgas/layout/fgas_arabic.cpp
+++ b/xfa/fgas/layout/fgas_arabic.cpp
@@ -4,11 +4,6 @@
 
 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
 
-#if defined(UNSAFE_BUFFERS_BUILD)
-// TODO(crbug.com/pdfium/2154): resolve buffer safety issues.
-#pragma allow_unsafe_buffers
-#endif
-
 #include "xfa/fgas/layout/fgas_arabic.h"
 
 #include <iterator>
@@ -142,7 +137,7 @@
   if (unicode < kFirstFormTableEntry || unicode > kLastFormTableEntry)
     return nullptr;
 
-  return &kFormTable[unicode - kFirstFormTableEntry];
+  return UNSAFE_TODO(&kFormTable[unicode - kFirstFormTableEntry]);
 }
 
 const FX_ARBFORMTABLE* ParseChar(const CFGAS_Char* pTC,
@@ -221,7 +216,7 @@
   if (shadda < kFirstShaddaTableEntry || shadda > kLastShaddaTableEntry)
     return std::nullopt;
 
-  return kShaddaTable[shadda - kFirstShaddaTableEntry];
+  return UNSAFE_TODO(kShaddaTable[shadda - kFirstShaddaTableEntry]);
 }
 
 }  // namespace pdfium::arabic
diff --git a/xfa/fgas/layout/fgas_linebreak.cpp b/xfa/fgas/layout/fgas_linebreak.cpp
index 2e5f1f6..a40c6fb 100644
--- a/xfa/fgas/layout/fgas_linebreak.cpp
+++ b/xfa/fgas/layout/fgas_linebreak.cpp
@@ -4,16 +4,13 @@
 
 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
 
-#if defined(UNSAFE_BUFFERS_BUILD)
-// TODO(crbug.com/pdfium/2154): resolve buffer safety issues.
-#pragma allow_unsafe_buffers
-#endif
-
 #include "xfa/fgas/layout/fgas_linebreak.h"
 
+#include <array>
 #include <iterator>
 
 #include "core/fxcrt/check.h"
+#include "core/fxcrt/compiler_specific.h"
 #include "core/fxcrt/fx_unicode.h"
 
 namespace {
@@ -26,7 +23,8 @@
 #define FX_LBPB FX_LINEBREAKTYPE::kPROHIBITED_BRK
 #define FX_LBHS FX_LINEBREAKTYPE::kHANGUL_SPACE_BRK
 
-const FX_LINEBREAKTYPE kFX_LineBreak_PairTable[38][38] = {
+using LineBreakPairRow = std::array<const FX_LINEBREAKTYPE, 38>;
+constexpr std::array<const LineBreakPairRow, 38> kLineBreakPairTable = {{
     {FX_LBPB, FX_LBPB, FX_LBPB, FX_LBPB, FX_LBPB, FX_LBPB, FX_LBPB, FX_LBPB,
      FX_LBPB, FX_LBPB, FX_LBPB, FX_LBPB, FX_LBPB, FX_LBPB, FX_LBPB, FX_LBPB,
      FX_LBPB, FX_LBPB, FX_LBPB, FX_LBCP, FX_LBPB, FX_LBPB, FX_LBPB, FX_LBPB,
@@ -217,7 +215,7 @@
      FX_LBDB, FX_LBDB, FX_LBPB, FX_LBCB, FX_LBPB, FX_LBDB, FX_LBDB, FX_LBDB,
      FX_LBDB, FX_LBDB, FX_LBUN, FX_LBUN, FX_LBUN, FX_LBUN, FX_LBUN, FX_LBUN,
      FX_LBUN, FX_LBUN, FX_LBUN, FX_LBUN, FX_LBUN, FX_LBUN},
-};
+}};
 
 #undef FX_LBUN
 #undef FX_LBDB
@@ -231,9 +229,7 @@
 
 FX_LINEBREAKTYPE GetLineBreakTypeFromPair(FX_BREAKPROPERTY curr_char,
                                           FX_BREAKPROPERTY next_char) {
-  size_t row = static_cast<size_t>(curr_char);
-  size_t col = static_cast<size_t>(next_char);
-  DCHECK(row < std::size(kFX_LineBreak_PairTable));
-  DCHECK(col < std::size(kFX_LineBreak_PairTable[0]));
-  return kFX_LineBreak_PairTable[row][col];
+  const size_t row = static_cast<size_t>(curr_char);
+  const size_t col = static_cast<size_t>(next_char);
+  return kLineBreakPairTable[row][col];
 }