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/include/fx_string.h | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) (limited to 'core/fxcrt/include/fx_string.h') 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