Apply pdfium_noshorten_config to fpdfsdk/

Convert remaining code to avoid unchecked shortening conversions.

-- rewrite a loop in GetPageIndexByAnnotDict()

Change-Id: I396a058525de0ff90e96a39e0cc28a937cc94484
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/91353
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/fpdfsdk/BUILD.gn b/fpdfsdk/BUILD.gn
index 228e7c8..fe1abaa 100644
--- a/fpdfsdk/BUILD.gn
+++ b/fpdfsdk/BUILD.gn
@@ -71,7 +71,10 @@
     "ipdfsdk_annothandler.h",
   ]
 
-  configs += [ "../:pdfium_strict_config" ]
+  configs += [
+    "../:pdfium_strict_config",
+    "../:pdfium_noshorten_config",
+  ]
   deps = [
     "../:pdfium_public_headers",
     "../constants",
diff --git a/fpdfsdk/cpdfsdk_appstream.cpp b/fpdfsdk/cpdfsdk_appstream.cpp
index 870bac4..41f2330 100644
--- a/fpdfsdk/cpdfsdk_appstream.cpp
+++ b/fpdfsdk/cpdfsdk_appstream.cpp
@@ -36,6 +36,7 @@
 #include "fpdfsdk/pwl/cpwl_edit_impl.h"
 #include "fpdfsdk/pwl/cpwl_wnd.h"
 #include "third_party/base/cxx17_backports.h"
+#include "third_party/base/numerics/safe_conversions.h"
 
 namespace {
 
@@ -1721,7 +1722,7 @@
       }
     } else {
       if (sValue.has_value())
-        nMaxLen = sValue.value().GetLength();
+        nMaxLen = pdfium::base::checked_cast<int>(sValue.value().GetLength());
       pEdit->SetLimitChar(nMaxLen);
     }
   }
diff --git a/fpdfsdk/cpdfsdk_customaccess.cpp b/fpdfsdk/cpdfsdk_customaccess.cpp
index 108ffbc..2d24ee9 100644
--- a/fpdfsdk/cpdfsdk_customaccess.cpp
+++ b/fpdfsdk/cpdfsdk_customaccess.cpp
@@ -7,6 +7,7 @@
 #include "fpdfsdk/cpdfsdk_customaccess.h"
 
 #include "core/fxcrt/fx_safe_types.h"
+#include "third_party/base/numerics/safe_conversions.h"
 
 CPDFSDK_CustomAccess::CPDFSDK_CustomAccess(FPDF_FILEACCESS* pFileAccess)
     : m_FileAccess(*pFileAccess) {}
@@ -29,6 +30,9 @@
   FX_SAFE_FILESIZE new_pos = size;
   new_pos += offset;
   return new_pos.IsValid() && new_pos.ValueOrDie() <= GetSize() &&
-         m_FileAccess.m_GetBlock(m_FileAccess.m_Param, offset,
-                                 static_cast<uint8_t*>(buffer), size);
+         m_FileAccess.m_GetBlock(
+             m_FileAccess.m_Param,
+             pdfium::base::checked_cast<unsigned long>(offset),
+             static_cast<uint8_t*>(buffer),
+             pdfium::base::checked_cast<unsigned long>(size));
 }
diff --git a/fpdfsdk/cpdfsdk_filewriteadapter.cpp b/fpdfsdk/cpdfsdk_filewriteadapter.cpp
index bf144ba..e697841 100644
--- a/fpdfsdk/cpdfsdk_filewriteadapter.cpp
+++ b/fpdfsdk/cpdfsdk_filewriteadapter.cpp
@@ -7,6 +7,7 @@
 #include "fpdfsdk/cpdfsdk_filewriteadapter.h"
 
 #include "third_party/base/check.h"
+#include "third_party/base/numerics/safe_conversions.h"
 
 CPDFSDK_FileWriteAdapter::CPDFSDK_FileWriteAdapter(FPDF_FILEWRITE* file_write)
     : file_write_(file_write) {
@@ -16,5 +17,7 @@
 CPDFSDK_FileWriteAdapter::~CPDFSDK_FileWriteAdapter() = default;
 
 bool CPDFSDK_FileWriteAdapter::WriteBlock(const void* data, size_t size) {
-  return file_write_->WriteBlock(file_write_.Get(), data, size) != 0;
+  return file_write_->WriteBlock(
+             file_write_.Get(), data,
+             pdfium::base::checked_cast<unsigned long>(size)) != 0;
 }
diff --git a/fpdfsdk/cpdfsdk_formfillenvironment.cpp b/fpdfsdk/cpdfsdk_formfillenvironment.cpp
index beed063..5f6aec8 100644
--- a/fpdfsdk/cpdfsdk_formfillenvironment.cpp
+++ b/fpdfsdk/cpdfsdk_formfillenvironment.cpp
@@ -408,7 +408,9 @@
     size_t nCharacters = text.GetLength();
     ByteString bsUTFText = text.ToUTF16LE();
     auto* pBuffer = reinterpret_cast<const unsigned short*>(bsUTFText.c_str());
-    m_pInfo->FFI_SetTextFieldFocus(m_pInfo, pBuffer, nCharacters, bFocus);
+    m_pInfo->FFI_SetTextFieldFocus(
+        m_pInfo, pBuffer, pdfium::base::checked_cast<FPDF_DWORD>(nCharacters),
+        bFocus);
   }
 }
 
diff --git a/fpdfsdk/cpdfsdk_helpers.cpp b/fpdfsdk/cpdfsdk_helpers.cpp
index 5c93834..d7bd086 100644
--- a/fpdfsdk/cpdfsdk_helpers.cpp
+++ b/fpdfsdk/cpdfsdk_helpers.cpp
@@ -281,7 +281,8 @@
 unsigned long NulTerminateMaybeCopyAndReturnLength(const ByteString& text,
                                                    void* buffer,
                                                    unsigned long buflen) {
-  unsigned long len = text.GetLength() + 1;
+  const unsigned long len =
+      pdfium::base::checked_cast<unsigned long>(text.GetLength() + 1);
   if (buffer && len <= buflen)
     memcpy(buffer, text.c_str(), len);
   return len;
@@ -291,7 +292,8 @@
                                                   void* buffer,
                                                   unsigned long buflen) {
   ByteString encoded_text = text.ToUTF16LE();
-  unsigned long len = encoded_text.GetLength();
+  const unsigned long len =
+      pdfium::base::checked_cast<unsigned long>(encoded_text.GetLength());
   if (buffer && len <= buflen)
     memcpy(buffer, encoded_text.c_str(), len);
   return len;
diff --git a/fpdfsdk/cpdfsdk_interactiveform.cpp b/fpdfsdk/cpdfsdk_interactiveform.cpp
index 9d56709..646cb53 100644
--- a/fpdfsdk/cpdfsdk_interactiveform.cpp
+++ b/fpdfsdk/cpdfsdk_interactiveform.cpp
@@ -24,6 +24,7 @@
 #include "core/fpdfdoc/cpdf_interactiveform.h"
 #include "core/fxcrt/autorestorer.h"
 #include "core/fxcrt/fx_string_wrappers.h"
+#include "core/fxcrt/stl_util.h"
 #include "core/fxge/cfx_graphstatedata.h"
 #include "core/fxge/cfx_path.h"
 #include "fpdfsdk/cpdfsdk_actionhandler.h"
@@ -103,8 +104,9 @@
   if (nBufSize <= 0)
     return false;
 
-  pBuffer->resize(nBufSize);
-  memcpy(pBuffer->data(), fdfEncodedData.str().c_str(), nBufSize);
+  size_t copy_size = static_cast<size_t>(nBufSize);
+  pBuffer->resize(copy_size);
+  memcpy(pBuffer->data(), fdfEncodedData.str().c_str(), copy_size);
   return true;
 }
 
@@ -181,17 +183,20 @@
   DCHECK(pAnnotDict);
 
   for (int i = 0, sz = pDocument->GetPageCount(); i < sz; i++) {
-    if (CPDF_Dictionary* pPageDict = pDocument->GetPageDictionary(i)) {
-      if (CPDF_Array* pAnnots = pPageDict->GetArrayFor("Annots")) {
-        for (int j = 0, jsz = pAnnots->size(); j < jsz; j++) {
-          CPDF_Object* pDict = pAnnots->GetDirectObjectAt(j);
-          if (pAnnotDict == pDict)
-            return i;
-        }
-      }
+    CPDF_Dictionary* pPageDict = pDocument->GetPageDictionary(i);
+    if (!pPageDict)
+      continue;
+
+    CPDF_Array* pAnnots = pPageDict->GetArrayFor("Annots");
+    if (!pAnnots)
+      continue;
+
+    for (size_t j = 0, jsz = pAnnots->size(); j < jsz; j++) {
+      CPDF_Object* pDict = pAnnots->GetDirectObjectAt(j);
+      if (pAnnotDict == pDict)
+        return i;
     }
   }
-
   return -1;
 }
 
diff --git a/fpdfsdk/fpdf_annot.cpp b/fpdfsdk/fpdf_annot.cpp
index 2dd52b2..817aa00 100644
--- a/fpdfsdk/fpdf_annot.cpp
+++ b/fpdfsdk/fpdf_annot.cpp
@@ -38,6 +38,7 @@
 #include "fpdfsdk/cpdfsdk_interactiveform.h"
 #include "third_party/base/check.h"
 #include "third_party/base/cxx17_backports.h"
+#include "third_party/base/numerics/safe_conversions.h"
 #include "third_party/base/ptr_util.h"
 
 namespace {
@@ -363,7 +364,7 @@
     return 0;
 
   const CPDF_Array* pAnnots = pPage->GetDict()->GetArrayFor("Annots");
-  return pAnnots ? pAnnots->size() : 0;
+  return pAnnots ? fxcrt::CollectionSize<int>(*pAnnots) : 0;
 }
 
 FPDF_EXPORT FPDF_ANNOTATION FPDF_CALLCONV FPDFPage_GetAnnot(FPDF_PAGE page,
@@ -410,7 +411,7 @@
   if (it == locker.end())
     return -1;
 
-  return it - locker.begin();
+  return pdfium::base::checked_cast<int>(it - locker.begin());
 }
 
 FPDF_EXPORT void FPDF_CALLCONV FPDFPage_CloseAnnot(FPDF_ANNOTATION annot) {
@@ -582,7 +583,8 @@
 
     pAnnot->SetForm(pStream);
   }
-  return pAnnot->GetForm()->GetPageObjectCount();
+  return pdfium::base::checked_cast<int>(
+      pAnnot->GetForm()->GetPageObjectCount());
 }
 
 FPDF_EXPORT FPDF_PAGEOBJECT FPDF_CALLCONV
@@ -851,7 +853,8 @@
     return 0;
 
   // Truncate to an even number.
-  unsigned long points_len = vertices->size() / 2;
+  const unsigned long points_len =
+      fxcrt::CollectionSize<unsigned long>(*vertices) / 2;
   if (buffer && length >= points_len) {
     for (unsigned long i = 0; i < points_len; ++i) {
       buffer[i].x = vertices->GetNumberAt(i * 2);
@@ -864,7 +867,7 @@
 FPDF_EXPORT unsigned long FPDF_CALLCONV
 FPDFAnnot_GetInkListCount(FPDF_ANNOTATION annot) {
   const CPDF_Array* ink_list = GetInkList(annot);
-  return ink_list ? ink_list->size() : 0;
+  return ink_list ? fxcrt::CollectionSize<unsigned long>(*ink_list) : 0;
 }
 
 FPDF_EXPORT unsigned long FPDF_CALLCONV
@@ -881,7 +884,8 @@
     return 0;
 
   // Truncate to an even number.
-  unsigned long points_len = path->size() / 2;
+  const unsigned long points_len =
+      fxcrt::CollectionSize<unsigned long>(*path) / 2;
   if (buffer && length >= points_len) {
     for (unsigned long i = 0; i < points_len; ++i) {
       buffer[i].x = path->GetNumberAt(i * 2);
diff --git a/fpdfsdk/fpdf_dataavail.cpp b/fpdfsdk/fpdf_dataavail.cpp
index a38c485..cfc2d5b 100644
--- a/fpdfsdk/fpdf_dataavail.cpp
+++ b/fpdfsdk/fpdf_dataavail.cpp
@@ -19,6 +19,7 @@
 #include "core/fxcrt/unowned_ptr.h"
 #include "fpdfsdk/cpdfsdk_helpers.h"
 #include "public/fpdf_formfill.h"
+#include "third_party/base/numerics/safe_conversions.h"
 
 #ifdef PDF_ENABLE_XFA
 #include "fpdfsdk/fpdfxfa/cpdfxfa_context.h"
@@ -58,7 +59,8 @@
 
   // CPDF_DataAvail::FileAvail:
   bool IsDataAvail(FX_FILESIZE offset, size_t size) override {
-    return !!avail_->IsDataAvail(avail_, offset, size);
+    return !!avail_->IsDataAvail(
+        avail_, pdfium::base::checked_cast<size_t>(offset), size);
   }
 
  private:
@@ -84,8 +86,10 @@
     FX_SAFE_FILESIZE new_pos = size;
     new_pos += offset;
     return new_pos.IsValid() && new_pos.ValueOrDie() <= GetSize() &&
-           file_->m_GetBlock(file_->m_Param, offset,
-                             static_cast<uint8_t*>(buffer), size);
+           file_->m_GetBlock(file_->m_Param,
+                             pdfium::base::checked_cast<unsigned long>(offset),
+                             static_cast<uint8_t*>(buffer),
+                             pdfium::base::checked_cast<unsigned long>(size));
   }
 
  private:
@@ -103,8 +107,10 @@
 
   // IFX_DownloadHints
   void AddSegment(FX_FILESIZE offset, size_t size) override {
-    if (m_pDownloadHints)
-      m_pDownloadHints->AddSegment(m_pDownloadHints, offset, size);
+    if (m_pDownloadHints) {
+      m_pDownloadHints->AddSegment(m_pDownloadHints,
+                                   static_cast<size_t>(offset), size);
+    }
   }
 
  private:
diff --git a/fpdfsdk/fpdf_doc.cpp b/fpdfsdk/fpdf_doc.cpp
index 459f8d5..2751ca8 100644
--- a/fpdfsdk/fpdf_doc.cpp
+++ b/fpdfsdk/fpdf_doc.cpp
@@ -29,6 +29,7 @@
 #include "public/fpdf_formfill.h"
 #include "third_party/base/check.h"
 #include "third_party/base/containers/contains.h"
+#include "third_party/base/numerics/safe_conversions.h"
 
 namespace {
 
@@ -220,7 +221,8 @@
 
   CPDF_Action cAction(CPDFDictionaryFromFPDFAction(action));
   ByteString path = cAction.GetURI(pDoc);
-  unsigned long len = path.GetLength() + 1;
+  const unsigned long len =
+      pdfium::base::checked_cast<unsigned long>(path.GetLength() + 1);
   if (buffer && len <= buflen)
     memcpy(buffer, path.c_str(), len);
   return len;
@@ -247,7 +249,8 @@
   }
 
   CPDF_Dest destination(CPDFArrayFromFPDFDest(dest));
-  unsigned long nParams = destination.GetNumParams();
+  const unsigned long nParams =
+      pdfium::base::checked_cast<unsigned long>(destination.GetNumParams());
   DCHECK(nParams <= 4);
   *pNumParams = nParams;
   for (unsigned long i = 0; i < nParams; ++i)
diff --git a/fpdfsdk/fpdf_editpage.cpp b/fpdfsdk/fpdf_editpage.cpp
index 12658e5..8692699 100644
--- a/fpdfsdk/fpdf_editpage.cpp
+++ b/fpdfsdk/fpdf_editpage.cpp
@@ -34,10 +34,12 @@
 #include "core/fpdfdoc/cpdf_annot.h"
 #include "core/fpdfdoc/cpdf_annotlist.h"
 #include "core/fxcrt/fx_extension.h"
+#include "core/fxcrt/stl_util.h"
 #include "fpdfsdk/cpdfsdk_helpers.h"
 #include "public/fpdf_formfill.h"
 #include "third_party/base/cxx17_backports.h"
 #include "third_party/base/notreached.h"
+#include "third_party/base/numerics/safe_conversions.h"
 
 #ifdef PDF_ENABLE_XFA
 #include "fpdfsdk/fpdfxfa/cpdfxfa_context.h"
@@ -273,7 +275,7 @@
   if (!IsPageObject(pPage))
     return -1;
 
-  return pPage->GetPageObjectCount();
+  return pdfium::base::checked_cast<int>(pPage->GetPageObjectCount());
 }
 
 FPDF_EXPORT FPDF_PAGEOBJECT FPDF_CALLCONV FPDFPage_GetObject(FPDF_PAGE page,
@@ -300,7 +302,8 @@
   if (!pPageObj)
     return -1;
 
-  return pPageObj->GetContentMarks()->CountItems();
+  return pdfium::base::checked_cast<int>(
+      pPageObj->GetContentMarks()->CountItems());
 }
 
 FPDF_EXPORT FPDF_PAGEOBJECTMARK FPDF_CALLCONV
@@ -326,7 +329,7 @@
   pMarks->AddMark(name);
   pPageObj->SetDirty(true);
 
-  const unsigned long index = pMarks->CountItems() - 1;
+  const size_t index = pMarks->CountItems() - 1;
   return FPDFPageObjectMarkFromCPDFContentMarkItem(pMarks->GetItem(index));
 }
 
@@ -369,7 +372,7 @@
     return -1;
 
   const CPDF_Dictionary* pParams = pMarkItem->GetParam();
-  return pParams ? pParams->size() : 0;
+  return pParams ? fxcrt::CollectionSize<int>(*pParams) : 0;
 }
 
 FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
@@ -468,7 +471,8 @@
     return false;
 
   ByteString result = pObj->GetString();
-  unsigned long len = result.GetLength();
+  const unsigned long len =
+      pdfium::base::checked_cast<unsigned long>(result.GetLength());
 
   if (buffer && len <= buflen)
     memcpy(buffer, result.c_str(), len);
@@ -919,7 +923,9 @@
 FPDF_EXPORT int FPDF_CALLCONV
 FPDFPageObj_GetDashCount(FPDF_PAGEOBJECT page_object) {
   auto* pPageObj = CPDFPageObjectFromFPDFPageObject(page_object);
-  return pPageObj ? pPageObj->m_GraphState.GetLineDashSize() : -1;
+  return pPageObj ? pdfium::base::checked_cast<int>(
+                        pPageObj->m_GraphState.GetLineDashSize())
+                  : -1;
 }
 
 FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
@@ -965,7 +971,9 @@
 FPDF_EXPORT int FPDF_CALLCONV
 FPDFFormObj_CountObjects(FPDF_PAGEOBJECT form_object) {
   const auto* pObjectList = CPDFPageObjHolderFromFPDFFormObject(form_object);
-  return pObjectList ? pObjectList->GetPageObjectCount() : -1;
+  return pObjectList ? pdfium::base::checked_cast<int>(
+                           pObjectList->GetPageObjectCount())
+                     : -1;
 }
 
 FPDF_EXPORT FPDF_PAGEOBJECT FPDF_CALLCONV
diff --git a/fpdfsdk/fpdf_edittext.cpp b/fpdfsdk/fpdf_edittext.cpp
index c2fda59..ca7c52b 100644
--- a/fpdfsdk/fpdf_edittext.cpp
+++ b/fpdfsdk/fpdf_edittext.cpp
@@ -292,8 +292,8 @@
   pFontDict->SetNewFor<CPDF_Name>("BaseFont", name);
 
   uint32_t dwGlyphIndex;
-  uint32_t dwCurrentChar =
-      FT_Get_First_Char(pFont->GetFaceRec(), &dwGlyphIndex);
+  uint32_t dwCurrentChar = static_cast<uint32_t>(
+      FT_Get_First_Char(pFont->GetFaceRec(), &dwGlyphIndex));
   static constexpr uint32_t kMaxSimpleFontChar = 0xFF;
   if (dwCurrentChar > kMaxSimpleFontChar || dwGlyphIndex == 0)
     return nullptr;
@@ -302,8 +302,8 @@
   CPDF_Array* widthsArray = pDoc->NewIndirect<CPDF_Array>();
   while (true) {
     widthsArray->AppendNew<CPDF_Number>(pFont->GetGlyphWidth(dwGlyphIndex));
-    uint32_t nextChar =
-        FT_Get_Next_Char(pFont->GetFaceRec(), dwCurrentChar, &dwGlyphIndex);
+    uint32_t nextChar = static_cast<uint32_t>(
+        FT_Get_Next_Char(pFont->GetFaceRec(), dwCurrentChar, &dwGlyphIndex));
     // Simple fonts have 1-byte charcodes only.
     if (nextChar > kMaxSimpleFontChar || dwGlyphIndex == 0)
       break;
@@ -359,8 +359,8 @@
                                       pFontDesc->GetObjNum());
 
   uint32_t dwGlyphIndex;
-  uint32_t dwCurrentChar =
-      FT_Get_First_Char(pFont->GetFaceRec(), &dwGlyphIndex);
+  uint32_t dwCurrentChar = static_cast<uint32_t>(
+      FT_Get_First_Char(pFont->GetFaceRec(), &dwGlyphIndex));
   static constexpr uint32_t kMaxUnicode = 0x10FFFF;
   // If it doesn't have a single char, just fail
   if (dwGlyphIndex == 0 || dwCurrentChar > kMaxUnicode)
@@ -375,8 +375,8 @@
     if (!pdfium::Contains(widths, dwGlyphIndex))
       widths[dwGlyphIndex] = pFont->GetGlyphWidth(dwGlyphIndex);
     to_unicode.emplace(dwGlyphIndex, dwCurrentChar);
-    dwCurrentChar =
-        FT_Get_Next_Char(pFont->GetFaceRec(), dwCurrentChar, &dwGlyphIndex);
+    dwCurrentChar = static_cast<uint32_t>(
+        FT_Get_Next_Char(pFont->GetFaceRec(), dwCurrentChar, &dwGlyphIndex));
     if (dwGlyphIndex == 0)
       break;
   }
@@ -643,8 +643,8 @@
 
   CFX_Font* pCfxFont = pFont->GetFont();
   ByteString name = pCfxFont->GetFamilyName();
-  unsigned long dwStringLen = name.GetLength() + 1;
-
+  const unsigned long dwStringLen =
+      pdfium::base::checked_cast<unsigned long>(name.GetLength() + 1);
   if (buffer && length >= dwStringLen)
     memcpy(buffer, name.c_str(), dwStringLen);
 
diff --git a/fpdfsdk/fpdf_formfill.cpp b/fpdfsdk/fpdf_formfill.cpp
index a027c9c..089c1d3 100644
--- a/fpdfsdk/fpdf_formfill.cpp
+++ b/fpdfsdk/fpdf_formfill.cpp
@@ -712,10 +712,12 @@
   if (!cast_input.has_value())
     return;
 
-  if (cast_input.value() == FormFieldType::kUnknown)
+  if (cast_input.value() == FormFieldType::kUnknown) {
     pForm->SetAllHighlightColors(static_cast<FX_COLORREF>(color));
-  else
-    pForm->SetHighlightColor(color, cast_input.value());
+  } else {
+    pForm->SetHighlightColor(static_cast<FX_COLORREF>(color),
+                             cast_input.value());
+  }
 }
 
 FPDF_EXPORT void FPDF_CALLCONV
diff --git a/fpdfsdk/fpdf_ppo.cpp b/fpdfsdk/fpdf_ppo.cpp
index ec42c90..e80e44c 100644
--- a/fpdfsdk/fpdf_ppo.cpp
+++ b/fpdfsdk/fpdf_ppo.cpp
@@ -536,15 +536,15 @@
   size_t nPagesPerSheet = nSafePagesPerSheet.ValueOrDie();
   NupState nupState(destPageSize, nPagesOnXAxis, nPagesOnYAxis);
 
-  size_t curpage = 0;
+  FX_SAFE_INT32 curpage = 0;
   const CFX_FloatRect destPageRect(0, 0, destPageSize.width,
                                    destPageSize.height);
   for (size_t iOuterPage = 0; iOuterPage < pageIndices.size();
        iOuterPage += nPagesPerSheet) {
     m_XObjectNameToNumberMap.clear();
 
-    // Create a new page
-    CPDF_Dictionary* pDestPageDict = dest()->CreateNewPage(curpage);
+    CPDF_Dictionary* pDestPageDict =
+        dest()->CreateNewPage(curpage.ValueOrDie());
     if (!pDestPageDict)
       return false;
 
diff --git a/fpdfsdk/fpdf_signature.cpp b/fpdfsdk/fpdf_signature.cpp
index b18ce5a..00f0a6c 100644
--- a/fpdfsdk/fpdf_signature.cpp
+++ b/fpdfsdk/fpdf_signature.cpp
@@ -11,6 +11,7 @@
 #include "core/fpdfapi/parser/cpdf_document.h"
 #include "core/fxcrt/stl_util.h"
 #include "fpdfsdk/cpdfsdk_helpers.h"
+#include "third_party/base/numerics/safe_conversions.h"
 
 namespace {
 
@@ -73,7 +74,8 @@
     return 0;
 
   ByteString contents = value_dict->GetStringFor("Contents");
-  unsigned long contents_len = contents.GetLength();
+  const unsigned long contents_len =
+      pdfium::base::checked_cast<unsigned long>(contents.GetLength());
   if (buffer && length >= contents_len)
     memcpy(buffer, contents.c_str(), contents_len);
 
@@ -96,12 +98,12 @@
   if (!byte_range)
     return 0;
 
-  unsigned long byte_range_len = byte_range->size();
+  const unsigned long byte_range_len =
+      fxcrt::CollectionSize<unsigned long>(*byte_range);
   if (buffer && length >= byte_range_len) {
     for (size_t i = 0; i < byte_range_len; ++i)
       buffer[i] = byte_range->GetIntegerAt(i);
   }
-
   return byte_range_len;
 }
 
diff --git a/fpdfsdk/fpdf_structtree.cpp b/fpdfsdk/fpdf_structtree.cpp
index e121e48..fd43e49 100644
--- a/fpdfsdk/fpdf_structtree.cpp
+++ b/fpdfsdk/fpdf_structtree.cpp
@@ -14,6 +14,7 @@
 #include "core/fxcrt/fx_safe_types.h"
 #include "core/fxcrt/stl_util.h"
 #include "fpdfsdk/cpdfsdk_helpers.h"
+#include "third_party/base/numerics/safe_conversions.h"
 
 namespace {
 
@@ -24,7 +25,8 @@
     return 0;
 
   ByteString encodedStr = str.ToUTF16LE();
-  const unsigned long len = encodedStr.GetLength();
+  const unsigned long len =
+      pdfium::base::checked_cast<unsigned long>(encodedStr.GetLength());
   if (buffer && len <= buflen)
     memcpy(buffer, encodedStr.c_str(), len);
   return len;
@@ -398,8 +400,8 @@
     return false;
 
   ByteString result = obj->GetString();
-  unsigned long len = result.GetLength();
-
+  const unsigned long len =
+      pdfium::base::checked_cast<unsigned long>(result.GetLength());
   if (buffer && len <= buflen)
     memcpy(buffer, result.c_str(), len);
 
diff --git a/fpdfsdk/fpdf_sysfontinfo.cpp b/fpdfsdk/fpdf_sysfontinfo.cpp
index ffdb842..0c8f245 100644
--- a/fpdfsdk/fpdf_sysfontinfo.cpp
+++ b/fpdfsdk/fpdf_sysfontinfo.cpp
@@ -11,6 +11,7 @@
 #include <memory>
 
 #include "core/fxcrt/fx_codepage.h"
+#include "core/fxcrt/stl_util.h"
 #include "core/fxcrt/unowned_ptr.h"
 #include "core/fxge/cfx_font.h"
 #include "core/fxge/cfx_fontmapper.h"
@@ -18,6 +19,7 @@
 #include "core/fxge/cfx_gemodule.h"
 #include "core/fxge/fx_font.h"
 #include "core/fxge/systemfontinfo_iface.h"
+#include "third_party/base/numerics/safe_conversions.h"
 
 static_assert(FXFONT_ANSI_CHARSET == static_cast<int>(FX_Charset::kANSI),
               "Charset must match");
@@ -106,13 +108,13 @@
     if (!m_pInfo->GetFontData)
       return 0;
     return m_pInfo->GetFontData(m_pInfo, hFont, table, buffer.data(),
-                                buffer.size());
+                                fxcrt::CollectionSize<unsigned long>(buffer));
   }
 
   bool GetFaceName(void* hFont, ByteString* name) override {
     if (!m_pInfo->GetFaceName)
       return false;
-    size_t size = m_pInfo->GetFaceName(m_pInfo, hFont, nullptr, 0);
+    unsigned long size = m_pInfo->GetFaceName(m_pInfo, hFont, nullptr, 0);
     if (size == 0)
       return false;
     char* buffer = FX_Alloc(char, size);
@@ -196,7 +198,8 @@
                                         unsigned char* buffer,
                                         unsigned long buf_size) {
   auto* pDefault = static_cast<FPDF_SYSFONTINFO_DEFAULT*>(pThis);
-  return pDefault->m_pFontInfo->GetFontData(hFont, table, {buffer, buf_size});
+  return pdfium::base::checked_cast<unsigned long>(
+      pDefault->m_pFontInfo->GetFontData(hFont, table, {buffer, buf_size}));
 }
 
 static unsigned long DefaultGetFaceName(struct _FPDF_SYSFONTINFO* pThis,
@@ -207,12 +210,13 @@
   auto* pDefault = static_cast<FPDF_SYSFONTINFO_DEFAULT*>(pThis);
   if (!pDefault->m_pFontInfo->GetFaceName(hFont, &name))
     return 0;
-  if (name.GetLength() >= static_cast<size_t>(buf_size))
-    return name.GetLength() + 1;
 
-  strncpy(buffer, name.c_str(),
-          (name.GetLength() + 1) * sizeof(ByteString::CharType));
-  return name.GetLength() + 1;
+  const unsigned long copy_length =
+      pdfium::base::checked_cast<unsigned long>(name.GetLength() + 1);
+  if (copy_length <= buf_size)
+    strncpy(buffer, name.c_str(), copy_length * sizeof(ByteString::CharType));
+
+  return copy_length;
 }
 
 static int DefaultGetFontCharset(struct _FPDF_SYSFONTINFO* pThis, void* hFont) {
diff --git a/fpdfsdk/fpdf_text.cpp b/fpdfsdk/fpdf_text.cpp
index d766393..d7420ee 100644
--- a/fpdfsdk/fpdf_text.cpp
+++ b/fpdfsdk/fpdf_text.cpp
@@ -99,7 +99,8 @@
     *flags = font->GetFontFlags();
 
   ByteString basefont = font->GetBaseFontName();
-  unsigned long length = basefont.GetLength() + 1;
+  const unsigned long length =
+      pdfium::base::checked_cast<unsigned long>(basefont.GetLength() + 1);
   if (buffer && buflen >= length)
     memcpy(buffer, basefont.c_str(), length);
 
diff --git a/fpdfsdk/fpdf_view.cpp b/fpdfsdk/fpdf_view.cpp
index e253687..161340b 100644
--- a/fpdfsdk/fpdf_view.cpp
+++ b/fpdfsdk/fpdf_view.cpp
@@ -47,6 +47,7 @@
 #include "fpdfsdk/cpdfsdk_renderpage.h"
 #include "fxjs/ijs_runtime.h"
 #include "public/fpdf_formfill.h"
+#include "third_party/base/numerics/safe_conversions.h"
 #include "third_party/base/ptr_util.h"
 #include "third_party/base/span.h"
 
@@ -867,7 +868,8 @@
   device.Attach(pBitmap, false, nullptr, false);
   if (!pBitmap->IsAlphaFormat())
     color |= 0xFF000000;
-  device.FillRect(FX_RECT(left, top, left + width, top + height), color);
+  device.FillRect(FX_RECT(left, top, left + width, top + height),
+                  static_cast<uint32_t>(color));
 }
 
 FPDF_EXPORT void* FPDF_CALLCONV FPDFBitmap_GetBuffer(FPDF_BITMAP bitmap) {
@@ -1082,7 +1084,7 @@
     return -1;
 
   if (length == -1)
-    length = strlen(cstr);
+    length = pdfium::base::checked_cast<int>(strlen(cstr));
 
   if (length == 0) {
     FPDF_BStr_Clear(bstr);
@@ -1173,7 +1175,7 @@
     return nullptr;
 
   ByteString utf16Name = wsName.ToUTF16LE();
-  int len = utf16Name.GetLength();
+  int len = pdfium::base::checked_cast<int>(utf16Name.GetLength());
   if (!buffer) {
     *buflen = len;
   } else if (len <= *buflen) {
@@ -1284,7 +1286,8 @@
   // Stop recording trailer ends.
   syntax->SetTrailerEnds(nullptr);
 
-  unsigned long trailer_ends_len = trailer_ends.size();
+  const unsigned long trailer_ends_len =
+      fxcrt::CollectionSize<unsigned long>(trailer_ends);
   if (buffer && length >= trailer_ends_len) {
     for (size_t i = 0; i < trailer_ends_len; ++i)
       buffer[i] = trailer_ends[i];