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 --- fpdfsdk/fpdfxfa/cpdfxfa_context.cpp | 13 ++++--------- fpdfsdk/pdfwindow/PWL_FontMap.cpp | 29 ++++++++++------------------- 2 files changed, 14 insertions(+), 28 deletions(-) (limited to 'fpdfsdk') diff --git a/fpdfsdk/fpdfxfa/cpdfxfa_context.cpp b/fpdfsdk/fpdfxfa/cpdfxfa_context.cpp index 88c88a1748..aaa0e9849b 100644 --- a/fpdfsdk/fpdfxfa/cpdfxfa_context.cpp +++ b/fpdfsdk/fpdfxfa/cpdfxfa_context.cpp @@ -172,10 +172,8 @@ CPDFXFA_Page* CPDFXFA_Context::GetXFAPage(int page_index) { pPage->Release(); return nullptr; } - if (page_index >= 0 && - page_index < pdfium::CollectionSize(m_XFAPageList)) { + if (pdfium::IndexInBounds(m_XFAPageList, page_index)) m_XFAPageList[page_index] = pPage; - } return pPage; } @@ -203,20 +201,17 @@ void CPDFXFA_Context::DeletePage(int page_index) { if (m_pPDFDoc) m_pPDFDoc->DeletePage(page_index); - if (page_index < 0 || - page_index >= pdfium::CollectionSize(m_XFAPageList)) { + if (!pdfium::IndexInBounds(m_XFAPageList, page_index)) return; - } + if (CPDFXFA_Page* pPage = m_XFAPageList[page_index]) pPage->Release(); } void CPDFXFA_Context::RemovePage(CPDFXFA_Page* page) { int page_index = page->GetPageIndex(); - if (page_index >= 0 && - page_index < pdfium::CollectionSize(m_XFAPageList)) { + if (pdfium::IndexInBounds(m_XFAPageList, page_index)) m_XFAPageList[page_index] = nullptr; - } } void CPDFXFA_Context::ClearChangeMark() { diff --git a/fpdfsdk/pdfwindow/PWL_FontMap.cpp b/fpdfsdk/pdfwindow/PWL_FontMap.cpp index 9a2962cd56..a7c42698cd 100644 --- a/fpdfsdk/pdfwindow/PWL_FontMap.cpp +++ b/fpdfsdk/pdfwindow/PWL_FontMap.cpp @@ -55,32 +55,26 @@ CPDF_Document* CPWL_FontMap::GetDocument() { m_pPDFDoc->CreateNewDoc(); } } - return m_pPDFDoc.get(); } CPDF_Font* CPWL_FontMap::GetPDFFont(int32_t nFontIndex) { - if (nFontIndex >= 0 && nFontIndex < pdfium::CollectionSize(m_Data)) { - if (m_Data[nFontIndex]) - return m_Data[nFontIndex]->pFont; - } + if (pdfium::IndexInBounds(m_Data, nFontIndex) && m_Data[nFontIndex]) + return m_Data[nFontIndex]->pFont; + return nullptr; } CFX_ByteString CPWL_FontMap::GetPDFFontAlias(int32_t nFontIndex) { - if (nFontIndex >= 0 && nFontIndex < pdfium::CollectionSize(m_Data)) { - if (m_Data[nFontIndex]) - return m_Data[nFontIndex]->sFontName; - } + if (pdfium::IndexInBounds(m_Data, nFontIndex) && m_Data[nFontIndex]) + return m_Data[nFontIndex]->sFontName; + return CFX_ByteString(); } bool CPWL_FontMap::KnowWord(int32_t nFontIndex, uint16_t word) { - if (nFontIndex >= 0 && nFontIndex < pdfium::CollectionSize(m_Data)) { - if (m_Data[nFontIndex]) - return CharCodeFromUnicode(nFontIndex, word) >= 0; - } - return false; + return pdfium::IndexInBounds(m_Data, nFontIndex) && m_Data[nFontIndex] && + CharCodeFromUnicode(nFontIndex, word) >= 0; } int32_t CPWL_FontMap::GetWordFontIndex(uint16_t word, @@ -116,7 +110,7 @@ int32_t CPWL_FontMap::GetWordFontIndex(uint16_t word, } int32_t CPWL_FontMap::CharCodeFromUnicode(int32_t nFontIndex, uint16_t word) { - if (nFontIndex < 0 || nFontIndex >= pdfium::CollectionSize(m_Data)) + if (!pdfium::IndexInBounds(m_Data, nFontIndex)) return -1; CPWL_FontMap_Data* pData = m_Data[nFontIndex].get(); @@ -284,10 +278,7 @@ CFX_ByteString CPWL_FontMap::EncodeFontAlias(const CFX_ByteString& sFontName) { } const CPWL_FontMap_Data* CPWL_FontMap::GetFontMapData(int32_t nIndex) const { - if (nIndex < 0 || nIndex >= pdfium::CollectionSize(m_Data)) - return nullptr; - - return m_Data[nIndex].get(); + return pdfium::IndexInBounds(m_Data, nIndex) ? m_Data[nIndex].get() : nullptr; } int32_t CPWL_FontMap::GetNativeCharset() { -- cgit v1.2.3