From 6136ec6347a5858a85912a805ea41126863558cd Mon Sep 17 00:00:00 2001 From: tsepez Date: Tue, 22 Nov 2016 13:01:40 -0800 Subject: Ensure CPDF_CountedObjects only made from owned references. Deletion of said object is still inflicted on the callers. But that's an issue for another day. Review-Url: https://codereview.chromium.org/2523743004 --- core/fpdfapi/page/cpdf_countedobject.h | 9 ++++-- core/fpdfapi/page/cpdf_docpagedata.cpp | 51 ++++++++++++++++-------------- core/fpdfapi/render/cpdf_docrenderdata.cpp | 9 +++--- 3 files changed, 38 insertions(+), 31 deletions(-) diff --git a/core/fpdfapi/page/cpdf_countedobject.h b/core/fpdfapi/page/cpdf_countedobject.h index 97d710cf76..64f936c52c 100644 --- a/core/fpdfapi/page/cpdf_countedobject.h +++ b/core/fpdfapi/page/cpdf_countedobject.h @@ -7,6 +7,8 @@ #ifndef CORE_FPDFAPI_PAGE_CPDF_COUNTEDOBJECT_H_ #define CORE_FPDFAPI_PAGE_CPDF_COUNTEDOBJECT_H_ +#include + #include "core/fpdfapi/page/cpdf_colorspace.h" #include "core/fpdfapi/page/cpdf_pattern.h" #include "core/fxcrt/fx_system.h" @@ -14,10 +16,11 @@ template class CPDF_CountedObject { public: - explicit CPDF_CountedObject(T* ptr) : m_nCount(1), m_pObj(ptr) {} - void reset(T* ptr) { // CAUTION: tosses prior ref counts. + explicit CPDF_CountedObject(std::unique_ptr ptr) + : m_nCount(1), m_pObj(ptr.release()) {} + void reset(std::unique_ptr ptr) { // CAUTION: tosses prior ref counts. m_nCount = 1; - m_pObj = ptr; + m_pObj = ptr.release(); } void clear() { // Now you're all weak ptrs ... // Guard against accidental re-entry. diff --git a/core/fpdfapi/page/cpdf_docpagedata.cpp b/core/fpdfapi/page/cpdf_docpagedata.cpp index 1c99d523b0..af2618286e 100644 --- a/core/fpdfapi/page/cpdf_docpagedata.cpp +++ b/core/fpdfapi/page/cpdf_docpagedata.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include "core/fdrm/crypto/fx_crypt.h" #include "core/fpdfapi/cpdf_modulemgr.h" @@ -143,9 +144,9 @@ CPDF_Font* CPDF_DocPageData::GetFont(CPDF_Dictionary* pFontDict) { return nullptr; if (pFontData) { - pFontData->reset(pFont.release()); + pFontData->reset(std::move(pFont)); } else { - pFontData = new CPDF_CountedFont(pFont.release()); + pFontData = new CPDF_CountedFont(std::move(pFont)); m_FontMap[pFontDict] = pFontData; } return pFontData->AddRef(); @@ -190,7 +191,7 @@ CPDF_Font* CPDF_DocPageData::GetStandardFont(const CFX_ByteString& fontName, if (!pFont) return nullptr; - CPDF_CountedFont* fontData = new CPDF_CountedFont(pFont.release()); + CPDF_CountedFont* fontData = new CPDF_CountedFont(std::move(pFont)); m_FontMap[pDict] = fontData; return fontData->AddRef(); } @@ -293,11 +294,11 @@ CPDF_ColorSpace* CPDF_DocPageData::GetColorSpaceImpl( if (!pCS) return nullptr; - if (!csData) { - csData = new CPDF_CountedColorSpace(pCS.release()); - m_ColorSpaceMap[pCSObj] = csData; + if (csData) { + csData->reset(std::move(pCS)); } else { - csData->reset(pCS.release()); + csData = new CPDF_CountedColorSpace(std::move(pCS)); + m_ColorSpaceMap[pCSObj] = csData; } return csData->AddRef(); } @@ -348,29 +349,31 @@ CPDF_Pattern* CPDF_DocPageData::GetPattern(CPDF_Object* pPatternObj, return ptData->AddRef(); } } - CPDF_Pattern* pPattern = nullptr; + std::unique_ptr pPattern; if (bShading) { - pPattern = new CPDF_ShadingPattern(m_pPDFDoc, pPatternObj, true, matrix); + pPattern = pdfium::MakeUnique(m_pPDFDoc, pPatternObj, + true, matrix); } else { CPDF_Dictionary* pDict = pPatternObj ? pPatternObj->GetDict() : nullptr; if (pDict) { int type = pDict->GetIntegerFor("PatternType"); if (type == CPDF_Pattern::TILING) { - pPattern = new CPDF_TilingPattern(m_pPDFDoc, pPatternObj, matrix); + pPattern = pdfium::MakeUnique(m_pPDFDoc, + pPatternObj, matrix); } else if (type == CPDF_Pattern::SHADING) { - pPattern = - new CPDF_ShadingPattern(m_pPDFDoc, pPatternObj, false, matrix); + pPattern = pdfium::MakeUnique( + m_pPDFDoc, pPatternObj, false, matrix); } } } if (!pPattern) return nullptr; - if (!ptData) { - ptData = new CPDF_CountedPattern(pPattern); - m_PatternMap[pPatternObj] = ptData; + if (ptData) { + ptData->reset(std::move(pPattern)); } else { - ptData->reset(pPattern); + ptData = new CPDF_CountedPattern(std::move(pPattern)); + m_PatternMap[pPatternObj] = ptData; } return ptData->AddRef(); } @@ -401,8 +404,8 @@ CPDF_Image* CPDF_DocPageData::GetImage(uint32_t dwStreamObjNum) { if (it != m_ImageMap.end()) return it->second->AddRef(); - CPDF_CountedImage* pCountedImage = - new CPDF_CountedImage(new CPDF_Image(m_pPDFDoc, dwStreamObjNum)); + CPDF_CountedImage* pCountedImage = new CPDF_CountedImage( + pdfium::MakeUnique(m_pPDFDoc, dwStreamObjNum)); m_ImageMap[dwStreamObjNum] = pCountedImage; return pCountedImage->AddRef(); } @@ -447,9 +450,8 @@ CPDF_IccProfile* CPDF_DocPageData::GetIccProfile( if (it_copied_stream != m_IccProfileMap.end()) return it_copied_stream->second->AddRef(); } - CPDF_IccProfile* pProfile = - new CPDF_IccProfile(stream.GetData(), stream.GetSize()); - CPDF_CountedIccProfile* ipData = new CPDF_CountedIccProfile(pProfile); + CPDF_CountedIccProfile* ipData = new CPDF_CountedIccProfile( + pdfium::MakeUnique(stream.GetData(), stream.GetSize())); m_IccProfileMap[pIccProfileStream] = ipData; m_HashProfileMap[bsDigest] = pIccProfileStream; return ipData->AddRef(); @@ -488,10 +490,11 @@ CPDF_StreamAcc* CPDF_DocPageData::GetFontFileStreamAcc( pFontDict->GetIntegerFor("Length3"); org_size = std::max(org_size, 0); - CPDF_StreamAcc* pFontFile = new CPDF_StreamAcc; - pFontFile->LoadAllData(pFontStream, false, org_size); + auto pFontAcc = pdfium::MakeUnique(); + pFontAcc->LoadAllData(pFontStream, false, org_size); - CPDF_CountedStreamAcc* pCountedFont = new CPDF_CountedStreamAcc(pFontFile); + CPDF_CountedStreamAcc* pCountedFont = + new CPDF_CountedStreamAcc(std::move(pFontAcc)); m_FontFileMap[pFontStream] = pCountedFont; return pCountedFont->AddRef(); } diff --git a/core/fpdfapi/render/cpdf_docrenderdata.cpp b/core/fpdfapi/render/cpdf_docrenderdata.cpp index c17b4d594e..7ac0353d42 100644 --- a/core/fpdfapi/render/cpdf_docrenderdata.cpp +++ b/core/fpdfapi/render/cpdf_docrenderdata.cpp @@ -55,8 +55,8 @@ CPDF_Type3Cache* CPDF_DocRenderData::GetCachedType3(CPDF_Type3Font* pFont) { CPDF_CountedObject* pCache; auto it = m_Type3FaceMap.find(pFont); if (it == m_Type3FaceMap.end()) { - CPDF_Type3Cache* pType3 = new CPDF_Type3Cache(pFont); - pCache = new CPDF_CountedObject(pType3); + pCache = new CPDF_CountedObject( + pdfium::MakeUnique(pFont)); m_Type3FaceMap[pFont] = pCache; } else { pCache = it->second; @@ -104,9 +104,10 @@ CPDF_TransferFunc* CPDF_DocRenderData::GetTransferFunc(CPDF_Object* pObj) { if (!pFuncs[0]) return nullptr; } - CPDF_TransferFunc* pTransfer = new CPDF_TransferFunc(m_pPDFDoc); CPDF_CountedObject* pTransferCounter = - new CPDF_CountedObject(pTransfer); + new CPDF_CountedObject( + pdfium::MakeUnique(m_pPDFDoc)); + CPDF_TransferFunc* pTransfer = pTransferCounter->get(); m_TransferFuncMap[pObj] = pTransferCounter; FX_FLOAT output[kMaxOutputs]; FXSYS_memset(output, 0, sizeof(output)); -- cgit v1.2.3