diff options
author | Tom Sepez <tsepez@chromium.org> | 2017-07-19 13:19:12 -0700 |
---|---|---|
committer | Chromium commit bot <commit-bot@chromium.org> | 2017-07-19 20:59:29 +0000 |
commit | 33b42e4ab56d56ff02cd08a47c5f590875d886bf (patch) | |
tree | d168b57aa48373a3213f918471fcd1c8224d2d5d /core/fxcrt | |
parent | d4c401194137f3f7f466f6daaa7fe3ffb4b6cd53 (diff) | |
download | pdfium-33b42e4ab56d56ff02cd08a47c5f590875d886bf.tar.xz |
Rename StringCs c_str() to unterminated_c_str().
Since there is no guarantee of termination if the StringC was
extracted from a snippet of another string. Make it more obvious
that things like
strlen(str.unterminated_c_str())
might be a bad idea.
Change-Id: I7832248ed89ebbddf5c0bcd402aac7d40ec2adc2
Reviewed-on: https://pdfium-review.googlesource.com/8170
Commit-Queue: Tom Sepez <tsepez@chromium.org>
Reviewed-by: dsinclair <dsinclair@chromium.org>
Reviewed-by: Henrique Nakashima <hnakashima@chromium.org>
Diffstat (limited to 'core/fxcrt')
-rw-r--r-- | core/fxcrt/cfx_bytestring.cpp | 37 | ||||
-rw-r--r-- | core/fxcrt/cfx_decimal.cpp | 2 | ||||
-rw-r--r-- | core/fxcrt/cfx_seekablestreamproxy.cpp | 2 | ||||
-rw-r--r-- | core/fxcrt/cfx_string_c_template.h | 2 | ||||
-rw-r--r-- | core/fxcrt/cfx_widestring.cpp | 36 | ||||
-rw-r--r-- | core/fxcrt/fx_basic_buffer.cpp | 2 | ||||
-rw-r--r-- | core/fxcrt/fx_basic_utf.cpp | 2 | ||||
-rw-r--r-- | core/fxcrt/fx_stream.cpp | 2 | ||||
-rw-r--r-- | core/fxcrt/fxcrt_posix.cpp | 4 | ||||
-rw-r--r-- | core/fxcrt/fxcrt_windows.cpp | 9 |
10 files changed, 55 insertions, 43 deletions
diff --git a/core/fxcrt/cfx_bytestring.cpp b/core/fxcrt/cfx_bytestring.cpp index 9d67ed30a1..c88ad51ab3 100644 --- a/core/fxcrt/cfx_bytestring.cpp +++ b/core/fxcrt/cfx_bytestring.cpp @@ -101,15 +101,16 @@ CFX_ByteString GetByteString(uint16_t codepage, const CFX_WideStringC& wstr) { ASSERT(IsValidCodePage(codepage)); int src_len = wstr.GetLength(); - int dest_len = FXSYS_WideCharToMultiByte(codepage, 0, wstr.c_str(), src_len, - nullptr, 0, nullptr, nullptr); + int dest_len = + FXSYS_WideCharToMultiByte(codepage, 0, wstr.unterminated_c_str(), src_len, + nullptr, 0, nullptr, nullptr); if (!dest_len) return CFX_ByteString(); CFX_ByteString bstr; char* dest_buf = bstr.GetBuffer(dest_len); - FXSYS_WideCharToMultiByte(codepage, 0, wstr.c_str(), src_len, dest_buf, - dest_len, nullptr, nullptr); + FXSYS_WideCharToMultiByte(codepage, 0, wstr.unterminated_c_str(), src_len, + dest_buf, dest_len, nullptr, nullptr); bstr.ReleaseBuffer(dest_len); return bstr; } @@ -153,7 +154,8 @@ CFX_ByteString::CFX_ByteString(const char* ptr) CFX_ByteString::CFX_ByteString(const CFX_ByteStringC& stringSrc) { if (!stringSrc.IsEmpty()) - m_pData.Reset(StringData::Create(stringSrc.c_str(), stringSrc.GetLength())); + m_pData.Reset(StringData::Create(stringSrc.unterminated_c_str(), + stringSrc.GetLength())); } CFX_ByteString::CFX_ByteString(const CFX_ByteStringC& str1, @@ -166,8 +168,9 @@ CFX_ByteString::CFX_ByteString(const CFX_ByteStringC& str1, return; m_pData.Reset(StringData::Create(nNewLen)); - m_pData->CopyContents(str1.c_str(), str1.GetLength()); - m_pData->CopyContentsAt(str1.GetLength(), str2.c_str(), str2.GetLength()); + m_pData->CopyContents(str1.unterminated_c_str(), str1.GetLength()); + m_pData->CopyContentsAt(str1.GetLength(), str2.unterminated_c_str(), + str2.GetLength()); } CFX_ByteString::CFX_ByteString( @@ -184,7 +187,8 @@ CFX_ByteString::CFX_ByteString( FX_STRSIZE nOffset = 0; for (const auto& item : list) { - m_pData->CopyContentsAt(nOffset, item.c_str(), item.GetLength()); + m_pData->CopyContentsAt(nOffset, item.unterminated_c_str(), + item.GetLength()); nOffset += item.GetLength(); } } @@ -209,7 +213,7 @@ const CFX_ByteString& CFX_ByteString::operator=( if (stringSrc.IsEmpty()) clear(); else - AssignCopy(stringSrc.c_str(), stringSrc.GetLength()); + AssignCopy(stringSrc.unterminated_c_str(), stringSrc.GetLength()); return *this; } @@ -243,7 +247,7 @@ const CFX_ByteString& CFX_ByteString::operator+=(const CFX_ByteString& str) { const CFX_ByteString& CFX_ByteString::operator+=(const CFX_ByteStringC& str) { if (!str.IsEmpty()) - Concat(str.c_str(), str.GetLength()); + Concat(str.unterminated_c_str(), str.GetLength()); return *this; } @@ -264,7 +268,8 @@ bool CFX_ByteString::operator==(const CFX_ByteStringC& str) const { return str.IsEmpty(); return m_pData->m_nDataLength == str.GetLength() && - memcmp(m_pData->m_String, str.c_str(), str.GetLength()) == 0; + memcmp(m_pData->m_String, str.unterminated_c_str(), str.GetLength()) == + 0; } bool CFX_ByteString::operator==(const CFX_ByteString& other) const { @@ -594,7 +599,7 @@ FX_STRSIZE CFX_ByteString::Find(const CFX_ByteStringC& pSub, const char* pStr = FX_strstr(m_pData->m_String + nStart, m_pData->m_nDataLength - nStart, - pSub.c_str(), pSub.GetLength()); + pSub.unterminated_c_str(), pSub.GetLength()); return pStr ? (int)(pStr - m_pData->m_String) : -1; } @@ -660,7 +665,7 @@ FX_STRSIZE CFX_ByteString::Replace(const CFX_ByteStringC& pOld, char* pEnd = m_pData->m_String + m_pData->m_nDataLength; while (1) { const char* pTarget = FX_strstr(pStart, (FX_STRSIZE)(pEnd - pStart), - pOld.c_str(), nSourceLen); + pOld.unterminated_c_str(), nSourceLen); if (!pTarget) break; @@ -683,10 +688,10 @@ FX_STRSIZE CFX_ByteString::Replace(const CFX_ByteStringC& pOld, char* pDest = pNewData->m_String; for (FX_STRSIZE i = 0; i < nCount; i++) { const char* pTarget = FX_strstr(pStart, (FX_STRSIZE)(pEnd - pStart), - pOld.c_str(), nSourceLen); + pOld.unterminated_c_str(), nSourceLen); memcpy(pDest, pStart, pTarget - pStart); pDest += pTarget - pStart; - memcpy(pDest, pNew.c_str(), pNew.GetLength()); + memcpy(pDest, pNew.unterminated_c_str(), pNew.GetLength()); pDest += pNew.GetLength(); pStart = pTarget + nSourceLen; } @@ -879,5 +884,5 @@ std::ostream& operator<<(std::ostream& os, const CFX_ByteString& str) { } std::ostream& operator<<(std::ostream& os, const CFX_ByteStringC& str) { - return os.write(str.c_str(), str.GetLength()); + return os.write(str.unterminated_c_str(), str.GetLength()); } diff --git a/core/fxcrt/cfx_decimal.cpp b/core/fxcrt/cfx_decimal.cpp index a463305f6a..b900e069a7 100644 --- a/core/fxcrt/cfx_decimal.cpp +++ b/core/fxcrt/cfx_decimal.cpp @@ -295,7 +295,7 @@ CFX_Decimal::CFX_Decimal(float val, uint8_t scale) { } CFX_Decimal::CFX_Decimal(const CFX_WideStringC& strObj) { - const wchar_t* str = strObj.c_str(); + const wchar_t* str = strObj.unterminated_c_str(); const wchar_t* strBound = str + strObj.GetLength(); bool pointmet = false; bool negmet = false; diff --git a/core/fxcrt/cfx_seekablestreamproxy.cpp b/core/fxcrt/cfx_seekablestreamproxy.cpp index 37dbed1fca..8cf855217a 100644 --- a/core/fxcrt/cfx_seekablestreamproxy.cpp +++ b/core/fxcrt/cfx_seekablestreamproxy.cpp @@ -294,7 +294,7 @@ void CFX_SeekableStreamProxy::WriteString(const CFX_WideStringC& str) { m_wCodePage != FX_CODEPAGE_UTF8) { return; } - if (!m_pStream->WriteBlock(str.c_str(), m_iPosition, + if (!m_pStream->WriteBlock(str.unterminated_c_str(), m_iPosition, str.GetLength() * sizeof(wchar_t))) { return; } diff --git a/core/fxcrt/cfx_string_c_template.h b/core/fxcrt/cfx_string_c_template.h index 5dfff69a95..1473160334 100644 --- a/core/fxcrt/cfx_string_c_template.h +++ b/core/fxcrt/cfx_string_c_template.h @@ -112,7 +112,7 @@ class CFX_StringCTemplate { } const UnsignedType* raw_str() const { return m_Ptr.Get(); } - const CharType* c_str() const { + const CharType* unterminated_c_str() const { return reinterpret_cast<const CharType*>(m_Ptr.Get()); } diff --git a/core/fxcrt/cfx_widestring.cpp b/core/fxcrt/cfx_widestring.cpp index 70a0cb63be..6a4fbb9846 100644 --- a/core/fxcrt/cfx_widestring.cpp +++ b/core/fxcrt/cfx_widestring.cpp @@ -263,15 +263,15 @@ CFX_WideString GetWideString(uint16_t codepage, const CFX_ByteStringC& bstr) { ASSERT(IsValidCodePage(codepage)); int src_len = bstr.GetLength(); - int dest_len = - FXSYS_MultiByteToWideChar(codepage, 0, bstr.c_str(), src_len, nullptr, 0); + int dest_len = FXSYS_MultiByteToWideChar( + codepage, 0, bstr.unterminated_c_str(), src_len, nullptr, 0); if (!dest_len) return CFX_WideString(); CFX_WideString wstr; wchar_t* dest_buf = wstr.GetBuffer(dest_len); - FXSYS_MultiByteToWideChar(codepage, 0, bstr.c_str(), src_len, dest_buf, - dest_len); + FXSYS_MultiByteToWideChar(codepage, 0, bstr.unterminated_c_str(), src_len, + dest_buf, dest_len); wstr.ReleaseBuffer(dest_len); return wstr; } @@ -308,7 +308,8 @@ CFX_WideString::CFX_WideString(const wchar_t* ptr) CFX_WideString::CFX_WideString(const CFX_WideStringC& stringSrc) { if (!stringSrc.IsEmpty()) { - m_pData.Reset(StringData::Create(stringSrc.c_str(), stringSrc.GetLength())); + m_pData.Reset(StringData::Create(stringSrc.unterminated_c_str(), + stringSrc.GetLength())); } } @@ -322,8 +323,9 @@ CFX_WideString::CFX_WideString(const CFX_WideStringC& str1, return; m_pData.Reset(StringData::Create(nNewLen)); - m_pData->CopyContents(str1.c_str(), str1.GetLength()); - m_pData->CopyContentsAt(str1.GetLength(), str2.c_str(), str2.GetLength()); + m_pData->CopyContents(str1.unterminated_c_str(), str1.GetLength()); + m_pData->CopyContentsAt(str1.GetLength(), str2.unterminated_c_str(), + str2.GetLength()); } CFX_WideString::CFX_WideString( @@ -340,7 +342,8 @@ CFX_WideString::CFX_WideString( FX_STRSIZE nOffset = 0; for (const auto& item : list) { - m_pData->CopyContentsAt(nOffset, item.c_str(), item.GetLength()); + m_pData->CopyContentsAt(nOffset, item.unterminated_c_str(), + item.GetLength()); nOffset += item.GetLength(); } } @@ -361,7 +364,7 @@ const CFX_WideString& CFX_WideString::operator=( if (stringSrc.IsEmpty()) clear(); else - AssignCopy(stringSrc.c_str(), stringSrc.GetLength()); + AssignCopy(stringSrc.unterminated_c_str(), stringSrc.GetLength()); return *this; } @@ -395,7 +398,7 @@ const CFX_WideString& CFX_WideString::operator+=(const CFX_WideString& str) { const CFX_WideString& CFX_WideString::operator+=(const CFX_WideStringC& str) { if (!str.IsEmpty()) - Concat(str.c_str(), str.GetLength()); + Concat(str.unterminated_c_str(), str.GetLength()); return *this; } @@ -416,7 +419,8 @@ bool CFX_WideString::operator==(const CFX_WideStringC& str) const { return str.IsEmpty(); return m_pData->m_nDataLength == str.GetLength() && - wmemcmp(m_pData->m_String, str.c_str(), str.GetLength()) == 0; + wmemcmp(m_pData->m_String, str.unterminated_c_str(), + str.GetLength()) == 0; } bool CFX_WideString::operator==(const CFX_WideString& other) const { @@ -746,7 +750,7 @@ FX_STRSIZE CFX_WideString::Find(const CFX_WideStringC& pSub, const wchar_t* pStr = FX_wcsstr(m_pData->m_String + nStart, m_pData->m_nDataLength - nStart, - pSub.c_str(), pSub.GetLength()); + pSub.unterminated_c_str(), pSub.GetLength()); return pStr ? (int)(pStr - m_pData->m_String) : -1; } @@ -812,7 +816,7 @@ FX_STRSIZE CFX_WideString::Replace(const CFX_WideStringC& pOld, wchar_t* pEnd = m_pData->m_String + m_pData->m_nDataLength; while (1) { const wchar_t* pTarget = FX_wcsstr(pStart, (FX_STRSIZE)(pEnd - pStart), - pOld.c_str(), nSourceLen); + pOld.unterminated_c_str(), nSourceLen); if (!pTarget) break; @@ -835,10 +839,10 @@ FX_STRSIZE CFX_WideString::Replace(const CFX_WideStringC& pOld, wchar_t* pDest = pNewData->m_String; for (FX_STRSIZE i = 0; i < nCount; i++) { const wchar_t* pTarget = FX_wcsstr(pStart, (FX_STRSIZE)(pEnd - pStart), - pOld.c_str(), nSourceLen); + pOld.unterminated_c_str(), nSourceLen); wmemcpy(pDest, pStart, pTarget - pStart); pDest += pTarget - pStart; - wmemcpy(pDest, pNew.c_str(), pNew.GetLength()); + wmemcpy(pDest, pNew.unterminated_c_str(), pNew.GetLength()); pDest += pNew.GetLength(); pStart = pTarget + nSourceLen; } @@ -1060,7 +1064,7 @@ std::ostream& operator<<(std::ostream& os, const CFX_WideString& str) { } std::wostream& operator<<(std::wostream& os, const CFX_WideStringC& str) { - return os.write(str.c_str(), str.GetLength()); + return os.write(str.unterminated_c_str(), str.GetLength()); } std::ostream& operator<<(std::ostream& os, const CFX_WideStringC& str) { diff --git a/core/fxcrt/fx_basic_buffer.cpp b/core/fxcrt/fx_basic_buffer.cpp index 36da3f4053..bcf0570d32 100644 --- a/core/fxcrt/fx_basic_buffer.cpp +++ b/core/fxcrt/fx_basic_buffer.cpp @@ -133,7 +133,7 @@ void CFX_WideTextBuf::AppendChar(wchar_t ch) { } CFX_WideTextBuf& CFX_WideTextBuf::operator<<(const CFX_WideStringC& str) { - AppendBlock(str.c_str(), str.GetLength() * sizeof(wchar_t)); + AppendBlock(str.unterminated_c_str(), str.GetLength() * sizeof(wchar_t)); return *this; } diff --git a/core/fxcrt/fx_basic_utf.cpp b/core/fxcrt/fx_basic_utf.cpp index 10aa918962..4dbfa37284 100644 --- a/core/fxcrt/fx_basic_utf.cpp +++ b/core/fxcrt/fx_basic_utf.cpp @@ -76,7 +76,7 @@ void CFX_UTF8Encoder::Input(wchar_t unicode) { CFX_ByteString FX_UTF8Encode(const CFX_WideStringC& wsStr) { FX_STRSIZE len = wsStr.GetLength(); - const wchar_t* pStr = wsStr.c_str(); + const wchar_t* pStr = wsStr.unterminated_c_str(); CFX_UTF8Encoder encoder; while (len-- > 0) encoder.Input(*pStr++); diff --git a/core/fxcrt/fx_stream.cpp b/core/fxcrt/fx_stream.cpp index ba21461098..a64b239546 100644 --- a/core/fxcrt/fx_stream.cpp +++ b/core/fxcrt/fx_stream.cpp @@ -95,5 +95,5 @@ bool IFX_SeekableStream::WriteBlock(const void* buffer, size_t size) { } bool IFX_SeekableStream::WriteString(const CFX_ByteStringC& str) { - return WriteBlock(str.c_str(), str.GetLength()); + return WriteBlock(str.unterminated_c_str(), str.GetLength()); } diff --git a/core/fxcrt/fxcrt_posix.cpp b/core/fxcrt/fxcrt_posix.cpp index ca4ac161a4..4d867dd688 100644 --- a/core/fxcrt/fxcrt_posix.cpp +++ b/core/fxcrt/fxcrt_posix.cpp @@ -56,7 +56,9 @@ bool CFXCRT_FileAccess_Posix::Open(const CFX_ByteStringC& fileName, int32_t nFlags; int32_t nMasks; FXCRT_Posix_GetFileMode(dwMode, nFlags, nMasks); - m_nFD = open(fileName.c_str(), nFlags, nMasks); + + // TODO(tsepez): check usage of c_str() below. + m_nFD = open(fileName.unterminated_c_str(), nFlags, nMasks); return m_nFD > -1; } diff --git a/core/fxcrt/fxcrt_windows.cpp b/core/fxcrt/fxcrt_windows.cpp index 6230c74eb7..1a2a36735e 100644 --- a/core/fxcrt/fxcrt_windows.cpp +++ b/core/fxcrt/fxcrt_windows.cpp @@ -57,8 +57,8 @@ bool CFXCRT_FileAccess_Win64::Open(const CFX_ByteStringC& fileName, uint32_t dwAccess, dwShare, dwCreation; FXCRT_Windows_GetFileMode(dwMode, dwAccess, dwShare, dwCreation); - m_hFile = ::CreateFileA(fileName.c_str(), dwAccess, dwShare, nullptr, - dwCreation, FILE_ATTRIBUTE_NORMAL, nullptr); + m_hFile = ::CreateFileA(fileName.unterminated_c_str(), dwAccess, dwShare, + nullptr, dwCreation, FILE_ATTRIBUTE_NORMAL, nullptr); if (m_hFile == INVALID_HANDLE_VALUE) m_hFile = nullptr; @@ -72,8 +72,9 @@ bool CFXCRT_FileAccess_Win64::Open(const CFX_WideStringC& fileName, uint32_t dwAccess, dwShare, dwCreation; FXCRT_Windows_GetFileMode(dwMode, dwAccess, dwShare, dwCreation); - m_hFile = ::CreateFileW((LPCWSTR)fileName.c_str(), dwAccess, dwShare, nullptr, - dwCreation, FILE_ATTRIBUTE_NORMAL, nullptr); + m_hFile = + ::CreateFileW((LPCWSTR)fileName.unterminated_c_str(), dwAccess, dwShare, + nullptr, dwCreation, FILE_ATTRIBUTE_NORMAL, nullptr); if (m_hFile == INVALID_HANDLE_VALUE) m_hFile = nullptr; |