Bug 1277377 - prevent unsafe C-style cast in Vector (r=waldo)
authorLuke Wagner <luke@mozilla.com>
Thu, 02 Jun 2016 14:38:02 -0500
changeset 341274 8a30acee45af528007141458f1314d818654a52a
parent 341273 0df235f3ea95d90cc8101c4dadc291ac521d05a6
child 341275 8203279f09e3a5eaba76d38cf614f79aff35561f
push id1183
push userraliiev@mozilla.com
push dateMon, 05 Sep 2016 20:01:49 +0000
treeherdermozilla-release@3148731bed45 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerswaldo
bugs1277377
milestone49.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1277377 - prevent unsafe C-style cast in Vector (r=waldo) MozReview-Commit-ID: L0bTDSBHOeY
mfbt/Vector.h
--- a/mfbt/Vector.h
+++ b/mfbt/Vector.h
@@ -158,17 +158,22 @@ struct VectorImpl
  */
 template<typename T, size_t N, class AP>
 struct VectorImpl<T, N, AP, true>
 {
   template<typename... Args>
   MOZ_NONNULL(1)
   static inline void new_(T* aDst, Args&&... aArgs)
   {
-    *aDst = T(Forward<Args>(aArgs)...);
+    // Explicitly construct a local object instead of using a temporary since
+    // T(args...) will be treated like a C-style cast in the unary case and
+    // allow unsafe conversions. Both forms should be equivalent to an
+    // optimizing compiler.
+    T temp(Forward<Args>(aArgs)...);
+    *aDst = temp;
   }
 
   static inline void destroy(T*, T*) {}
 
   static inline void initialize(T* aBegin, T* aEnd)
   {
     /*
      * You would think that memset would be a big win (or even break even)