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

Side by Side Diff: include/private/SkTArray.h

Issue 1908763002: SkTArray: fix invalid reinterpret_casts over non-initialized memory. (Closed) Base URL: https://chromium.googlesource.com/skia.git@master
Patch Set: Created 4 years, 8 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 | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2011 Google Inc. 2 * Copyright 2011 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #ifndef SkTArray_DEFINED 8 #ifndef SkTArray_DEFINED
9 #define SkTArray_DEFINED 9 #define SkTArray_DEFINED
10 10
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
152 * Is the array empty. 152 * Is the array empty.
153 */ 153 */
154 bool empty() const { return !fCount; } 154 bool empty() const { return !fCount; }
155 155
156 /** 156 /**
157 * Adds 1 new default-initialized T value and returns it by reference. Note 157 * Adds 1 new default-initialized T value and returns it by reference. Note
158 * the reference only remains valid until the next call that adds or removes 158 * the reference only remains valid until the next call that adds or removes
159 * elements. 159 * elements.
160 */ 160 */
161 T& push_back() { 161 T& push_back() {
162 T* newT = reinterpret_cast<T*>(this->push_back_raw(1)); 162 void* newT = this->push_back_raw(1);
163 new (newT) T; 163 return *new (newT) T;
164 return *newT;
165 } 164 }
166 165
167 /** 166 /**
168 * Version of above that uses a copy constructor to initialize the new item 167 * Version of above that uses a copy constructor to initialize the new item
169 */ 168 */
170 T& push_back(const T& t) { 169 T& push_back(const T& t) {
171 T* newT = reinterpret_cast<T*>(this->push_back_raw(1)); 170 void* newT = this->push_back_raw(1);
172 new (newT) T(t); 171 return *new (newT) T(t);
173 return *newT;
174 } 172 }
175 173
176 /** 174 /**
177 * Version of above that uses a move constructor to initialize the new item 175 * Version of above that uses a move constructor to initialize the new item
178 */ 176 */
179 T& push_back(T&& t) { 177 T& push_back(T&& t) {
180 T* newT = reinterpret_cast<T*>(this->push_back_raw(1)); 178 void* newT = this->push_back_raw(1);
181 new (newT) T(std::move(t)); 179 return *new (newT) T(std::move(t));
182 return *newT;
183 } 180 }
184 181
185 /** 182 /**
186 * Construct a new T at the back of this array. 183 * Construct a new T at the back of this array.
187 */ 184 */
188 template<class... Args> T& emplace_back(Args&&... args) { 185 template<class... Args> T& emplace_back(Args&&... args) {
189 T* newT = reinterpret_cast<T*>(this->push_back_raw(1)); 186 void* newT = this->push_back_raw(1);
190 return *new (newT) T(std::forward<Args>(args)...); 187 return *new (newT) T(std::forward<Args>(args)...);
191 } 188 }
192 189
193 /** 190 /**
194 * Allocates n more default-initialized T values, and returns the address of 191 * Allocates n more default-initialized T values, and returns the address of
195 * the start of that new range. Note: this address is only valid until the 192 * the start of that new range. Note: this address is only valid until the
196 * next API call made on the array that might add or remove elements. 193 * next API call made on the array that might add or remove elements.
197 */ 194 */
198 T* push_back_n(int n) { 195 T* push_back_n(int n) {
199 SkASSERT(n >= 0); 196 SkASSERT(n >= 0);
200 T* newTs = reinterpret_cast<T*>(this->push_back_raw(n)); 197 char* newTs = static_cast<char*>(this->push_back_raw(n));
201 for (int i = 0; i < n; ++i) { 198 for (int i = 0; i < n; ++i) {
202 new (newTs + i) T; 199 new (newTs + i * sizeof(T)) T;
bungeman-skia 2016/04/21 15:32:20 nit: skia uses four space indents
203 } 200 }
204 return newTs; 201 return reinterpret_cast<T*>(newTs);
205 } 202 }
206 203
207 /** 204 /**
208 * Version of above that uses a copy constructor to initialize all n items 205 * Version of above that uses a copy constructor to initialize all n items
209 * to the same T. 206 * to the same T.
210 */ 207 */
211 T* push_back_n(int n, const T& t) { 208 T* push_back_n(int n, const T& t) {
212 SkASSERT(n >= 0); 209 SkASSERT(n >= 0);
213 T* newTs = reinterpret_cast<T*>(this->push_back_raw(n)); 210 char* newTs = static_cast<char*>(this->push_back_raw(n));
214 for (int i = 0; i < n; ++i) { 211 for (int i = 0; i < n; ++i) {
215 new (newTs + i) T(t); 212 new (newTs + i * sizeof(T)) T(t);
bungeman-skia 2016/04/21 15:32:20 nit: four space
216 } 213 }
217 return newTs; 214 return reinterpret_cast<T*>(newTs);
218 } 215 }
219 216
220 /** 217 /**
221 * Version of above that uses a copy constructor to initialize the n items 218 * Version of above that uses a copy constructor to initialize the n items
222 * to separate T values. 219 * to separate T values.
223 */ 220 */
224 T* push_back_n(int n, const T t[]) { 221 T* push_back_n(int n, const T t[]) {
225 SkASSERT(n >= 0); 222 SkASSERT(n >= 0);
226 this->checkRealloc(n); 223 this->checkRealloc(n);
227 for (int i = 0; i < n; ++i) { 224 for (int i = 0; i < n; ++i) {
(...skipping 304 matching lines...) Expand 10 before | Expand all | Expand 10 after
532 SkSTArray& operator= (const INHERITED& array) { 529 SkSTArray& operator= (const INHERITED& array) {
533 INHERITED::operator=(array); 530 INHERITED::operator=(array);
534 return *this; 531 return *this;
535 } 532 }
536 533
537 private: 534 private:
538 SkAlignedSTStorage<N,T> fStorage; 535 SkAlignedSTStorage<N,T> fStorage;
539 }; 536 };
540 537
541 #endif 538 #endif
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698