diff options
author | Greg Daniel <egdaniel@google.com> | 2017-03-22 13:33:21 -0400 |
---|---|---|
committer | Skia Commit-Bot <skia-commit-bot@chromium.org> | 2017-03-22 18:33:19 +0000 |
commit | 70131b97f97f97d6fbcf4545b97827dfe8509654 (patch) | |
tree | 73da517a55a7b3121dfab19de799869fec82d111 | |
parent | dbd11ec106e3726b09bf5d240c9fcbf6a671a570 (diff) |
Fix SkTArray operator= to work with self assignment
BUG=skia:
Change-Id: I2a403a7ccbb87a030757f3e57d2ea53503f72512
Reviewed-on: https://skia-review.googlesource.com/10012
Reviewed-by: Brian Salomon <bsalomon@google.com>
Commit-Queue: Greg Daniel <egdaniel@google.com>
-rw-r--r-- | include/private/SkTArray.h | 6 | ||||
-rw-r--r-- | tests/TArrayTest.cpp | 15 |
2 files changed, 21 insertions, 0 deletions
diff --git a/include/private/SkTArray.h b/include/private/SkTArray.h index fdf96c87bc..4b700d2d29 100644 --- a/include/private/SkTArray.h +++ b/include/private/SkTArray.h @@ -59,6 +59,9 @@ public: } SkTArray& operator=(const SkTArray& that) { + if (this == &that) { + return *this; + } for (int i = 0; i < fCount; ++i) { fItemArray[i].~T(); } @@ -69,6 +72,9 @@ public: return *this; } SkTArray& operator=(SkTArray&& that) { + if (this == &that) { + return *this; + } for (int i = 0; i < fCount; ++i) { fItemArray[i].~T(); } diff --git a/tests/TArrayTest.cpp b/tests/TArrayTest.cpp index 6e23c49b35..0e18e9f96d 100644 --- a/tests/TArrayTest.cpp +++ b/tests/TArrayTest.cpp @@ -281,6 +281,19 @@ void test_unnecessary_alloc(skiatest::Reporter* reporter) { } } +static void test_self_assignment(skiatest::Reporter* reporter) { + SkTArray<int> a; + a.push_back(1); + REPORTER_ASSERT(reporter, !a.empty()); + REPORTER_ASSERT(reporter, a.count() == 1); + REPORTER_ASSERT(reporter, a[0] == 1); + + a = a; + REPORTER_ASSERT(reporter, !a.empty()); + REPORTER_ASSERT(reporter, a.count() == 1); + REPORTER_ASSERT(reporter, a[0] == 1); +} + DEF_TEST(TArray, reporter) { TestTSet_basic<true>(reporter); TestTSet_basic<false>(reporter); @@ -296,4 +309,6 @@ DEF_TEST(TArray, reporter) { test_move(reporter); test_unnecessary_alloc(reporter); + + test_self_assignment(reporter); } |