diff --git a/xpcom/ds/nsTArray-inl.h b/xpcom/ds/nsTArray-inl.h index 95bd99ffcf7a..e2790af90c65 100644 --- a/xpcom/ds/nsTArray-inl.h +++ b/xpcom/ds/nsTArray-inl.h @@ -461,9 +461,8 @@ nsTArray_base::SwapArrayElements(nsTArray_base, 64> temp; - if (!ActualAlloc::Successful(temp.template EnsureCapacity(smallerLength, - aElemSize))) { + AutoTArray temp; + if (!ActualAlloc::Successful(temp.template EnsureCapacity(smallerLength * aElemSize, sizeof(uint8_t)))) { return ActualAlloc::FailureResult(); } diff --git a/xpcom/tests/gtest/TestTArray2.cpp b/xpcom/tests/gtest/TestTArray2.cpp index 91804ba05b22..ac00a9336c78 100644 --- a/xpcom/tests/gtest/TestTArray2.cpp +++ b/xpcom/tests/gtest/TestTArray2.cpp @@ -1086,4 +1086,29 @@ TEST(TArray, test_comparator_objects) { ASSERT_TRUE(TestCompareMethods([] (int aLeft, int aRight) { return aLeft - aRight; })); } +struct Big +{ + uint64_t size[40] = {}; +}; + +TEST(TArray, test_AutoTArray_SwapElements) { + AutoTArray oneArray; + AutoTArray another; + + for (size_t i = 0; i < 8; ++i) { + oneArray.AppendElement(Big()); + } + oneArray[0].size[10] = 1; + for (size_t i = 0; i < 9; ++i) { + another.AppendElement(Big()); + } + oneArray.SwapElements(another); + + ASSERT_EQ(oneArray.Length(), 9u); + ASSERT_EQ(another.Length(), 8u); + + ASSERT_EQ(oneArray[0].size[10], 0u); + ASSERT_EQ(another[0].size[10], 1u); +} + } // namespace TestTArray