From 9494421208674d2c57a9f864d342f017c0b20902 Mon Sep 17 00:00:00 2001 From: Lei Zhang Date: Mon, 17 Aug 2015 12:20:05 -0700 Subject: Fix more sign comparison errors. R=tsepez@chromium.org Review URL: https://codereview.chromium.org/1290383003 . --- core/include/fpdfapi/fpdf_resource.h | 2 +- core/src/fpdfdoc/doc_ap.cpp | 37 +++++++++++++---------- fpdfsdk/src/fpdftext_embeddertest.cpp | 8 +++-- fpdfsdk/src/javascript/PublicMethods.cpp | 51 ++++++++++++-------------------- 4 files changed, 47 insertions(+), 51 deletions(-) diff --git a/core/include/fpdfapi/fpdf_resource.h b/core/include/fpdfapi/fpdf_resource.h index 5b6f3498fe..d687c97b9c 100644 --- a/core/include/fpdfapi/fpdf_resource.h +++ b/core/include/fpdfapi/fpdf_resource.h @@ -21,7 +21,6 @@ class CPDF_CMap; class CPDF_Color; class CPDF_ColorSpace; class CPDF_Face; -class CPDF_Font; class CPDF_FontEncoding; class CPDF_Form; class CPDF_Function; @@ -101,6 +100,7 @@ class CPDF_Font { CPDF_Dictionary* pFontDict); static CPDF_Font* GetStockFont(CPDF_Document* pDoc, const CFX_ByteStringC& fontname); + static const FX_DWORD kInvalidCharCode = static_cast(-1); virtual ~CPDF_Font(); diff --git a/core/src/fpdfdoc/doc_ap.cpp b/core/src/fpdfdoc/doc_ap.cpp index cffaad9900..f9bb3a7641 100644 --- a/core/src/fpdfdoc/doc_ap.cpp +++ b/core/src/fpdfdoc/doc_ap.cpp @@ -133,7 +133,7 @@ int32_t CPVT_Provider::GetCharWidth(int32_t nFontIndex, int32_t nWordStyle) { if (CPDF_Font* pPDFFont = m_pFontMap->GetPDFFont(nFontIndex)) { FX_DWORD charcode = pPDFFont->CharCodeFromUnicode(word); - if (charcode != -1) { + if (charcode != CPDF_Font::kInvalidCharCode) { return pPDFFont->GetCharWidthF(charcode); } } @@ -155,14 +155,15 @@ int32_t CPVT_Provider::GetWordFontIndex(FX_WORD word, int32_t charset, int32_t nFontIndex) { if (CPDF_Font* pDefFont = m_pFontMap->GetPDFFont(0)) { - if (pDefFont->CharCodeFromUnicode(word) != -1) { + if (pDefFont->CharCodeFromUnicode(word) != CPDF_Font::kInvalidCharCode) { return 0; } } - if (CPDF_Font* pSysFont = m_pFontMap->GetPDFFont(1)) - if (pSysFont->CharCodeFromUnicode(word) != -1) { + if (CPDF_Font* pSysFont = m_pFontMap->GetPDFFont(1)) { + if (pSysFont->CharCodeFromUnicode(word) != CPDF_Font::kInvalidCharCode) { return 1; } + } return -1; } FX_BOOL CPVT_Provider::IsLatinWord(FX_WORD word) { @@ -175,6 +176,7 @@ FX_BOOL CPVT_Provider::IsLatinWord(FX_WORD word) { int32_t CPVT_Provider::GetDefaultFontIndex() { return 0; } + static CFX_ByteString GetPDFWordString(IPVT_FontMap* pFontMap, int32_t nFontIndex, FX_WORD Word, @@ -182,23 +184,26 @@ static CFX_ByteString GetPDFWordString(IPVT_FontMap* pFontMap, CFX_ByteString sWord; if (SubWord > 0) { sWord.Format("%c", SubWord); - } else { - if (pFontMap) { - if (CPDF_Font* pPDFFont = pFontMap->GetPDFFont(nFontIndex)) { - if (pPDFFont->GetBaseFont().Compare("Symbol") == 0 || - pPDFFont->GetBaseFont().Compare("ZapfDingbats") == 0) { - sWord.Format("%c", Word); - } else { - FX_DWORD dwCharCode = pPDFFont->CharCodeFromUnicode(Word); - if (dwCharCode != -1) { - pPDFFont->AppendChar(sWord, dwCharCode); - } - } + return sWord; + } + + if (!pFontMap) + return sWord; + + if (CPDF_Font* pPDFFont = pFontMap->GetPDFFont(nFontIndex)) { + if (pPDFFont->GetBaseFont().Compare("Symbol") == 0 || + pPDFFont->GetBaseFont().Compare("ZapfDingbats") == 0) { + sWord.Format("%c", Word); + } else { + FX_DWORD dwCharCode = pPDFFont->CharCodeFromUnicode(Word); + if (dwCharCode != CPDF_Font::kInvalidCharCode) { + pPDFFont->AppendChar(sWord, dwCharCode); } } } return sWord; } + static CFX_ByteString GetWordRenderString(const CFX_ByteString& strWords) { if (strWords.GetLength() > 0) { return PDF_EncodeString(strWords) + " Tj\n"; diff --git a/fpdfsdk/src/fpdftext_embeddertest.cpp b/fpdfsdk/src/fpdftext_embeddertest.cpp index dd3737a40e..b3fe9e4c1e 100644 --- a/fpdfsdk/src/fpdftext_embeddertest.cpp +++ b/fpdfsdk/src/fpdftext_embeddertest.cpp @@ -40,13 +40,17 @@ TEST_F(FPDFTextEmbeddertest, Text) { memset(fixed_buffer, 0xbd, sizeof(fixed_buffer)); // Check includes the terminating NUL that is provided. - EXPECT_EQ(sizeof(expected), FPDFText_GetText(textpage, 0, 128, fixed_buffer)); + int num_chars = FPDFText_GetText(textpage, 0, 128, fixed_buffer); + ASSERT_GE(num_chars, 0); + EXPECT_EQ(sizeof(expected), static_cast(num_chars)); EXPECT_TRUE(check_unsigned_shorts(expected, fixed_buffer, sizeof(expected))); // Count does not include the terminating NUL in the string literal. EXPECT_EQ(sizeof(expected) - 1, FPDFText_CountChars(textpage)); for (size_t i = 0; i < sizeof(expected) - 1; ++i) { - EXPECT_EQ(expected[i], FPDFText_GetUnicode(textpage, i)) << " at " << i; + EXPECT_EQ(static_cast(expected[i]), + FPDFText_GetUnicode(textpage, i)) + << " at " << i; } EXPECT_EQ(12.0, FPDFText_GetFontSize(textpage, 0)); diff --git a/fpdfsdk/src/javascript/PublicMethods.cpp b/fpdfsdk/src/javascript/PublicMethods.cpp index d0ecd2987e..c83ad05409 100644 --- a/fpdfsdk/src/javascript/PublicMethods.cpp +++ b/fpdfsdk/src/javascript/PublicMethods.cpp @@ -1496,12 +1496,9 @@ FX_BOOL CJS_PublicMethods::AFDate_Format(IFXJS_Context* cc, L"m/d/yy h:MM tt", L"m/d/yy HH:MM"}; - ASSERT(iIndex < FX_ArraySize(cFormats)); - - if (iIndex < 0) - iIndex = 0; - if (iIndex >= FX_ArraySize(cFormats)) + if (iIndex < 0 || (static_cast(iIndex) >= FX_ArraySize(cFormats))) iIndex = 0; + CJS_Parameters newParams; CJS_Value val(isolate, cFormats[iIndex]); newParams.push_back(val); @@ -1539,12 +1536,9 @@ FX_BOOL CJS_PublicMethods::AFDate_Keystroke(IFXJS_Context* cc, L"m/d/yy h:MM tt", L"m/d/yy HH:MM"}; - ASSERT(iIndex < FX_ArraySize(cFormats)); - - if (iIndex < 0) - iIndex = 0; - if (iIndex >= FX_ArraySize(cFormats)) + if (iIndex < 0 || (static_cast(iIndex) >= FX_ArraySize(cFormats))) iIndex = 0; + CJS_Parameters newParams; CJS_Value val(isolate, cFormats[iIndex]); newParams.push_back(val); @@ -1569,12 +1563,9 @@ FX_BOOL CJS_PublicMethods::AFTime_Format(IFXJS_Context* cc, const FX_WCHAR* cFormats[] = {L"HH:MM", L"h:MM tt", L"HH:MM:ss", L"h:MM:ss tt"}; - ASSERT(iIndex < FX_ArraySize(cFormats)); - - if (iIndex < 0) - iIndex = 0; - if (iIndex >= FX_ArraySize(cFormats)) + if (iIndex < 0 || (static_cast(iIndex) >= FX_ArraySize(cFormats))) iIndex = 0; + CJS_Parameters newParams; CJS_Value val(isolate, cFormats[iIndex]); newParams.push_back(val); @@ -1597,12 +1588,9 @@ FX_BOOL CJS_PublicMethods::AFTime_Keystroke(IFXJS_Context* cc, const FX_WCHAR* cFormats[] = {L"HH:MM", L"h:MM tt", L"HH:MM:ss", L"h:MM:ss tt"}; - ASSERT(iIndex < FX_ArraySize(cFormats)); - - if (iIndex < 0) - iIndex = 0; - if (iIndex >= FX_ArraySize(cFormats)) + if (iIndex < 0 || (static_cast(iIndex) >= FX_ArraySize(cFormats))) iIndex = 0; + CJS_Parameters newParams; CJS_Value val(isolate, cFormats[iIndex]); newParams.push_back(val); @@ -1698,22 +1686,21 @@ FX_BOOL CJS_PublicMethods::AFSpecial_KeystrokeEx(IFXJS_Context* cc, if (wstrMask.IsEmpty()) return TRUE; - std::wstring wstrValue = valEvent.c_str(); + const size_t wstrMaskLen = wstrMask.GetLength(); + const std::wstring wstrValue = valEvent.c_str(); if (pEvent->WillCommit()) { if (wstrValue.empty()) return TRUE; - int iIndexMask = 0; - for (std::wstring::iterator it = wstrValue.begin(); it != wstrValue.end(); - it++) { - wchar_t w_Value = *it; + size_t iIndexMask = 0; + for (const auto& w_Value : wstrValue) { if (!maskSatisfied(w_Value, wstrMask[iIndexMask])) break; iIndexMask++; } - if (iIndexMask != wstrMask.GetLength() || - (iIndexMask != wstrValue.size() && wstrMask.GetLength() != 0)) { + if (iIndexMask != wstrMaskLen || + (iIndexMask != wstrValue.size() && wstrMaskLen != 0)) { Alert( pContext, JSGetStringFromID(pContext, IDS_STRING_JSAFNUMBER_KEYSTROKE).c_str()); @@ -1729,16 +1716,16 @@ FX_BOOL CJS_PublicMethods::AFSpecial_KeystrokeEx(IFXJS_Context* cc, int iIndexMask = pEvent->SelStart(); - if (wstrValue.length() - (pEvent->SelEnd() - pEvent->SelStart()) + - wChange.length() > - (FX_DWORD)wstrMask.GetLength()) { + size_t combined_len = wstrValue.length() + wChange.length() - + (pEvent->SelEnd() - pEvent->SelStart()); + if (combined_len > wstrMaskLen) { Alert(pContext, JSGetStringFromID(pContext, IDS_STRING_JSPARAM_TOOLONG).c_str()); pEvent->Rc() = FALSE; return TRUE; } - if (iIndexMask >= wstrMask.GetLength() && (!wChange.empty())) { + if (iIndexMask >= wstrMaskLen && (!wChange.empty())) { Alert(pContext, JSGetStringFromID(pContext, IDS_STRING_JSPARAM_TOOLONG).c_str()); pEvent->Rc() = FALSE; @@ -1746,7 +1733,7 @@ FX_BOOL CJS_PublicMethods::AFSpecial_KeystrokeEx(IFXJS_Context* cc, } for (std::wstring::iterator it = wChange.begin(); it != wChange.end(); it++) { - if (iIndexMask >= wstrMask.GetLength()) { + if (iIndexMask >= wstrMaskLen) { Alert(pContext, JSGetStringFromID(pContext, IDS_STRING_JSPARAM_TOOLONG).c_str()); pEvent->Rc() = FALSE; -- cgit v1.2.3