Bug 1415083 - Rearrange the declaration of objects to avoid stack-use-after-scope. draft
authorJames Cheng <jacheng@mozilla.com>
Tue, 07 Nov 2017 17:02:54 +0800
changeset 694015 5d256016ce551d1316179c255a535b7f1e395533
parent 693409 d5732e78f529724643a220c1426c8bf900b8f2f4
child 739234 446a270bff8d478e27736e16cb9037e2d949f62e
push id88021
push userbmo:jacheng@mozilla.com
push dateTue, 07 Nov 2017 09:03:22 +0000
bugs1415083
milestone58.0a1
Bug 1415083 - Rearrange the declaration of objects to avoid stack-use-after-scope. MozReview-Commit-ID: 7nMw1i10b7e
xpcom/tests/gtest/TestTArray.cpp
--- a/xpcom/tests/gtest/TestTArray.cpp
+++ b/xpcom/tests/gtest/TestTArray.cpp
@@ -139,22 +139,22 @@ TEST(TArray, AssignmentOperatorSelfAssig
   array = array;
   ASSERT_EQ(DummyArray(), array);
   array = Move(array);
   ASSERT_EQ(DummyArray(), array);
 }
 
 TEST(TArray, CopyOverlappingForwards)
 {
-  nsTArray<Movable> array;
   const size_t rangeLength = 8;
   const size_t initialLength = 2 * rangeLength;
+  uint32_t destructionCounters[initialLength];
+  nsTArray<Movable> array;
   array.AppendElements(initialLength);
 
-  uint32_t destructionCounters[initialLength];
   for (uint32_t i = 0; i < initialLength; ++i) {
     destructionCounters[i] = 0;
   }
   for (uint32_t i = 0; i < initialLength; ++i) {
     array[i].mDestructionCounter = &destructionCounters[i];
   }
 
   const size_t removedLength = rangeLength / 2;
@@ -167,33 +167,33 @@ TEST(TArray, CopyOverlappingForwards)
     ASSERT_EQ(destructionCounters[i], 0u);
   }
 }
 
 // The code to copy overlapping regions had a bug in that it wouldn't correctly
 // destroy all over the source elements being copied.
 TEST(TArray, CopyOverlappingBackwards)
 {
-  nsTArray<Copyable> array;
   const size_t rangeLength = 8;
   const size_t initialLength = 2 * rangeLength;
+  uint32_t destructionCounters[initialLength];
+  nsTArray<Copyable> array;
   array.SetCapacity(3 * rangeLength);
   array.AppendElements(initialLength);
   // To tickle the bug, we need to copy a source region:
   //
   //   ..XXXXX..
   //
   // such that it overlaps the destination region:
   //
   //   ....XXXXX
   //
   // so we are forced to copy back-to-front to ensure correct behavior.
   // The easiest way to do that is to call InsertElementsAt, which will force
   // the desired kind of shift.
-  uint32_t destructionCounters[initialLength];
   for (uint32_t i = 0; i < initialLength; ++i) {
     destructionCounters[i] = 0;
   }
   for (uint32_t i = 0; i < initialLength; ++i) {
     array[i].mDestructionCounter = &destructionCounters[i];
   }
 
   array.InsertElementsAt(0, rangeLength);