From dd533baad22f5143c093b98e98463a0dc62899ea Mon Sep 17 00:00:00 2001 From: tsepez Date: Thu, 19 Jan 2017 06:56:13 -0800 Subject: use unique_ptr in fpdf_font_cid.cpp Also return CFX_MaybeOwned<> from functions so to make cleanup automatic, avoiding a theoretical leak. Review-Url: https://codereview.chromium.org/2641853004 --- core/fpdfapi/font/cpdf_cidfont.cpp | 6 ++-- core/fpdfapi/font/cpdf_cidfont.h | 4 +-- core/fpdfapi/font/font_int.h | 14 +++++--- core/fpdfapi/font/fpdf_font_cid.cpp | 65 +++++++++++++++++++------------------ 4 files changed, 47 insertions(+), 42 deletions(-) diff --git a/core/fpdfapi/font/cpdf_cidfont.cpp b/core/fpdfapi/font/cpdf_cidfont.cpp index c6c6992562..e96f824bd5 100644 --- a/core/fpdfapi/font/cpdf_cidfont.cpp +++ b/core/fpdfapi/font/cpdf_cidfont.cpp @@ -200,8 +200,7 @@ bool IsMetricForCID(const uint32_t* pEntry, uint16_t CID) { } // namespace CPDF_CIDFont::CPDF_CIDFont() - : m_pCMap(nullptr), - m_pCID2UnicodeMap(nullptr), + : m_pCID2UnicodeMap(nullptr), m_bCIDIsGID(false), m_bAnsiWidthsFixed(false), m_bAdobeCourierStd(false) { @@ -360,8 +359,7 @@ bool CPDF_CIDFont::Load() { if (!m_pCMap) return false; } else if (CPDF_Stream* pStream = pEncoding->AsStream()) { - m_pCMap = new CPDF_CMap; - m_pAllocatedCMap.reset(m_pCMap); + m_pCMap = pdfium::MakeUnique(); CPDF_StreamAcc acc; acc.LoadAllData(pStream, false); m_pCMap->LoadEmbedded(acc.GetData(), acc.GetSize()); diff --git a/core/fpdfapi/font/cpdf_cidfont.h b/core/fpdfapi/font/cpdf_cidfont.h index ae3be3908f..79ec7cbb35 100644 --- a/core/fpdfapi/font/cpdf_cidfont.h +++ b/core/fpdfapi/font/cpdf_cidfont.h @@ -11,6 +11,7 @@ #include #include "core/fpdfapi/font/cpdf_font.h" +#include "core/fxcrt/cfx_maybe_owned.h" #include "core/fxcrt/fx_string.h" #include "core/fxcrt/fx_system.h" @@ -71,8 +72,7 @@ class CPDF_CIDFont : public CPDF_Font { void LoadSubstFont(); FX_WCHAR GetUnicodeFromCharCode(uint32_t charcode) const; - CPDF_CMap* m_pCMap; - std::unique_ptr m_pAllocatedCMap; + CFX_MaybeOwned m_pCMap; CPDF_CID2UnicodeMap* m_pCID2UnicodeMap; CIDSet m_Charset; bool m_bType1; diff --git a/core/fpdfapi/font/font_int.h b/core/fpdfapi/font/font_int.h index 2410741e53..41d821ec81 100644 --- a/core/fpdfapi/font/font_int.h +++ b/core/fpdfapi/font/font_int.h @@ -12,6 +12,7 @@ #include #include "core/fpdfapi/font/cpdf_cidfont.h" +#include "core/fxcrt/cfx_maybe_owned.h" #include "core/fxcrt/fx_basic.h" class CPDF_CID2UnicodeMap; @@ -30,15 +31,18 @@ class CPDF_CMapManager { CPDF_CMapManager(); ~CPDF_CMapManager(); - CPDF_CMap* GetPredefinedCMap(const CFX_ByteString& name, bool bPromptCJK); + CFX_MaybeOwned GetPredefinedCMap(const CFX_ByteString& name, + bool bPromptCJK); CPDF_CID2UnicodeMap* GetCID2UnicodeMap(CIDSet charset, bool bPromptCJK); private: - CPDF_CMap* LoadPredefinedCMap(const CFX_ByteString& name, bool bPromptCJK); - CPDF_CID2UnicodeMap* LoadCID2UnicodeMap(CIDSet charset, bool bPromptCJK); + std::unique_ptr LoadPredefinedCMap(const CFX_ByteString& name, + bool bPromptCJK); + std::unique_ptr LoadCID2UnicodeMap(CIDSet charset, + bool bPromptCJK); - std::map m_CMaps; - CPDF_CID2UnicodeMap* m_CID2UnicodeMaps[6]; + std::map> m_CMaps; + std::unique_ptr m_CID2UnicodeMaps[6]; }; class CFX_StockFontArray { diff --git a/core/fpdfapi/font/fpdf_font_cid.cpp b/core/fpdfapi/font/fpdf_font_cid.cpp index 010c94ced2..386eec334a 100644 --- a/core/fpdfapi/font/fpdf_font_cid.cpp +++ b/core/fpdfapi/font/fpdf_font_cid.cpp @@ -6,6 +6,9 @@ #include "core/fpdfapi/font/font_int.h" +#include +#include + #include "core/fpdfapi/cmaps/cmap_int.h" #include "core/fpdfapi/cpdf_modulemgr.h" #include "core/fpdfapi/font/ttgsubtable.h" @@ -278,37 +281,34 @@ int GetCharSizeImpl(uint32_t charcode, } // namespace -CPDF_CMapManager::CPDF_CMapManager() { - FXSYS_memset(m_CID2UnicodeMaps, 0, sizeof m_CID2UnicodeMaps); -} -CPDF_CMapManager::~CPDF_CMapManager() { - for (const auto& pair : m_CMaps) { - delete pair.second; - } - m_CMaps.clear(); - for (size_t i = 0; i < FX_ArraySize(m_CID2UnicodeMaps); ++i) { - delete m_CID2UnicodeMaps[i]; - } -} -CPDF_CMap* CPDF_CMapManager::GetPredefinedCMap(const CFX_ByteString& name, - bool bPromptCJK) { +CPDF_CMapManager::CPDF_CMapManager() {} + +CPDF_CMapManager::~CPDF_CMapManager() {} + +CFX_MaybeOwned CPDF_CMapManager::GetPredefinedCMap( + const CFX_ByteString& name, + bool bPromptCJK) { auto it = m_CMaps.find(name); - if (it != m_CMaps.end()) { - return it->second; - } - CPDF_CMap* pCMap = LoadPredefinedCMap(name, bPromptCJK); - if (!name.IsEmpty()) { - m_CMaps[name] = pCMap; - } - return pCMap; + if (it != m_CMaps.end()) + return CFX_MaybeOwned(it->second.get()); // Unowned. + + std::unique_ptr pCMap = LoadPredefinedCMap(name, bPromptCJK); + if (name.IsEmpty()) + return CFX_MaybeOwned(std::move(pCMap)); // Owned. + + CPDF_CMap* pUnowned = pCMap.get(); + m_CMaps[name] = std::move(pCMap); + return CFX_MaybeOwned(pUnowned); // Unowned. } -CPDF_CMap* CPDF_CMapManager::LoadPredefinedCMap(const CFX_ByteString& name, - bool bPromptCJK) { - CPDF_CMap* pCMap = new CPDF_CMap; + +std::unique_ptr CPDF_CMapManager::LoadPredefinedCMap( + const CFX_ByteString& name, + bool bPromptCJK) { + auto pCMap = pdfium::MakeUnique(); const FX_CHAR* pname = name.c_str(); - if (*pname == '/') { + if (*pname == '/') pname++; - } + pCMap->LoadPredefined(this, pname, bPromptCJK); return pCMap; } @@ -317,11 +317,14 @@ CPDF_CID2UnicodeMap* CPDF_CMapManager::GetCID2UnicodeMap(CIDSet charset, bool bPromptCJK) { if (!m_CID2UnicodeMaps[charset]) m_CID2UnicodeMaps[charset] = LoadCID2UnicodeMap(charset, bPromptCJK); - return m_CID2UnicodeMaps[charset]; + + return m_CID2UnicodeMaps[charset].get(); } -CPDF_CID2UnicodeMap* CPDF_CMapManager::LoadCID2UnicodeMap(CIDSet charset, - bool bPromptCJK) { - CPDF_CID2UnicodeMap* pMap = new CPDF_CID2UnicodeMap(); + +std::unique_ptr CPDF_CMapManager::LoadCID2UnicodeMap( + CIDSet charset, + bool bPromptCJK) { + auto pMap = pdfium::MakeUnique(); pMap->Load(this, charset, bPromptCJK); return pMap; } -- cgit v1.2.3