Add -Wshorten-64-to-32 to core/fpdfdoc.
Change-Id: I79d1d98fd9322b6a4f6ab2495684430759b71e5a
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/89590
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fpdfdoc/BUILD.gn b/core/fpdfdoc/BUILD.gn
index bb5c421..c917a5c 100644
--- a/core/fpdfdoc/BUILD.gn
+++ b/core/fpdfdoc/BUILD.gn
@@ -77,6 +77,7 @@
"cpvt_wordrange.h",
"ipvt_fontmap.h",
]
+ cflags = []
configs += [ "../../:pdfium_strict_config" ]
deps = [
"../../constants",
@@ -88,6 +89,9 @@
"../fxge",
]
visibility = [ "../../*" ]
+ if (is_clang) {
+ cflags += [ "-Wshorten-64-to-32" ]
+ }
}
pdfium_unittest_source_set("unittests") {
diff --git a/core/fpdfdoc/cpdf_bookmark.cpp b/core/fpdfdoc/cpdf_bookmark.cpp
index 236b5ec..954831b 100644
--- a/core/fpdfdoc/cpdf_bookmark.cpp
+++ b/core/fpdfdoc/cpdf_bookmark.cpp
@@ -30,12 +30,12 @@
return WideString();
WideString title = pString->GetUnicodeText();
- int len = title.GetLength();
+ size_t len = title.GetLength();
if (!len)
return WideString();
std::vector<wchar_t, FxAllocAllocator<wchar_t>> buf(len);
- for (int i = 0; i < len; i++) {
+ for (size_t i = 0; i < len; i++) {
wchar_t w = title[i];
buf[i] = w > 0x20 ? w : 0x20;
}
diff --git a/core/fpdfdoc/cpdf_dest.cpp b/core/fpdfdoc/cpdf_dest.cpp
index 016df00..f3b1152 100644
--- a/core/fpdfdoc/cpdf_dest.cpp
+++ b/core/fpdfdoc/cpdf_dest.cpp
@@ -127,15 +127,15 @@
return true;
}
-unsigned long CPDF_Dest::GetNumParams() const {
+size_t CPDF_Dest::GetNumParams() const {
if (!m_pArray || m_pArray->size() < 2)
return 0;
- unsigned long maxParamsForFitType = kZoomModeMaxParamCount[GetZoomMode()];
- unsigned long numParamsInArray = m_pArray->size() - 2;
+ size_t maxParamsForFitType = kZoomModeMaxParamCount[GetZoomMode()];
+ size_t numParamsInArray = m_pArray->size() - 2;
return std::min(maxParamsForFitType, numParamsInArray);
}
-float CPDF_Dest::GetParam(int index) const {
+float CPDF_Dest::GetParam(size_t index) const {
return m_pArray ? m_pArray->GetNumberAt(2 + index) : 0;
}
diff --git a/core/fpdfdoc/cpdf_dest.h b/core/fpdfdoc/cpdf_dest.h
index 0118bb4..98f51d0 100644
--- a/core/fpdfdoc/cpdf_dest.h
+++ b/core/fpdfdoc/cpdf_dest.h
@@ -28,8 +28,8 @@
// Returns the zoom mode, as one of the PDFDEST_VIEW_* values in fpdf_doc.h.
int GetZoomMode() const;
- unsigned long GetNumParams() const;
- float GetParam(int index) const;
+ size_t GetNumParams() const;
+ float GetParam(size_t index) const;
bool GetXYZ(bool* pHasX,
bool* pHasY,
diff --git a/core/fpdfdoc/cpdf_formfield.cpp b/core/fpdfdoc/cpdf_formfield.cpp
index 61f64bc..456acea 100644
--- a/core/fpdfdoc/cpdf_formfield.cpp
+++ b/core/fpdfdoc/cpdf_formfield.cpp
@@ -232,7 +232,10 @@
const auto& controls = GetControls();
auto it = std::find(controls.begin(), controls.end(), pControl);
- return it != controls.end() ? it - controls.begin() : -1;
+ if (it == controls.end())
+ return -1;
+
+ return pdfium::base::checked_cast<int>(it - controls.begin());
}
FormFieldType CPDF_FormField::GetFieldType() const {
@@ -422,7 +425,7 @@
if (pValue->IsString() || pValue->IsNumber())
return pValue->GetString().IsEmpty() ? 0 : 1;
const CPDF_Array* pArray = pValue->AsArray();
- return pArray ? pArray->size() : 0;
+ return pArray ? fxcrt::CollectionSize<int>(*pArray) : 0;
}
int CPDF_FormField::GetSelectedIndex(int index) const {
@@ -555,7 +558,7 @@
int CPDF_FormField::CountOptions() const {
const CPDF_Array* pArray = ToArray(GetFieldAttr(m_pDict.Get(), "Opt"));
- return pArray ? pArray->size() : 0;
+ return pArray ? fxcrt::CollectionSize<int>(*pArray) : 0;
}
WideString CPDF_FormField::GetOptionText(int index, int sub_index) const {
@@ -687,18 +690,20 @@
int CPDF_FormField::CountSelectedOptions() const {
const CPDF_Array* pArray = ToArray(GetSelectedIndicesObject());
- return pArray ? pArray->size() : 0;
+ return pArray ? fxcrt::CollectionSize<int>(*pArray) : 0;
}
int CPDF_FormField::GetSelectedOptionIndex(int index) const {
+ if (index < 0)
+ return 0;
+
const CPDF_Array* pArray = ToArray(GetSelectedIndicesObject());
if (!pArray)
return -1;
- int iCount = pArray->size();
- if (iCount < 0 || index >= iCount)
- return -1;
- return pArray->GetIntegerAt(index);
+ return index < fxcrt::CollectionSize<int>(*pArray)
+ ? pArray->GetIntegerAt(index)
+ : -1;
}
bool CPDF_FormField::IsSelectedOption(const WideString& wsOptValue) const {
diff --git a/core/fpdfdoc/cpdf_interactiveform.cpp b/core/fpdfdoc/cpdf_interactiveform.cpp
index c60468b..00d5aef 100644
--- a/core/fpdfdoc/cpdf_interactiveform.cpp
+++ b/core/fpdfdoc/cpdf_interactiveform.cpp
@@ -673,7 +673,7 @@
return 0;
CPDF_Array* pArray = m_pFormDict->GetArrayFor("CO");
- return pArray ? pArray->size() : 0;
+ return pArray ? fxcrt::CollectionSize<int>(*pArray) : 0;
}
CPDF_FormField* CPDF_InteractiveForm::GetFieldInCalculationOrder(int index) {
diff --git a/core/fpdfdoc/cpdf_linklist.cpp b/core/fpdfdoc/cpdf_linklist.cpp
index 0a4c791..0acd1fa 100644
--- a/core/fpdfdoc/cpdf_linklist.cpp
+++ b/core/fpdfdoc/cpdf_linklist.cpp
@@ -33,7 +33,7 @@
continue;
if (z_order)
- *z_order = annot_index;
+ *z_order = pdfium::base::checked_cast<int32_t>(annot_index);
return link;
}
return CPDF_Link();
diff --git a/core/fpdfdoc/cpdf_nametree.cpp b/core/fpdfdoc/cpdf_nametree.cpp
index 56d58e0..dfd4e61 100644
--- a/core/fpdfdoc/cpdf_nametree.cpp
+++ b/core/fpdfdoc/cpdf_nametree.cpp
@@ -193,8 +193,7 @@
if (ppFind)
*ppFind = pNames;
if (pFindIndex)
- *pFindIndex = pNames->size() / 2 - 1;
-
+ *pFindIndex = fxcrt::CollectionSize<int32_t>(*pNames) / 2 - 1;
return nullptr;
}
}
@@ -210,7 +209,7 @@
if (ppFind)
*ppFind = pNames;
if (pFindIndex)
- *pFindIndex = i;
+ *pFindIndex = pdfium::base::checked_cast<int32_t>(i);
if (iCompare < 0)
continue;
@@ -257,7 +256,7 @@
// The leaf node that holds `key` and `value`.
CPDF_Array* container;
// The index for `key` in `container`. Must be even.
- int index;
+ size_t index;
};
// Find the `nTargetPairIndex` node in the tree with root `pNode`. `nLevel`
@@ -279,7 +278,7 @@
return absl::nullopt;
}
- int index = 2 * (nTargetPairIndex - *nCurPairIndex);
+ size_t index = 2 * (nTargetPairIndex - *nCurPairIndex);
CPDF_Object* value = pNames->GetDirectObjectAt(index + 1);
if (!value)
return absl::nullopt;
diff --git a/core/fpdfdoc/cpdf_structtree.cpp b/core/fpdfdoc/cpdf_structtree.cpp
index 26c3395..6519e1c 100644
--- a/core/fpdfdoc/cpdf_structtree.cpp
+++ b/core/fpdfdoc/cpdf_structtree.cpp
@@ -55,7 +55,7 @@
if (pKids->IsDictionary())
dwKids = 1;
else if (const CPDF_Array* pArray = pKids->AsArray())
- dwKids = pArray->size();
+ dwKids = fxcrt::CollectionSize<uint32_t>(*pArray);
else
return;
diff --git a/core/fpdfdoc/cpvt_section.cpp b/core/fpdfdoc/cpvt_section.cpp
index 4975316..a09a5e9 100644
--- a/core/fpdfdoc/cpvt_section.cpp
+++ b/core/fpdfdoc/cpvt_section.cpp
@@ -232,7 +232,8 @@
CPVT_WordPlace CPVT_Section::AddLine(const CPVT_LineInfo& lineinfo) {
m_LineArray.push_back(std::make_unique<Line>(lineinfo));
- return CPVT_WordPlace(m_SecPlace.nSecIndex, m_LineArray.size() - 1, -1);
+ return CPVT_WordPlace(m_SecPlace.nSecIndex,
+ fxcrt::CollectionSize<int32_t>(m_LineArray) - 1, -1);
}
CPVT_FloatRect CPVT_Section::Rearrange() {
diff --git a/core/fpdfdoc/cpvt_variabletext.cpp b/core/fpdfdoc/cpvt_variabletext.cpp
index 9dff62a..c14dda1 100644
--- a/core/fpdfdoc/cpvt_variabletext.cpp
+++ b/core/fpdfdoc/cpvt_variabletext.cpp
@@ -15,6 +15,7 @@
#include "core/fpdfdoc/cpvt_wordinfo.h"
#include "core/fpdfdoc/ipvt_fontmap.h"
#include "core/fxcrt/fx_codepage.h"
+#include "core/fxcrt/fx_safe_types.h"
#include "core/fxcrt/stl_util.h"
#include "third_party/base/check.h"
#include "third_party/base/compiler_specific.h"
@@ -266,11 +267,11 @@
if (!m_SectionArray.empty())
m_SectionArray.front()->SetRect(CPVT_FloatRect());
- int32_t nCharCount = 0;
- for (int32_t i = 0, sz = swText.GetLength(); i < sz; i++) {
- if (m_nLimitChar > 0 && nCharCount >= m_nLimitChar)
+ FX_SAFE_INT32 nCharCount = 0;
+ for (size_t i = 0, sz = swText.GetLength(); i < sz; i++) {
+ if (m_nLimitChar > 0 && nCharCount.ValueOrDie() >= m_nLimitChar)
break;
- if (m_nCharArray > 0 && nCharCount >= m_nCharArray)
+ if (m_nCharArray > 0 && nCharCount.ValueOrDie() >= m_nCharArray)
break;
uint16_t word = swText[i];