Use signed widths consistently in CFGAS_TxtBreak. CFGAS_TxtBreak::AppendChar_Combination() may return a negative width for combining characters, so ensure it isn't cast back-and-forth to size_t. Bug: chromium:1333307 Change-Id: Ibf05bb9159f6ab233069c43296e0e88156f94333 Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/94230 Commit-Queue: Tom Sepez <tsepez@chromium.org> Reviewed-by: Tom Sepez <tsepez@chromium.org> Reviewed-by: Lei Zhang <thestig@chromium.org>
diff --git a/xfa/fde/cfde_texteditengine.cpp b/xfa/fde/cfde_texteditengine.cpp index 05823e8..42c07ab 100644 --- a/xfa/fde/cfde_texteditengine.cpp +++ b/xfa/fde/cfde_texteditengine.cpp
@@ -896,7 +896,7 @@ : content_[gap_position_ + gap_size_ + (idx - gap_position_)]; } -size_t CFDE_TextEditEngine::GetWidthOfChar(size_t idx) { +int32_t CFDE_TextEditEngine::GetWidthOfChar(size_t idx) { // Recalculate the widths if necessary. Layout(); return idx < char_widths_.size() ? char_widths_[idx] : 0;
diff --git a/xfa/fde/cfde_texteditengine.h b/xfa/fde/cfde_texteditengine.h index 9195191..00aa2d8 100644 --- a/xfa/fde/cfde_texteditengine.h +++ b/xfa/fde/cfde_texteditengine.h
@@ -90,7 +90,7 @@ // CFGAS_TxtBreak::Engine: wchar_t GetChar(size_t idx) const override; - size_t GetWidthOfChar(size_t idx) override; + int32_t GetWidthOfChar(size_t idx) override; void SetDelegate(Delegate* delegate) { delegate_ = delegate; } void Clear(); @@ -213,7 +213,7 @@ CFX_RectF contents_bounding_box_; UnownedPtr<Delegate> delegate_; std::vector<FDE_TEXTEDITPIECE> text_piece_info_; - std::vector<size_t> char_widths_; + std::vector<int32_t> char_widths_; // May be negative for combining chars. CFGAS_TxtBreak text_break_; RetainPtr<CFGAS_GEFont> font_; FX_ARGB font_color_ = 0xff000000;
diff --git a/xfa/fde/cfde_texteditengine_unittest.cpp b/xfa/fde/cfde_texteditengine_unittest.cpp index 36c4d35..380d91f 100644 --- a/xfa/fde/cfde_texteditengine_unittest.cpp +++ b/xfa/fde/cfde_texteditengine_unittest.cpp
@@ -265,14 +265,14 @@ TEST_F(CFDE_TextEditEngineTest, GetWidthOfChar) { // Out of Bounds. - EXPECT_EQ(0U, engine()->GetWidthOfChar(0)); + EXPECT_EQ(0, engine()->GetWidthOfChar(0)); engine()->Insert(0, L"Hello World"); - EXPECT_EQ(173280U, engine()->GetWidthOfChar(0)); - EXPECT_EQ(133440U, engine()->GetWidthOfChar(1)); + EXPECT_EQ(173280, engine()->GetWidthOfChar(0)); + EXPECT_EQ(133440, engine()->GetWidthOfChar(1)); engine()->Insert(0, L"\t"); - EXPECT_EQ(0U, engine()->GetWidthOfChar(0)); + EXPECT_EQ(0, engine()->GetWidthOfChar(0)); } TEST_F(CFDE_TextEditEngineTest, GetDisplayPos) {
diff --git a/xfa/fgas/layout/cfgas_txtbreak.cpp b/xfa/fgas/layout/cfgas_txtbreak.cpp index 6d677ef..fc2dc7b 100644 --- a/xfa/fgas/layout/cfgas_txtbreak.cpp +++ b/xfa/fgas/layout/cfgas_txtbreak.cpp
@@ -677,8 +677,7 @@ wchar_t wch; if (pEngine) { wch = pEngine->GetChar(iAbsolute); - iWidth = pdfium::base::checked_cast<int32_t>( - pEngine->GetWidthOfChar(iAbsolute)); + iWidth = pEngine->GetWidthOfChar(iAbsolute); } else { wch = *pStr++; iWidth = *pWidths++; @@ -905,8 +904,7 @@ if (pEngine) { int32_t iAbsolute = i + run.iStart; wch = pEngine->GetChar(iAbsolute); - iCharSize = pdfium::base::checked_cast<int32_t>( - pEngine->GetWidthOfChar(iAbsolute)); + iCharSize = pEngine->GetWidthOfChar(iAbsolute); } else { wch = *pStr++; iCharSize = *pWidths++;
diff --git a/xfa/fgas/layout/cfgas_txtbreak.h b/xfa/fgas/layout/cfgas_txtbreak.h index 283e8c6..618853e 100644 --- a/xfa/fgas/layout/cfgas_txtbreak.h +++ b/xfa/fgas/layout/cfgas_txtbreak.h
@@ -39,8 +39,9 @@ public: virtual ~Engine(); virtual wchar_t GetChar(size_t idx) const = 0; - // Non-const so we can force a layout if needed. - virtual size_t GetWidthOfChar(size_t idx) = 0; + // May return negative for combining characters. Non-const so we can force + // a layout if needed. + virtual int32_t GetWidthOfChar(size_t idx) = 0; }; struct Run {