From 7e9343c697a426d2b31aec6b300e35d430e1d2dc Mon Sep 17 00:00:00 2001 From: Felix Geyer Date: Tue, 17 Jul 2012 10:47:56 +0200 Subject: [PATCH] Fix CompositKey self assignment. --- src/keys/CompositeKey.cpp | 5 +++++ tests/TestKeys.cpp | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/src/keys/CompositeKey.cpp b/src/keys/CompositeKey.cpp index afd8f0704..d3c807bf3 100644 --- a/src/keys/CompositeKey.cpp +++ b/src/keys/CompositeKey.cpp @@ -51,6 +51,11 @@ CompositeKey* CompositeKey::clone() const CompositeKey& CompositeKey::operator=(const CompositeKey& key) { + // handle self assignment as that would break when calling clear() + if (this == &key) { + return *this; + } + clear(); Q_FOREACH (const Key* subKey, key.m_keys) { diff --git a/tests/TestKeys.cpp b/tests/TestKeys.cpp index 38bb0707d..51d09495e 100644 --- a/tests/TestKeys.cpp +++ b/tests/TestKeys.cpp @@ -70,6 +70,10 @@ void TestKeys::testComposite() *compositeKey4 = *compositeKey3; QCOMPARE(compositeKey3->rawKey(), compositeKey4->rawKey()); + // test self-assignment + *compositeKey3 = *compositeKey3; + QCOMPARE(compositeKey3->rawKey(), compositeKey4->rawKey()); + delete compositeKey3; delete compositeKey4; }