Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(122)

Side by Side Diff: core/fpdfapi/fpdf_parser/cpdf_array.cpp

Issue 2273293003: Check for nullptrs in CPDF_Dictionary dtor. (Closed)
Patch Set: nit Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | core/fpdfapi/fpdf_parser/cpdf_dictionary.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 PDFium Authors. All rights reserved. 1 // Copyright 2016 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 6
7 #include "core/fpdfapi/fpdf_parser/include/cpdf_array.h" 7 #include "core/fpdfapi/fpdf_parser/include/cpdf_array.h"
8 8
9 #include <set>
10
9 #include "core/fpdfapi/fpdf_parser/include/cpdf_name.h" 11 #include "core/fpdfapi/fpdf_parser/include/cpdf_name.h"
10 #include "core/fpdfapi/fpdf_parser/include/cpdf_number.h" 12 #include "core/fpdfapi/fpdf_parser/include/cpdf_number.h"
11 #include "core/fpdfapi/fpdf_parser/include/cpdf_reference.h" 13 #include "core/fpdfapi/fpdf_parser/include/cpdf_reference.h"
12 #include "core/fpdfapi/fpdf_parser/include/cpdf_stream.h" 14 #include "core/fpdfapi/fpdf_parser/include/cpdf_stream.h"
13 #include "core/fpdfapi/fpdf_parser/include/cpdf_string.h" 15 #include "core/fpdfapi/fpdf_parser/include/cpdf_string.h"
14 #include "third_party/base/stl_util.h" 16 #include "third_party/base/stl_util.h"
15 17
16 CPDF_Array::CPDF_Array() {} 18 CPDF_Array::CPDF_Array() {}
17 19
18 CPDF_Array::~CPDF_Array() { 20 CPDF_Array::~CPDF_Array() {
19 // Mark the object as deleted so that it will not be deleted again 21 // Mark the object as deleted so that it will not be deleted again
20 // in case of cyclic references. 22 // in case of cyclic references.
21 m_ObjNum = kInvalidObjNum; 23 m_ObjNum = kInvalidObjNum;
22 for (auto& it : m_Objects) { 24 for (auto& it : m_Objects) {
23 if (it) 25 if (it)
Lei Zhang 2016/08/25 17:20:24 Same check here.
24 it->Release(); 26 it->Release();
25 } 27 }
26 } 28 }
27 29
28 CPDF_Object::Type CPDF_Array::GetType() const { 30 CPDF_Object::Type CPDF_Array::GetType() const {
29 return ARRAY; 31 return ARRAY;
30 } 32 }
31 33
32 bool CPDF_Array::IsArray() const { 34 bool CPDF_Array::IsArray() const {
33 return true; 35 return true;
(...skipping 10 matching lines...) Expand all
44 CPDF_Object* CPDF_Array::Clone() const { 46 CPDF_Object* CPDF_Array::Clone() const {
45 return CloneObjectNonCyclic(false); 47 return CloneObjectNonCyclic(false);
46 } 48 }
47 49
48 CPDF_Object* CPDF_Array::CloneNonCyclic( 50 CPDF_Object* CPDF_Array::CloneNonCyclic(
49 bool bDirect, 51 bool bDirect,
50 std::set<const CPDF_Object*>* pVisited) const { 52 std::set<const CPDF_Object*>* pVisited) const {
51 pVisited->insert(this); 53 pVisited->insert(this);
52 CPDF_Array* pCopy = new CPDF_Array(); 54 CPDF_Array* pCopy = new CPDF_Array();
53 for (size_t i = 0; i < GetCount(); i++) { 55 for (size_t i = 0; i < GetCount(); i++) {
54 CPDF_Object* value = m_Objects.at(i); 56 CPDF_Object* value = m_Objects[i];
55 if (!pdfium::ContainsKey(*pVisited, value)) 57 if (!pdfium::ContainsKey(*pVisited, value))
56 pCopy->m_Objects.push_back(value->CloneNonCyclic(bDirect, pVisited)); 58 pCopy->m_Objects.push_back(value->CloneNonCyclic(bDirect, pVisited));
57 } 59 }
58 return pCopy; 60 return pCopy;
59 } 61 }
60 62
61 CFX_FloatRect CPDF_Array::GetRect() { 63 CFX_FloatRect CPDF_Array::GetRect() {
62 CFX_FloatRect rect; 64 CFX_FloatRect rect;
63 if (!IsArray() || m_Objects.size() != 4) 65 if (!IsArray() || m_Objects.size() != 4)
64 return rect; 66 return rect;
(...skipping 11 matching lines...) Expand all
76 return matrix; 78 return matrix;
77 79
78 matrix.Set(GetNumberAt(0), GetNumberAt(1), GetNumberAt(2), GetNumberAt(3), 80 matrix.Set(GetNumberAt(0), GetNumberAt(1), GetNumberAt(2), GetNumberAt(3),
79 GetNumberAt(4), GetNumberAt(5)); 81 GetNumberAt(4), GetNumberAt(5));
80 return matrix; 82 return matrix;
81 } 83 }
82 84
83 CPDF_Object* CPDF_Array::GetObjectAt(size_t i) const { 85 CPDF_Object* CPDF_Array::GetObjectAt(size_t i) const {
84 if (i >= m_Objects.size()) 86 if (i >= m_Objects.size())
85 return nullptr; 87 return nullptr;
86 return m_Objects.at(i); 88 return m_Objects[i];
87 } 89 }
88 90
89 CPDF_Object* CPDF_Array::GetDirectObjectAt(size_t i) const { 91 CPDF_Object* CPDF_Array::GetDirectObjectAt(size_t i) const {
90 if (i >= m_Objects.size()) 92 if (i >= m_Objects.size())
91 return nullptr; 93 return nullptr;
92 return m_Objects.at(i)->GetDirect(); 94 return m_Objects[i]->GetDirect();
93 } 95 }
94 96
95 CFX_ByteString CPDF_Array::GetStringAt(size_t i) const { 97 CFX_ByteString CPDF_Array::GetStringAt(size_t i) const {
96 if (i >= m_Objects.size()) 98 if (i >= m_Objects.size())
97 return CFX_ByteString(); 99 return CFX_ByteString();
98 return m_Objects.at(i)->GetString(); 100 return m_Objects[i]->GetString();
99 } 101 }
100 102
101 int CPDF_Array::GetIntegerAt(size_t i) const { 103 int CPDF_Array::GetIntegerAt(size_t i) const {
102 if (i >= m_Objects.size()) 104 if (i >= m_Objects.size())
103 return 0; 105 return 0;
104 return m_Objects.at(i)->GetInteger(); 106 return m_Objects[i]->GetInteger();
105 } 107 }
106 108
107 FX_FLOAT CPDF_Array::GetNumberAt(size_t i) const { 109 FX_FLOAT CPDF_Array::GetNumberAt(size_t i) const {
108 if (i >= m_Objects.size()) 110 if (i >= m_Objects.size())
109 return 0; 111 return 0;
110 return m_Objects.at(i)->GetNumber(); 112 return m_Objects[i]->GetNumber();
111 } 113 }
112 114
113 CPDF_Dictionary* CPDF_Array::GetDictAt(size_t i) const { 115 CPDF_Dictionary* CPDF_Array::GetDictAt(size_t i) const {
114 CPDF_Object* p = GetDirectObjectAt(i); 116 CPDF_Object* p = GetDirectObjectAt(i);
115 if (!p) 117 if (!p)
116 return nullptr; 118 return nullptr;
117 if (CPDF_Dictionary* pDict = p->AsDictionary()) 119 if (CPDF_Dictionary* pDict = p->AsDictionary())
118 return pDict; 120 return pDict;
119 if (CPDF_Stream* pStream = p->AsStream()) 121 if (CPDF_Stream* pStream = p->AsStream())
120 return pStream->GetDict(); 122 return pStream->GetDict();
121 return nullptr; 123 return nullptr;
122 } 124 }
123 125
124 CPDF_Stream* CPDF_Array::GetStreamAt(size_t i) const { 126 CPDF_Stream* CPDF_Array::GetStreamAt(size_t i) const {
125 return ToStream(GetDirectObjectAt(i)); 127 return ToStream(GetDirectObjectAt(i));
126 } 128 }
127 129
128 CPDF_Array* CPDF_Array::GetArrayAt(size_t i) const { 130 CPDF_Array* CPDF_Array::GetArrayAt(size_t i) const {
129 return ToArray(GetDirectObjectAt(i)); 131 return ToArray(GetDirectObjectAt(i));
130 } 132 }
131 133
132 void CPDF_Array::RemoveAt(size_t i, size_t nCount) { 134 void CPDF_Array::RemoveAt(size_t i, size_t nCount) {
133 if (i >= m_Objects.size()) 135 if (i >= m_Objects.size())
134 return; 136 return;
135 137
136 if (nCount <= 0 || nCount > m_Objects.size() - i) 138 if (nCount <= 0 || nCount > m_Objects.size() - i)
137 return; 139 return;
138 140
139 for (size_t j = 0; j < nCount; ++j) { 141 for (size_t j = 0; j < nCount; ++j) {
140 if (CPDF_Object* p = m_Objects.at(i + j)) 142 if (CPDF_Object* p = m_Objects[i + j])
141 p->Release(); 143 p->Release();
142 } 144 }
143 m_Objects.erase(m_Objects.begin() + i, m_Objects.begin() + i + nCount); 145 m_Objects.erase(m_Objects.begin() + i, m_Objects.begin() + i + nCount);
144 } 146 }
145 147
146 void CPDF_Array::SetAt(size_t i, 148 void CPDF_Array::SetAt(size_t i,
147 CPDF_Object* pObj, 149 CPDF_Object* pObj,
148 CPDF_IndirectObjectHolder* pObjs) { 150 CPDF_IndirectObjectHolder* pObjs) {
149 ASSERT(IsArray()); 151 ASSERT(IsArray());
150 ASSERT(i < m_Objects.size()); 152 if (i >= m_Objects.size()) {
151 if (i >= m_Objects.size()) 153 ASSERT(false);
152 return; 154 return;
153 if (CPDF_Object* pOld = m_Objects.at(i)) 155 }
156 if (CPDF_Object* pOld = m_Objects[i])
154 pOld->Release(); 157 pOld->Release();
155 if (pObj->GetObjNum()) { 158 if (pObj->GetObjNum()) {
156 ASSERT(pObjs); 159 ASSERT(pObjs);
157 pObj = new CPDF_Reference(pObjs, pObj->GetObjNum()); 160 pObj = new CPDF_Reference(pObjs, pObj->GetObjNum());
158 } 161 }
159 m_Objects[i] = pObj; 162 m_Objects[i] = pObj;
160 } 163 }
161 164
162 void CPDF_Array::InsertAt(size_t index, 165 void CPDF_Array::InsertAt(size_t index,
163 CPDF_Object* pObj, 166 CPDF_Object* pObj,
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
203 ASSERT(IsArray()); 206 ASSERT(IsArray());
204 CPDF_Number* pNumber = new CPDF_Number(f); 207 CPDF_Number* pNumber = new CPDF_Number(f);
205 Add(pNumber); 208 Add(pNumber);
206 } 209 }
207 210
208 void CPDF_Array::AddReference(CPDF_IndirectObjectHolder* pDoc, 211 void CPDF_Array::AddReference(CPDF_IndirectObjectHolder* pDoc,
209 uint32_t objnum) { 212 uint32_t objnum) {
210 ASSERT(IsArray()); 213 ASSERT(IsArray());
211 Add(new CPDF_Reference(pDoc, objnum)); 214 Add(new CPDF_Reference(pDoc, objnum));
212 } 215 }
OLDNEW
« no previous file with comments | « no previous file | core/fpdfapi/fpdf_parser/cpdf_dictionary.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698