From 690d456ad54f021063dcc17fde27c7ba4d910717 Mon Sep 17 00:00:00 2001 From: Tom Sepez Date: Thu, 18 May 2017 11:42:46 -0700 Subject: Use UnownedPtr to check CFX_*StringC lifetimes Change interform to avoid temp StringC with dangling ptr. Change-Id: I8d8659973bcdf2cdbcaa6efa6012e4acce5f1604 Reviewed-on: https://pdfium-review.googlesource.com/5571 Commit-Queue: Tom Sepez Reviewed-by: Lei Zhang --- core/fxcrt/cfx_string_c_template.h | 46 +++++++++++++++++++++----------------- core/fxcrt/cfx_unowned_ptr.h | 2 +- 2 files changed, 26 insertions(+), 22 deletions(-) (limited to 'core/fxcrt') diff --git a/core/fxcrt/cfx_string_c_template.h b/core/fxcrt/cfx_string_c_template.h index 75c2be0efc..3a0ad74c08 100644 --- a/core/fxcrt/cfx_string_c_template.h +++ b/core/fxcrt/cfx_string_c_template.h @@ -11,6 +11,7 @@ #include #include +#include "core/fxcrt/cfx_unowned_ptr.h" #include "core/fxcrt/fx_system.h" #include "third_party/base/stl_util.h" @@ -75,22 +76,23 @@ class CFX_StringCTemplate { } const_iterator begin() const { - return reinterpret_cast(m_Ptr); + return reinterpret_cast(m_Ptr.Get()); } const_iterator end() const { - return m_Ptr ? reinterpret_cast(m_Ptr) + m_Length + return m_Ptr ? reinterpret_cast(m_Ptr.Get()) + m_Length : nullptr; } bool operator==(const CharType* ptr) const { return FXSYS_len(ptr) == m_Length && - FXSYS_cmp(ptr, reinterpret_cast(m_Ptr), m_Length) == - 0; + FXSYS_cmp(ptr, reinterpret_cast(m_Ptr.Get()), + m_Length) == 0; } bool operator==(const CFX_StringCTemplate& other) const { return other.m_Length == m_Length && - FXSYS_cmp(reinterpret_cast(other.m_Ptr), - reinterpret_cast(m_Ptr), m_Length) == 0; + FXSYS_cmp(reinterpret_cast(other.m_Ptr.Get()), + reinterpret_cast(m_Ptr.Get()), + m_Length) == 0; } bool operator!=(const CharType* ptr) const { return !(*this == ptr); } bool operator!=(const CFX_StringCTemplate& other) const { @@ -104,28 +106,28 @@ class CFX_StringCTemplate { uint32_t strid = 0; FX_STRSIZE size = std::min(4, m_Length - start_pos); for (FX_STRSIZE i = 0; i < size; i++) - strid = strid * 256 + m_Ptr[start_pos + i]; + strid = strid * 256 + m_Ptr.Get()[start_pos + i]; return strid << ((4 - size) * 8); } - const UnsignedType* raw_str() const { return m_Ptr; } + const UnsignedType* raw_str() const { return m_Ptr.Get(); } const CharType* c_str() const { - return reinterpret_cast(m_Ptr); + return reinterpret_cast(m_Ptr.Get()); } FX_STRSIZE GetLength() const { return m_Length; } bool IsEmpty() const { return m_Length == 0; } - UnsignedType GetAt(FX_STRSIZE index) const { return m_Ptr[index]; } + UnsignedType GetAt(FX_STRSIZE index) const { return m_Ptr.Get()[index]; } CharType CharAt(FX_STRSIZE index) const { - return static_cast(m_Ptr[index]); + return static_cast(m_Ptr.Get()[index]); } FX_STRSIZE Find(CharType ch) const { - const UnsignedType* found = reinterpret_cast( - FXSYS_chr(reinterpret_cast(m_Ptr), ch, m_Length)); - return found ? found - m_Ptr : -1; + const UnsignedType* found = reinterpret_cast(FXSYS_chr( + reinterpret_cast(m_Ptr.Get()), ch, m_Length)); + return found ? found - m_Ptr.Get() : -1; } CFX_StringCTemplate Mid(FX_STRSIZE index, FX_STRSIZE count = -1) const { @@ -136,14 +138,14 @@ class CFX_StringCTemplate { if (count < 0 || count > m_Length - index) count = m_Length - index; - return CFX_StringCTemplate(m_Ptr + index, count); + return CFX_StringCTemplate(m_Ptr.Get() + index, count); } CFX_StringCTemplate Left(FX_STRSIZE count) const { if (count <= 0) return CFX_StringCTemplate(); - return CFX_StringCTemplate(m_Ptr, std::min(count, m_Length)); + return CFX_StringCTemplate(m_Ptr.Get(), std::min(count, m_Length)); } CFX_StringCTemplate Right(FX_STRSIZE count) const { @@ -151,20 +153,22 @@ class CFX_StringCTemplate { return CFX_StringCTemplate(); count = std::min(count, m_Length); - return CFX_StringCTemplate(m_Ptr + m_Length - count, count); + return CFX_StringCTemplate(m_Ptr.Get() + m_Length - count, count); } - const UnsignedType& operator[](size_t index) const { return m_Ptr[index]; } + const UnsignedType& operator[](size_t index) const { + return m_Ptr.Get()[index]; + } bool operator<(const CFX_StringCTemplate& that) const { - int result = FXSYS_cmp(reinterpret_cast(m_Ptr), - reinterpret_cast(that.m_Ptr), + int result = FXSYS_cmp(reinterpret_cast(m_Ptr.Get()), + reinterpret_cast(that.m_Ptr.Get()), std::min(m_Length, that.m_Length)); return result < 0 || (result == 0 && m_Length < that.m_Length); } protected: - const UnsignedType* m_Ptr; + CFX_UnownedPtr m_Ptr; FX_STRSIZE m_Length; private: diff --git a/core/fxcrt/cfx_unowned_ptr.h b/core/fxcrt/cfx_unowned_ptr.h index bc58144335..d91833abd4 100644 --- a/core/fxcrt/cfx_unowned_ptr.h +++ b/core/fxcrt/cfx_unowned_ptr.h @@ -64,7 +64,7 @@ class CFX_UnownedPtr { #if defined(MEMORY_TOOL_REPLACES_ALLOCATOR) void Probe() { if (m_pObj) - reinterpret_cast(m_pObj)[0]; + reinterpret_cast(m_pObj)[0]; } #endif -- cgit v1.2.3