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

Unified 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: include/private/SkTArray.h
diff --git a/include/private/SkTArray.h b/include/private/SkTArray.h
index 043784f093a2169ce5d640ea11c020eee2d8d1ac..8d8f6598db96862df6f7cceb71e83f360e1f35cb 100644
--- a/include/private/SkTArray.h
+++ b/include/private/SkTArray.h
@@ -159,34 +159,31 @@ public:
* elements.
*/
T& push_back() {
- T* newT = reinterpret_cast<T*>(this->push_back_raw(1));
- new (newT) T;
- return *newT;
+ void* newT = this->push_back_raw(1);
+ return *new (newT) T;
}
/**
* Version of above that uses a copy constructor to initialize the new item
*/
T& push_back(const T& t) {
- T* newT = reinterpret_cast<T*>(this->push_back_raw(1));
- new (newT) T(t);
- return *newT;
+ void* newT = this->push_back_raw(1);
+ return *new (newT) T(t);
}
/**
* Version of above that uses a move constructor to initialize the new item
*/
T& push_back(T&& t) {
- T* newT = reinterpret_cast<T*>(this->push_back_raw(1));
- new (newT) T(std::move(t));
- return *newT;
+ void* newT = this->push_back_raw(1);
+ return *new (newT) T(std::move(t));
}
/**
* Construct a new T at the back of this array.
*/
template<class... Args> T& emplace_back(Args&&... args) {
- T* newT = reinterpret_cast<T*>(this->push_back_raw(1));
+ void* newT = this->push_back_raw(1);
return *new (newT) T(std::forward<Args>(args)...);
}
@@ -197,11 +194,11 @@ public:
*/
T* push_back_n(int n) {
SkASSERT(n >= 0);
- T* newTs = reinterpret_cast<T*>(this->push_back_raw(n));
+ char* newTs = static_cast<char*>(this->push_back_raw(n));
for (int i = 0; i < n; ++i) {
- new (newTs + i) T;
+ new (newTs + i * sizeof(T)) T;
bungeman-skia 2016/04/21 15:32:20 nit: skia uses four space indents
}
- return newTs;
+ return reinterpret_cast<T*>(newTs);
}
/**
@@ -210,11 +207,11 @@ public:
*/
T* push_back_n(int n, const T& t) {
SkASSERT(n >= 0);
- T* newTs = reinterpret_cast<T*>(this->push_back_raw(n));
+ char* newTs = static_cast<char*>(this->push_back_raw(n));
for (int i = 0; i < n; ++i) {
- new (newTs + i) T(t);
+ new (newTs + i * sizeof(T)) T(t);
bungeman-skia 2016/04/21 15:32:20 nit: four space
}
- return newTs;
+ return reinterpret_cast<T*>(newTs);
}
/**
« 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