From 193e6ca5e48ee99e620f0e7546f1407ba1a20323 Mon Sep 17 00:00:00 2001 From: Tom Sepez Date: Tue, 14 Mar 2017 15:53:36 -0700 Subject: Add IndexInBounds() convenience routine. Avoid writing |Type| in CollectionSize() so that index type can change without rewriting conditions. Change-Id: I40c94ca39148b379908760ba9b861114b88af7bb Reviewed-on: https://pdfium-review.googlesource.com/3056 Reviewed-by: Lei Zhang Commit-Queue: Tom Sepez --- core/fpdfapi/font/cpdf_font.cpp | 5 ++--- core/fpdfapi/font/ttgsubtable.cpp | 6 ++---- core/fpdfapi/page/cpdf_pageobjectlist.cpp | 4 +--- core/fpdfapi/parser/cpdf_document.cpp | 6 +++--- 4 files changed, 8 insertions(+), 13 deletions(-) (limited to 'core/fpdfapi') diff --git a/core/fpdfapi/font/cpdf_font.cpp b/core/fpdfapi/font/cpdf_font.cpp index 7988ec7124..f0b5ac16b7 100644 --- a/core/fpdfapi/font/cpdf_font.cpp +++ b/core/fpdfapi/font/cpdf_font.cpp @@ -458,10 +458,9 @@ uint32_t CPDF_Font::FallbackFontFromCharcode(uint32_t charcode) { } int CPDF_Font::FallbackGlyphFromCharcode(int fallbackFont, uint32_t charcode) { - if (fallbackFont < 0 || - fallbackFont >= pdfium::CollectionSize(m_FontFallbacks)) { + if (!pdfium::IndexInBounds(m_FontFallbacks, fallbackFont)) return -1; - } + int glyph = FXFT_Get_Char_Index(m_FontFallbacks[fallbackFont]->GetFace(), charcode); if (glyph == 0 || glyph == 0xffff) diff --git a/core/fpdfapi/font/ttgsubtable.cpp b/core/fpdfapi/font/ttgsubtable.cpp index 946ccd7abe..4fae5d41d2 100644 --- a/core/fpdfapi/font/ttgsubtable.cpp +++ b/core/fpdfapi/font/ttgsubtable.cpp @@ -127,9 +127,8 @@ bool CFX_CTTGSUBTable::GetVerticalGlyphSub(uint32_t glyphnum, uint32_t* vglyphnum, TFeature* Feature) { for (int index : Feature->LookupListIndices) { - if (index < 0 || index >= pdfium::CollectionSize(LookupList.Lookups)) + if (!pdfium::IndexInBounds(LookupList.Lookups, index)) continue; - if (LookupList.Lookups[index].LookupType == 1 && GetVerticalGlyphSub2(glyphnum, vglyphnum, &LookupList.Lookups[index])) { return true; @@ -154,8 +153,7 @@ bool CFX_CTTGSUBTable::GetVerticalGlyphSub2(uint32_t glyphnum, case 2: { auto* tbl2 = static_cast(subTable.get()); int index = GetCoverageIndex(tbl2->Coverage.get(), glyphnum); - if (index >= 0 && - index < pdfium::CollectionSize(tbl2->Substitutes)) { + if (pdfium::IndexInBounds(tbl2->Substitutes, index)) { *vglyphnum = tbl2->Substitutes[index]; return true; } diff --git a/core/fpdfapi/page/cpdf_pageobjectlist.cpp b/core/fpdfapi/page/cpdf_pageobjectlist.cpp index 02b590e413..5c30fe9877 100644 --- a/core/fpdfapi/page/cpdf_pageobjectlist.cpp +++ b/core/fpdfapi/page/cpdf_pageobjectlist.cpp @@ -10,7 +10,5 @@ #include "third_party/base/stl_util.h" CPDF_PageObject* CPDF_PageObjectList::GetPageObjectByIndex(int index) { - if (index < 0 || index >= pdfium::CollectionSize(*this)) - return nullptr; - return (*this)[index].get(); + return pdfium::IndexInBounds(*this, index) ? (*this)[index].get() : nullptr; } diff --git a/core/fpdfapi/parser/cpdf_document.cpp b/core/fpdfapi/parser/cpdf_document.cpp index ed88a4f82a..8ff9e66506 100644 --- a/core/fpdfapi/parser/cpdf_document.cpp +++ b/core/fpdfapi/parser/cpdf_document.cpp @@ -477,10 +477,10 @@ bool CPDF_Document::IsPageLoaded(int iPage) const { } CPDF_Dictionary* CPDF_Document::GetPage(int iPage) { - if (iPage < 0 || iPage >= pdfium::CollectionSize(m_PageList)) + if (!pdfium::IndexInBounds(m_PageList, iPage)) return nullptr; - if (m_bLinearized && (iPage == m_iFirstPageNo)) { + if (m_bLinearized && iPage == m_iFirstPageNo) { if (CPDF_Dictionary* pDict = ToDictionary(GetOrParseIndirectObject(m_dwFirstPageObjNum))) { return pDict; @@ -586,7 +586,7 @@ int CPDF_Document::GetPageIndex(uint32_t objnum) { int found_index = FindPageIndex(pPages, &skip_count, objnum, &start_index); // Corrupt page tree may yield out-of-range results. - if (found_index < 0 || found_index >= pdfium::CollectionSize(m_PageList)) + if (!pdfium::IndexInBounds(m_PageList, found_index)) return -1; m_PageList[found_index] = objnum; -- cgit v1.2.3