diff options
author | tsepez <tsepez@chromium.org> | 2016-09-19 14:05:05 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2016-09-19 14:05:05 -0700 |
commit | c8544d634a1993e2592e41458be215fcd0956031 (patch) | |
tree | e71b04e3ceea55454d8b8f48d1750117f347f958 /core/fpdfapi/fpdf_parser | |
parent | c6c2e36f59f54b17d9e2c54a2003166e79ccb5e6 (diff) | |
download | pdfium-c8544d634a1993e2592e41458be215fcd0956031.tar.xz |
Make CPDF_IndirectObjectHolder use unique_ptr to objects
The objects it is given are owned by it and are simply
deleted without regard to Release() used by others.
Review-Url: https://codereview.chromium.org/2350263002
Diffstat (limited to 'core/fpdfapi/fpdf_parser')
4 files changed, 16 insertions, 24 deletions
diff --git a/core/fpdfapi/fpdf_parser/cfdf_document.cpp b/core/fpdfapi/fpdf_parser/cfdf_document.cpp index 0182948173..92b20cafd8 100644 --- a/core/fpdfapi/fpdf_parser/cfdf_document.cpp +++ b/core/fpdfapi/fpdf_parser/cfdf_document.cpp @@ -90,7 +90,8 @@ FX_BOOL CFDF_Document::WriteBuf(CFX_ByteTextBuf& buf) const { buf << "%FDF-1.2\r\n"; for (const auto& pair : *this) - buf << pair.first << " 0 obj\r\n" << pair.second << "\r\nendobj\r\n\r\n"; + buf << pair.first << " 0 obj\r\n" + << pair.second.get() << "\r\nendobj\r\n\r\n"; buf << "trailer\r\n<</Root " << m_pRootDict->GetObjNum() << " 0 R>>\r\n%%EOF\r\n"; diff --git a/core/fpdfapi/fpdf_parser/cpdf_indirect_object_holder.cpp b/core/fpdfapi/fpdf_parser/cpdf_indirect_object_holder.cpp index dc639feb1a..800e34b3d1 100644 --- a/core/fpdfapi/fpdf_parser/cpdf_indirect_object_holder.cpp +++ b/core/fpdfapi/fpdf_parser/cpdf_indirect_object_holder.cpp @@ -11,15 +11,12 @@ CPDF_IndirectObjectHolder::CPDF_IndirectObjectHolder() : m_LastObjNum(0) {} -CPDF_IndirectObjectHolder::~CPDF_IndirectObjectHolder() { - for (const auto& pair : m_IndirectObjs) - delete pair.second; -} +CPDF_IndirectObjectHolder::~CPDF_IndirectObjectHolder() {} CPDF_Object* CPDF_IndirectObjectHolder::GetIndirectObject( uint32_t objnum) const { auto it = m_IndirectObjs.find(objnum); - return it != m_IndirectObjs.end() ? it->second : nullptr; + return it != m_IndirectObjs.end() ? it->second.get() : nullptr; } CPDF_Object* CPDF_IndirectObjectHolder::GetOrParseIndirectObject( @@ -37,10 +34,7 @@ CPDF_Object* CPDF_IndirectObjectHolder::GetOrParseIndirectObject( pObj->m_ObjNum = objnum; m_LastObjNum = std::max(m_LastObjNum, objnum); - if (m_IndirectObjs[objnum]) - delete m_IndirectObjs[objnum]; - - m_IndirectObjs[objnum] = pObj; + m_IndirectObjs[objnum].reset(pObj); return pObj; } @@ -53,7 +47,7 @@ uint32_t CPDF_IndirectObjectHolder::AddIndirectObject(CPDF_Object* pObj) { return pObj->m_ObjNum; m_LastObjNum++; - m_IndirectObjs[m_LastObjNum] = pObj; + m_IndirectObjs[m_LastObjNum].reset(pObj); pObj->m_ObjNum = m_LastObjNum; return m_LastObjNum; } @@ -65,15 +59,12 @@ bool CPDF_IndirectObjectHolder::ReplaceIndirectObjectIfHigherGeneration( return false; CPDF_Object* pOldObj = GetIndirectObject(objnum); - if (pOldObj) { - if (pObj->GetGenNum() <= pOldObj->GetGenNum()) { - delete pObj; - return false; - } - delete pOldObj; + if (pOldObj && pObj->GetGenNum() <= pOldObj->GetGenNum()) { + delete pObj; + return false; } pObj->m_ObjNum = objnum; - m_IndirectObjs[objnum] = pObj; + m_IndirectObjs[objnum].reset(pObj); m_LastObjNum = std::max(m_LastObjNum, objnum); return true; } @@ -83,6 +74,5 @@ void CPDF_IndirectObjectHolder::ReleaseIndirectObject(uint32_t objnum) { if (!pObj || pObj->GetObjNum() == CPDF_Object::kInvalidObjNum) return; - delete pObj; m_IndirectObjs.erase(objnum); } diff --git a/core/fpdfapi/fpdf_parser/include/cpdf_indirect_object_holder.h b/core/fpdfapi/fpdf_parser/include/cpdf_indirect_object_holder.h index 57dafb0528..2abbda2a36 100644 --- a/core/fpdfapi/fpdf_parser/include/cpdf_indirect_object_holder.h +++ b/core/fpdfapi/fpdf_parser/include/cpdf_indirect_object_holder.h @@ -8,6 +8,7 @@ #define CORE_FPDFAPI_FPDF_PARSER_INCLUDE_CPDF_INDIRECT_OBJECT_HOLDER_H_ #include <map> +#include <memory> #include "core/fxcrt/include/fx_system.h" @@ -15,8 +16,8 @@ class CPDF_Object; class CPDF_IndirectObjectHolder { public: - using iterator = std::map<uint32_t, CPDF_Object*>::iterator; - using const_iterator = std::map<uint32_t, CPDF_Object*>::const_iterator; + using const_iterator = + std::map<uint32_t, std::unique_ptr<CPDF_Object>>::const_iterator; CPDF_IndirectObjectHolder(); virtual ~CPDF_IndirectObjectHolder(); @@ -33,9 +34,7 @@ class CPDF_IndirectObjectHolder { uint32_t GetLastObjNum() const { return m_LastObjNum; } void SetLastObjNum(uint32_t objnum) { m_LastObjNum = objnum; } - iterator begin() { return m_IndirectObjs.begin(); } const_iterator begin() const { return m_IndirectObjs.begin(); } - iterator end() { return m_IndirectObjs.end(); } const_iterator end() const { return m_IndirectObjs.end(); } protected: @@ -43,7 +42,7 @@ class CPDF_IndirectObjectHolder { private: uint32_t m_LastObjNum; - std::map<uint32_t, CPDF_Object*> m_IndirectObjs; + std::map<uint32_t, std::unique_ptr<CPDF_Object>> m_IndirectObjs; }; #endif // CORE_FPDFAPI_FPDF_PARSER_INCLUDE_CPDF_INDIRECT_OBJECT_HOLDER_H_ diff --git a/core/fpdfapi/fpdf_parser/include/cpdf_object.h b/core/fpdfapi/fpdf_parser/include/cpdf_object.h index 115ce02200..de14503bbb 100644 --- a/core/fpdfapi/fpdf_parser/include/cpdf_object.h +++ b/core/fpdfapi/fpdf_parser/include/cpdf_object.h @@ -7,6 +7,7 @@ #ifndef CORE_FPDFAPI_FPDF_PARSER_INCLUDE_CPDF_OBJECT_H_ #define CORE_FPDFAPI_FPDF_PARSER_INCLUDE_CPDF_OBJECT_H_ +#include <memory> #include <set> #include "core/fxcrt/include/fx_string.h" @@ -92,6 +93,7 @@ class CPDF_Object { friend class CPDF_Parser; friend class CPDF_Reference; friend class CPDF_Stream; + friend struct std::default_delete<CPDF_Object>; CPDF_Object() : m_ObjNum(0), m_GenNum(0) {} virtual ~CPDF_Object(); |