Bug 1415083 - Rearrange the declaration of objects to avoid stack-use-after-scope.
MozReview-Commit-ID: 7nMw1i10b7e
--- 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);