From 28f97ff783c16f3391384ce97b765ce4eb310ac7 Mon Sep 17 00:00:00 2001 From: tsepez Date: Mon, 4 Apr 2016 16:41:35 -0700 Subject: Make down-conversion explicit from CFX_ByteString to CFX_ByteStringC. Having this happen implicitly can be dangerous because the lifetime has to be considered; we should have caught the "red bots" in https://codereview.chromium.org/1847333004/#ps60001 at compile time. Review URL: https://codereview.chromium.org/1853233002 --- core/fxcrt/fx_basic_buffer.cpp | 2 +- core/fxcrt/fx_xml_parser.cpp | 7 ++++--- core/fxcrt/fxcrt_posix.cpp | 2 +- core/fxcrt/include/fx_basic.h | 10 ++++++++-- core/fxcrt/include/fx_string.h | 30 ++++++++++++++++-------------- 5 files changed, 30 insertions(+), 21 deletions(-) (limited to 'core/fxcrt') diff --git a/core/fxcrt/fx_basic_buffer.cpp b/core/fxcrt/fx_basic_buffer.cpp index 28c8ac0f32..de7bff2401 100644 --- a/core/fxcrt/fx_basic_buffer.cpp +++ b/core/fxcrt/fx_basic_buffer.cpp @@ -248,7 +248,7 @@ CFX_ArchiveSaver& CFX_ArchiveSaver::operator<<(const FX_WCHAR* wstr) { } CFX_ArchiveSaver& CFX_ArchiveSaver::operator<<(const CFX_WideString& wstr) { CFX_ByteString encoded = wstr.UTF16LE_Encode(); - return operator<<(encoded); + return operator<<(encoded.AsByteStringC()); } void CFX_ArchiveSaver::Write(const void* pData, FX_STRSIZE dwSize) { if (m_pStream) { diff --git a/core/fxcrt/fx_xml_parser.cpp b/core/fxcrt/fx_xml_parser.cpp index 9cdf73ca90..b39c32bfbd 100644 --- a/core/fxcrt/fx_xml_parser.cpp +++ b/core/fxcrt/fx_xml_parser.cpp @@ -371,7 +371,7 @@ CXML_Element* CXML_Parser::ParseElement(CXML_Element* pParent, } CXML_Element* pElement = new CXML_Element; pElement->m_pParent = pParent; - pElement->SetTag(tag_space, tag_name); + pElement->SetTag(tag_space.AsByteStringC(), tag_name.AsByteStringC()); do { CFX_ByteString attr_space, attr_name; while (m_dwIndex < m_dwBufferSize) { @@ -397,7 +397,8 @@ CXML_Element* CXML_Parser::ParseElement(CXML_Element* pParent, } CFX_WideString attr_value; GetAttrValue(attr_value); - pElement->m_AttrMap.SetAt(attr_space, attr_name, attr_value); + pElement->m_AttrMap.SetAt(attr_space.AsByteStringC(), + attr_name.AsByteStringC(), attr_value); } m_nOffset = m_nBufferOffset + (FX_FILESIZE)m_dwIndex; if (m_dwIndex < m_dwBufferSize || IsEOF()) { @@ -600,7 +601,7 @@ CFX_ByteString CXML_Element::GetNamespace(FX_BOOL bQualified) const { if (bQualified) { return m_QSpaceName; } - return GetNamespaceURI(m_QSpaceName); + return GetNamespaceURI(m_QSpaceName.AsByteStringC()); } CFX_ByteString CXML_Element::GetNamespaceURI( const CFX_ByteStringC& qName) const { diff --git a/core/fxcrt/fxcrt_posix.cpp b/core/fxcrt/fxcrt_posix.cpp index 2f17e71567..0226313934 100644 --- a/core/fxcrt/fxcrt_posix.cpp +++ b/core/fxcrt/fxcrt_posix.cpp @@ -45,7 +45,7 @@ FX_BOOL CFXCRT_FileAccess_Posix::Open(const CFX_ByteStringC& fileName, } FX_BOOL CFXCRT_FileAccess_Posix::Open(const CFX_WideStringC& fileName, uint32_t dwMode) { - return Open(FX_UTF8Encode(fileName), dwMode); + return Open(FX_UTF8Encode(fileName).AsByteStringC(), dwMode); } void CFXCRT_FileAccess_Posix::Close() { if (m_nFD < 0) { diff --git a/core/fxcrt/include/fx_basic.h b/core/fxcrt/include/fx_basic.h index feeb6e756d..2ef11beb27 100644 --- a/core/fxcrt/include/fx_basic.h +++ b/core/fxcrt/include/fx_basic.h @@ -27,8 +27,8 @@ class CFX_BinaryBuf { void Clear(); void EstimateSize(FX_STRSIZE size, FX_STRSIZE alloc_step = 0); void AppendBlock(const void* pBuf, FX_STRSIZE size); - void AppendString(const CFX_ByteStringC& str) { - AppendBlock(str.GetPtr(), str.GetLength()); + void AppendString(const CFX_ByteString& str) { + AppendBlock(str.c_str(), str.GetLength()); } void AppendByte(uint8_t byte) { @@ -63,6 +63,12 @@ class CFX_ByteTextBuf : public CFX_BinaryBuf { CFX_ByteTextBuf& operator<<(int i); CFX_ByteTextBuf& operator<<(uint32_t i); CFX_ByteTextBuf& operator<<(double f); + CFX_ByteTextBuf& operator<<(const FX_CHAR* pStr) { + return *this << CFX_ByteStringC(pStr); + } + CFX_ByteTextBuf& operator<<(const CFX_ByteString& str) { + return *this << str.AsByteStringC(); + } CFX_ByteTextBuf& operator<<(const CFX_ByteStringC& lpsz); CFX_ByteTextBuf& operator<<(const CFX_ByteTextBuf& buf); }; diff --git a/core/fxcrt/include/fx_string.h b/core/fxcrt/include/fx_string.h index 9aa67845f8..cbf8c4de2f 100644 --- a/core/fxcrt/include/fx_string.h +++ b/core/fxcrt/include/fx_string.h @@ -61,8 +61,6 @@ class CFX_ByteStringC { m_Length = src.m_Length; } - CFX_ByteStringC(const CFX_ByteString& src); - CFX_ByteStringC& operator=(const FX_CHAR* src) { m_Ptr = (const uint8_t*)src; m_Length = m_Ptr ? FXSYS_strlen(src) : 0; @@ -167,12 +165,14 @@ class CFX_ByteString { static CFX_ByteString FromUnicode(const CFX_WideString& str); // Explicit conversion to C-style string. + // Note: |this| must outlive the use of the result. const FX_CHAR* c_str() const { return m_pData ? m_pData->m_String : ""; } // Implicit conversion to C-style string -- deprecated. operator const FX_CHAR*() const { return m_pData ? m_pData->m_String : ""; } // Explicit conversion to uint8_t*. + // Note: |this| must outlive the use of the result. const uint8_t* raw_str() const { return m_pData ? reinterpret_cast(m_pData->m_String) : nullptr; @@ -184,6 +184,12 @@ class CFX_ByteString { : nullptr; } + // Explicit conversion to CFX_ByteStringC. + // Note: |this| must outlive the use of the result. + CFX_ByteStringC AsByteStringC() const { + return CFX_ByteStringC(raw_str(), GetLength()); + } + FX_STRSIZE GetLength() const { return m_pData ? m_pData->m_nDataLength : 0; } bool IsEmpty() const { return !GetLength(); } @@ -330,10 +336,6 @@ class CFX_ByteString { friend class fxcrt_ByteStringConcat_Test; }; -inline CFX_ByteStringC::CFX_ByteStringC(const CFX_ByteString& src) { - m_Ptr = (const uint8_t*)src; - m_Length = src.GetLength(); -} inline CFX_ByteStringC& CFX_ByteStringC::operator=(const CFX_ByteString& src) { m_Ptr = (const uint8_t*)src; m_Length = src.GetLength(); @@ -373,29 +375,29 @@ inline CFX_ByteString operator+(FX_CHAR ch, const CFX_ByteStringC& str2) { } inline CFX_ByteString operator+(const CFX_ByteString& str1, const CFX_ByteString& str2) { - return CFX_ByteString(str1, str2); + return CFX_ByteString(str1.AsByteStringC(), str2.AsByteStringC()); } inline CFX_ByteString operator+(const CFX_ByteString& str1, FX_CHAR ch) { - return CFX_ByteString(str1, CFX_ByteStringC(ch)); + return CFX_ByteString(str1.AsByteStringC(), CFX_ByteStringC(ch)); } inline CFX_ByteString operator+(FX_CHAR ch, const CFX_ByteString& str2) { - return CFX_ByteString(ch, str2); + return CFX_ByteString(ch, str2.AsByteStringC()); } inline CFX_ByteString operator+(const CFX_ByteString& str1, const FX_CHAR* str2) { - return CFX_ByteString(str1, str2); + return CFX_ByteString(str1.AsByteStringC(), str2); } inline CFX_ByteString operator+(const FX_CHAR* str1, const CFX_ByteString& str2) { - return CFX_ByteString(str1, str2); + return CFX_ByteString(str1, str2.AsByteStringC()); } inline CFX_ByteString operator+(const CFX_ByteString& str1, const CFX_ByteStringC& str2) { - return CFX_ByteString(str1, str2); + return CFX_ByteString(str1.AsByteStringC(), str2); } inline CFX_ByteString operator+(const CFX_ByteStringC& str1, const CFX_ByteString& str2) { - return CFX_ByteString(str1, str2); + return CFX_ByteString(str1, str2.AsByteStringC()); } class CFX_WideStringC { @@ -758,7 +760,7 @@ inline CFX_ByteString FX_UTF8Encode(const CFX_WideString& wsStr) { FX_FLOAT FX_atof(const CFX_ByteStringC& str); inline FX_FLOAT FX_atof(const CFX_WideStringC& wsStr) { - return FX_atof(FX_UTF8Encode(wsStr.GetPtr(), wsStr.GetLength())); + return FX_atof(FX_UTF8Encode(wsStr.GetPtr(), wsStr.GetLength()).c_str()); } void FX_atonum(const CFX_ByteStringC& str, FX_BOOL& bInteger, void* pData); FX_STRSIZE FX_ftoa(FX_FLOAT f, FX_CHAR* buf); -- cgit v1.2.3