Fix re-initializing a moved-from QProtobufMessage using copy

It was expecting the data-pointer was not nullptr, but after moving out
it is.

Fixes: QTBUG-119227
Change-Id: I3a8907dd0e16b33604481d9d6c382c238b067676
Reviewed-by: Alexey Edelev <alexey.edelev@qt.io>
(cherry picked from commit 0bc538fa9a)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Mårten Nordheim 2023-11-17 15:32:14 +01:00 committed by Qt Cherry-pick Bot
parent df16712156
commit 0ce7f59cc1
2 changed files with 31 additions and 1 deletions

View File

@ -148,7 +148,11 @@ QProtobufMessage::QProtobufMessage(const QProtobufMessage &other)
*/ */
QProtobufMessage &QProtobufMessage::operator=(const QProtobufMessage &other) QProtobufMessage &QProtobufMessage::operator=(const QProtobufMessage &other)
{ {
if (this != &other) if (!other.d_ptr)
delete std::exchange(d_ptr, {}); // delete d_ptr if other.d_ptr is null
else if (!d_ptr)
d_ptr = new QProtobufMessagePrivate(*other.d_ptr);
else if (this != &other)
*d_ptr = *other.d_ptr; *d_ptr = *other.d_ptr;
return *this; return *this;
} }

View File

@ -62,6 +62,18 @@ void QtProtobufTypesGenerationTest::EmptyMessageTest()
QProtobufMessagePointer rawMessage( QProtobufMessagePointer rawMessage(
QProtobufMessage::constructByName("qtprotobufnamespace.tests.EmptyMessage")); QProtobufMessage::constructByName("qtprotobufnamespace.tests.EmptyMessage"));
QVERIFY(reinterpret_cast<qtprotobufnamespace::tests::EmptyMessage*>(rawMessage.get()) != nullptr); QVERIFY(reinterpret_cast<qtprotobufnamespace::tests::EmptyMessage*>(rawMessage.get()) != nullptr);
// Move from and reuse. This should compile and run:
qtprotobufnamespace::tests::EmptyMessage from;
qtprotobufnamespace::tests::EmptyMessage to = std::move(from);
from = to;
QCOMPARE(from, to);
qtprotobufnamespace::tests::EmptyMessage bucket = std::move(to);
bucket = std::move(from);
from = to;
QCOMPARE(from, to);
} }
void QtProtobufTypesGenerationTest::BoolMessageTest() void QtProtobufTypesGenerationTest::BoolMessageTest()
@ -77,6 +89,20 @@ void QtProtobufTypesGenerationTest::BoolMessageTest()
QCOMPARE(SimpleBoolMessage::TestFieldBoolProtoFieldNumber, 1); QCOMPARE(SimpleBoolMessage::TestFieldBoolProtoFieldNumber, 1);
QCOMPARE(test.propertyOrdering.getMessageFullName(), QCOMPARE(test.propertyOrdering.getMessageFullName(),
"qtprotobufnamespace.tests.SimpleBoolMessage"); "qtprotobufnamespace.tests.SimpleBoolMessage");
// Move from and reuse
qtprotobufnamespace::tests::SimpleBoolMessage from;
qtprotobufnamespace::tests::SimpleBoolMessage to = std::move(from);
from = to;
QCOMPARE(from.testFieldBool(), to.testFieldBool());
// Changes in one should not be visible in the other:
to.setTestFieldBool(!to.testFieldBool());
QCOMPARE_NE(from.testFieldBool(), to.testFieldBool());
from = to;
to.setProperty(propertyName, QVariant::fromValue(!to.testFieldBool()));
QCOMPARE_NE(from.testFieldBool(), to.testFieldBool());
} }
void QtProtobufTypesGenerationTest::IntMessageTest() void QtProtobufTypesGenerationTest::IntMessageTest()