Change CFGAS_BreakPiece::GetLength() to return size_t.

The length should never be negative. Return size_t so there are fewer
locations that mix int and size_t. Add a GetChar() variant that takes
size_t to go with the GetLength() value.

Change-Id: I1584a41cc7fca1fa24a3000195d82e43091dd33e
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/95130
Reviewed-by: Nigi <nigi@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/xfa/fde/cfde_texteditengine.cpp b/xfa/fde/cfde_texteditengine.cpp
index 54d1663..49d2a8f 100644
--- a/xfa/fde/cfde_texteditengine.cpp
+++ b/xfa/fde/cfde_texteditengine.cpp
@@ -1072,7 +1072,7 @@
       txtEdtPiece.rtPiece.height = line_spacing_;
       txtEdtPiece.nStart =
           pdfium::base::checked_cast<int32_t>(current_piece_start);
-      txtEdtPiece.nCount = piece->GetLength();
+      txtEdtPiece.nCount = piece->GetCharCount();
       txtEdtPiece.nBidiLevel = piece->GetBidiLevel();
       txtEdtPiece.dwCharStyles = piece->GetCharStyles();
       if (FX_IsOdd(piece->GetBidiLevel()))
diff --git a/xfa/fde/cfde_textout.cpp b/xfa/fde/cfde_textout.cpp
index bdaf09e..1de901c 100644
--- a/xfa/fde/cfde_textout.cpp
+++ b/xfa/fde/cfde_textout.cpp
@@ -372,10 +372,10 @@
   int32_t iCount = m_pTxtBreak->CountBreakPieces();
   for (int32_t i = 0; i < iCount; i++) {
     const CFGAS_BreakPiece* pPiece = m_pTxtBreak->GetBreakPieceUnstable(i);
-    int32_t iPieceChars = pPiece->GetLength();
+    size_t iPieceChars = pPiece->GetLength();
     size_t iChar = *pStartChar;
     int32_t iWidth = 0;
-    int32_t j = 0;
+    size_t j = 0;
     for (; j < iPieceChars; j++) {
       const CFGAS_Char* pTC = pPiece->GetChar(j);
       int32_t iCurCharWidth = std::max(pTC->m_iCharWidth, 0);
diff --git a/xfa/fgas/layout/cfgas_breakpiece.cpp b/xfa/fgas/layout/cfgas_breakpiece.cpp
index cd6aa06..6d4a79c 100644
--- a/xfa/fgas/layout/cfgas_breakpiece.cpp
+++ b/xfa/fgas/layout/cfgas_breakpiece.cpp
@@ -6,7 +6,8 @@
 
 #include "xfa/fgas/layout/cfgas_breakpiece.h"
 
-#include "third_party/base/check.h"
+#include "third_party/base/check_op.h"
+#include "third_party/base/numerics/safe_conversions.h"
 #include "xfa/fgas/layout/cfgas_textuserdata.h"
 
 CFGAS_BreakPiece::CFGAS_BreakPiece() = default;
@@ -19,9 +20,16 @@
   return m_iWidth < 0 ? m_iStartPos : m_iStartPos + m_iWidth;
 }
 
+size_t CFGAS_BreakPiece::GetLength() const {
+  return pdfium::base::checked_cast<size_t>(m_iCharCount);
+}
+
 CFGAS_Char* CFGAS_BreakPiece::GetChar(int32_t index) const {
-  DCHECK(index >= 0);
-  DCHECK(index < m_iCharCount);
+  return GetChar(pdfium::base::checked_cast<size_t>(index));
+}
+
+CFGAS_Char* CFGAS_BreakPiece::GetChar(size_t index) const {
+  DCHECK_LT(index, GetLength());
   DCHECK(m_pChars);
   return &(*m_pChars)[m_iStartChar + index];
 }
diff --git a/xfa/fgas/layout/cfgas_breakpiece.h b/xfa/fgas/layout/cfgas_breakpiece.h
index b76cb5c..e2ae8dd 100644
--- a/xfa/fgas/layout/cfgas_breakpiece.h
+++ b/xfa/fgas/layout/cfgas_breakpiece.h
@@ -23,9 +23,12 @@
   ~CFGAS_BreakPiece();
 
   int32_t GetEndPos() const;
-  int32_t GetLength() const { return m_iCharCount; }
+
+  // TODO(thestig): When GetCharCount() returns size_t, remove this.
+  size_t GetLength() const;
 
   CFGAS_Char* GetChar(int32_t index) const;
+  CFGAS_Char* GetChar(size_t index) const;
   WideString GetString() const;
   std::vector<int32_t> GetWidths() const;