From 33805cc811c722a0c5cd439cff419de252cd39c9 Mon Sep 17 00:00:00 2001 From: Ryan Harrison Date: Thu, 27 Jul 2017 16:28:44 -0400 Subject: Remove single param Mid() method from string classes This support is being removed from CFX_ByteString, CFX_ByteStringC, CFX_WideString, and CFX_WideStringC. This standardizes all of these classes to only have one Mid method that takes in 2 params, offset and count. Count now must be positive. The old behaviour of calculating the length for the user if -1 is passed in for the count has been removed. This work is in preperation for converting these classes to not accept negative lengths anywhere and thus make the underlying size type unsigned. BUG=pdfium:828 Change-Id: I5f15e7b7b00b264231817f143e2da88ee6f69e7b Reviewed-on: https://pdfium-review.googlesource.com/9430 Reviewed-by: (OOO Jul 28 - Aug 8) dsinclair Reviewed-by: Tom Sepez Commit-Queue: Ryan Harrison --- core/fpdfapi/font/cpdf_simplefont.cpp | 2 +- core/fpdfapi/page/cpdf_streamcontentparser.cpp | 11 +++++++---- core/fpdfdoc/cpvt_generateap.cpp | 8 +++++--- core/fxcrt/cfx_bytestring.cpp | 10 +++------- core/fxcrt/cfx_bytestring.h | 1 - core/fxcrt/cfx_bytestring_unittest.cpp | 11 ++++------- core/fxcrt/cfx_string_c_template.h | 19 +++++++++++-------- core/fxcrt/cfx_widestring.cpp | 9 ++------- core/fxcrt/cfx_widestring.h | 1 - core/fxcrt/cfx_widestring_unittest.cpp | 11 ++++------- core/fxcrt/xml/cxml_parser.cpp | 2 +- core/fxge/cfx_fontmapper.cpp | 6 +++--- 12 files changed, 41 insertions(+), 50 deletions(-) (limited to 'core') diff --git a/core/fpdfapi/font/cpdf_simplefont.cpp b/core/fpdfapi/font/cpdf_simplefont.cpp index a6c3ba0547..8c8520f0c4 100644 --- a/core/fpdfapi/font/cpdf_simplefont.cpp +++ b/core/fpdfapi/font/cpdf_simplefont.cpp @@ -130,7 +130,7 @@ bool CPDF_SimpleFont::LoadCommon() { } if (m_pFontFile) { if (m_BaseFont.GetLength() > 8 && m_BaseFont[7] == '+') - m_BaseFont = m_BaseFont.Mid(8); + m_BaseFont = m_BaseFont.Mid(8, m_BaseFont.GetLength() - 8); } else { LoadSubstFont(); } diff --git a/core/fpdfapi/page/cpdf_streamcontentparser.cpp b/core/fpdfapi/page/cpdf_streamcontentparser.cpp index e00dbc54fd..5f571aa8d6 100644 --- a/core/fpdfapi/page/cpdf_streamcontentparser.cpp +++ b/core/fpdfapi/page/cpdf_streamcontentparser.cpp @@ -619,7 +619,8 @@ void CPDF_StreamContentParser::Handle_BeginImage() { if (type != CPDF_StreamParser::Name) { break; } - CFX_ByteString key(m_pSyntax->GetWord().Mid(1)); + auto word = m_pSyntax->GetWord(); + CFX_ByteString key(word.Mid(1, word.GetLength() - 1)); auto pObj = m_pSyntax->ReadNextObject(false, false, 0); if (!key.IsEmpty()) { uint32_t dwObjNum = pObj ? pObj->GetObjNum() : 0; @@ -1185,7 +1186,7 @@ CPDF_ColorSpace* CPDF_StreamContentParser::FindColorSpace( } if (name == "DeviceGray" || name == "DeviceCMYK" || name == "DeviceRGB") { CFX_ByteString defname = "Default"; - defname += name.Mid(7); + defname += name.Mid(7, name.GetLength() - 7); CPDF_Object* pDefObj = FindResourceObj("ColorSpace", defname); if (!pDefObj) { if (name == "DeviceGray") { @@ -1522,9 +1523,11 @@ uint32_t CPDF_StreamContentParser::Parse(const uint8_t* pData, case CPDF_StreamParser::Number: AddNumberParam(syntax.GetWord()); break; - case CPDF_StreamParser::Name: - AddNameParam(syntax.GetWord().Mid(1)); + case CPDF_StreamParser::Name: { + auto word = syntax.GetWord(); + AddNameParam(word.Mid(1, word.GetLength() - 1)); break; + } default: AddObjectParam(syntax.GetObject()); } diff --git a/core/fpdfdoc/cpvt_generateap.cpp b/core/fpdfdoc/cpvt_generateap.cpp index eb874b3352..83c1f65587 100644 --- a/core/fpdfdoc/cpvt_generateap.cpp +++ b/core/fpdfdoc/cpvt_generateap.cpp @@ -65,15 +65,17 @@ bool GenerateWidgetAP(CPDF_Document* pDoc, if (!pDRFontDict) return false; - CPDF_Dictionary* pFontDict = pDRFontDict->GetDictFor(sFontName.Mid(1)); + CPDF_Dictionary* pFontDict = + pDRFontDict->GetDictFor(sFontName.Mid(1, sFontName.GetLength() - 1)); if (!pFontDict) { pFontDict = pDoc->NewIndirect(); pFontDict->SetNewFor("Type", "Font"); pFontDict->SetNewFor("Subtype", "Type1"); pFontDict->SetNewFor("BaseFont", "Helvetica"); pFontDict->SetNewFor("Encoding", "WinAnsiEncoding"); - pDRFontDict->SetNewFor(sFontName.Mid(1), pDoc, - pFontDict->GetObjNum()); + pDRFontDict->SetNewFor( + sFontName.Mid(1, sFontName.GetLength() - 1), pDoc, + pFontDict->GetObjNum()); } CPDF_Font* pDefFont = pDoc->LoadFont(pFontDict); if (!pDefFont) diff --git a/core/fxcrt/cfx_bytestring.cpp b/core/fxcrt/cfx_bytestring.cpp index c88ad51ab3..e7e55de8e3 100644 --- a/core/fxcrt/cfx_bytestring.cpp +++ b/core/fxcrt/cfx_bytestring.cpp @@ -121,6 +121,7 @@ static_assert(sizeof(CFX_ByteString) <= sizeof(char*), "Strings must not require more space than pointers"); CFX_ByteString::CFX_ByteString(const char* pStr, FX_STRSIZE nLen) { + ASSERT(nLen >= 0); if (nLen < 0) nLen = pStr ? FXSYS_strlen(pStr) : 0; @@ -129,6 +130,7 @@ CFX_ByteString::CFX_ByteString(const char* pStr, FX_STRSIZE nLen) { } CFX_ByteString::CFX_ByteString(const uint8_t* pStr, FX_STRSIZE nLen) { + ASSERT(nLen >= 0); if (nLen > 0) { m_pData.Reset( StringData::Create(reinterpret_cast(pStr), nLen)); @@ -456,14 +458,8 @@ void CFX_ByteString::Concat(const char* pSrcData, FX_STRSIZE nSrcLen) { m_pData.Swap(pNewData); } -CFX_ByteString CFX_ByteString::Mid(FX_STRSIZE nFirst) const { - if (!m_pData) - return CFX_ByteString(); - - return Mid(nFirst, m_pData->m_nDataLength - nFirst); -} - CFX_ByteString CFX_ByteString::Mid(FX_STRSIZE nFirst, FX_STRSIZE nCount) const { + ASSERT(nCount >= 0); if (!m_pData) return CFX_ByteString(); diff --git a/core/fxcrt/cfx_bytestring.h b/core/fxcrt/cfx_bytestring.h index abf8ac80c8..519cee39d4 100644 --- a/core/fxcrt/cfx_bytestring.h +++ b/core/fxcrt/cfx_bytestring.h @@ -124,7 +124,6 @@ class CFX_ByteString { char* GetBuffer(FX_STRSIZE len); void ReleaseBuffer(FX_STRSIZE len = -1); - CFX_ByteString Mid(FX_STRSIZE first) const; CFX_ByteString Mid(FX_STRSIZE first, FX_STRSIZE count) const; CFX_ByteString Left(FX_STRSIZE count) const; CFX_ByteString Right(FX_STRSIZE count) const; diff --git a/core/fxcrt/cfx_bytestring_unittest.cpp b/core/fxcrt/cfx_bytestring_unittest.cpp index f5d9413b7d..bcf6f7495e 100644 --- a/core/fxcrt/cfx_bytestring_unittest.cpp +++ b/core/fxcrt/cfx_bytestring_unittest.cpp @@ -440,10 +440,10 @@ TEST(fxcrt, ByteStringMid) { CFX_ByteString fred("FRED"); EXPECT_EQ("", fred.Mid(0, 0)); EXPECT_EQ("", fred.Mid(3, 0)); - EXPECT_EQ("FRED", fred.Mid(0)); - EXPECT_EQ("RED", fred.Mid(1)); - EXPECT_EQ("ED", fred.Mid(2)); - EXPECT_EQ("D", fred.Mid(3)); + EXPECT_EQ("FRED", fred.Mid(0, 4)); + EXPECT_EQ("RED", fred.Mid(1, 3)); + EXPECT_EQ("ED", fred.Mid(2, 2)); + EXPECT_EQ("D", fred.Mid(3, 1)); EXPECT_EQ("F", fred.Mid(0, 1)); EXPECT_EQ("R", fred.Mid(1, 1)); EXPECT_EQ("E", fred.Mid(2, 1)); @@ -458,9 +458,6 @@ TEST(fxcrt, ByteStringMid) { CFX_ByteString empty; EXPECT_EQ("", empty.Mid(0, 0)); - EXPECT_EQ("", empty.Mid(0)); - EXPECT_EQ("", empty.Mid(1)); - EXPECT_EQ("", empty.Mid(-1)); } TEST(fxcrt, ByteStringLeft) { diff --git a/core/fxcrt/cfx_string_c_template.h b/core/fxcrt/cfx_string_c_template.h index cd3cd27e75..77f34f638e 100644 --- a/core/fxcrt/cfx_string_c_template.h +++ b/core/fxcrt/cfx_string_c_template.h @@ -130,29 +130,32 @@ class CFX_StringCTemplate { return found ? found - m_Ptr.Get() : -1; } - CFX_StringCTemplate Mid(FX_STRSIZE index, FX_STRSIZE count = -1) const { - index = std::max(0, index); + CFX_StringCTemplate Mid(FX_STRSIZE index, FX_STRSIZE count) const { + ASSERT(count >= 0); if (index > m_Length) return CFX_StringCTemplate(); - if (count < 0 || count > m_Length - index) - count = m_Length - index; + index = pdfium::clamp(index, 0, m_Length); + count = pdfium::clamp(count, 0, m_Length - index); + if (count == 0) + return CFX_StringCTemplate(); return CFX_StringCTemplate(m_Ptr.Get() + index, count); } CFX_StringCTemplate Left(FX_STRSIZE count) const { - if (count <= 0) + count = pdfium::clamp(count, 0, m_Length); + if (count == 0) return CFX_StringCTemplate(); - return CFX_StringCTemplate(m_Ptr.Get(), std::min(count, m_Length)); + return CFX_StringCTemplate(m_Ptr.Get(), count); } CFX_StringCTemplate Right(FX_STRSIZE count) const { - if (count <= 0) + count = pdfium::clamp(count, 0, m_Length); + if (count == 0) return CFX_StringCTemplate(); - count = std::min(count, m_Length); return CFX_StringCTemplate(m_Ptr.Get() + m_Length - count, count); } diff --git a/core/fxcrt/cfx_widestring.cpp b/core/fxcrt/cfx_widestring.cpp index 6a4fbb9846..d7c1ef23ea 100644 --- a/core/fxcrt/cfx_widestring.cpp +++ b/core/fxcrt/cfx_widestring.cpp @@ -291,6 +291,7 @@ CFX_WideString::CFX_WideString(CFX_WideString&& other) noexcept { } CFX_WideString::CFX_WideString(const wchar_t* pStr, FX_STRSIZE nLen) { + ASSERT(nLen >= 0); if (nLen < 0) nLen = pStr ? FXSYS_wcslen(pStr) : 0; @@ -605,14 +606,8 @@ CFX_ByteString CFX_WideString::UTF16LE_Encode() const { return result; } -CFX_WideString CFX_WideString::Mid(FX_STRSIZE nFirst) const { - if (!m_pData) - return CFX_WideString(); - - return Mid(nFirst, m_pData->m_nDataLength - nFirst); -} - CFX_WideString CFX_WideString::Mid(FX_STRSIZE nFirst, FX_STRSIZE nCount) const { + ASSERT(nCount >= 0); if (!m_pData) return CFX_WideString(); diff --git a/core/fxcrt/cfx_widestring.h b/core/fxcrt/cfx_widestring.h index 0e477bfc8b..938b1e7958 100644 --- a/core/fxcrt/cfx_widestring.h +++ b/core/fxcrt/cfx_widestring.h @@ -112,7 +112,6 @@ class CFX_WideString { int Compare(const CFX_WideString& str) const; int CompareNoCase(const wchar_t* str) const; - CFX_WideString Mid(FX_STRSIZE first) const; CFX_WideString Mid(FX_STRSIZE first, FX_STRSIZE count) const; CFX_WideString Left(FX_STRSIZE count) const; CFX_WideString Right(FX_STRSIZE count) const; diff --git a/core/fxcrt/cfx_widestring_unittest.cpp b/core/fxcrt/cfx_widestring_unittest.cpp index 4cd71d9997..1d6d110497 100644 --- a/core/fxcrt/cfx_widestring_unittest.cpp +++ b/core/fxcrt/cfx_widestring_unittest.cpp @@ -406,10 +406,10 @@ TEST(fxcrt, WideStringMid) { CFX_WideString fred(L"FRED"); EXPECT_EQ(L"", fred.Mid(0, 0)); EXPECT_EQ(L"", fred.Mid(3, 0)); - EXPECT_EQ(L"FRED", fred.Mid(0)); - EXPECT_EQ(L"RED", fred.Mid(1)); - EXPECT_EQ(L"ED", fred.Mid(2)); - EXPECT_EQ(L"D", fred.Mid(3)); + EXPECT_EQ(L"FRED", fred.Mid(0, 4)); + EXPECT_EQ(L"RED", fred.Mid(1, 3)); + EXPECT_EQ(L"ED", fred.Mid(2, 2)); + EXPECT_EQ(L"D", fred.Mid(3, 1)); EXPECT_EQ(L"F", fred.Mid(0, 1)); EXPECT_EQ(L"R", fred.Mid(1, 1)); EXPECT_EQ(L"E", fred.Mid(2, 1)); @@ -424,9 +424,6 @@ TEST(fxcrt, WideStringMid) { CFX_WideString empty; EXPECT_EQ(L"", empty.Mid(0, 0)); - EXPECT_EQ(L"", empty.Mid(0)); - EXPECT_EQ(L"", empty.Mid(1)); - EXPECT_EQ(L"", empty.Mid(-1)); } TEST(fxcrt, WideStringLeft) { diff --git a/core/fxcrt/xml/cxml_parser.cpp b/core/fxcrt/xml/cxml_parser.cpp index a741d8409e..0166ea8490 100644 --- a/core/fxcrt/xml/cxml_parser.cpp +++ b/core/fxcrt/xml/cxml_parser.cpp @@ -87,7 +87,7 @@ void FX_XML_SplitQualifiedName(const CFX_ByteStringC& bsFullName, bsName = bsFullName; } else { bsSpace = bsFullName.Mid(0, iStart); - bsName = bsFullName.Mid(iStart + 1); + bsName = bsFullName.Mid(iStart + 1, bsFullName.GetLength() - (iStart + 1)); } } diff --git a/core/fxge/cfx_fontmapper.cpp b/core/fxge/cfx_fontmapper.cpp index 5b4ceaac59..5c7b602fed 100644 --- a/core/fxge/cfx_fontmapper.cpp +++ b/core/fxge/cfx_fontmapper.cpp @@ -432,7 +432,7 @@ FXFT_Face CFX_FontMapper::FindSubstFont(const CFX_ByteString& name, CFX_ByteString SubstName = name; SubstName.Remove(' '); if (bTrueType && name[0] == '@') - SubstName = name.Mid(1); + SubstName = name.Mid(1, name.GetLength() - 1); PDF_GetStandardFontName(&SubstName); if (SubstName == "Symbol" && !bTrueType) { pSubstFont->m_Family = "Chrome Symbol"; @@ -453,7 +453,7 @@ FXFT_Face CFX_FontMapper::FindSubstFont(const CFX_ByteString& name, if (find >= 0) { family = SubstName.Left(find); PDF_GetStandardFontName(&family); - style = SubstName.Mid(find + 1); + style = SubstName.Mid(find + 1, SubstName.GetLength() - (find + 1)); bHasComma = true; } else { family = SubstName; @@ -480,7 +480,7 @@ FXFT_Face CFX_FontMapper::FindSubstFont(const CFX_ByteString& name, if (!bHasComma) { find = family.ReverseFind('-'); if (find >= 0) { - style = family.Mid(find + 1); + style = family.Mid(find + 1, family.GetLength() - (find + 1)); family = family.Left(find); bHasHyphen = true; } -- cgit v1.2.3