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; }