From 56fbd7644b6f292dcd7d8d215bd469d94a0948ae Mon Sep 17 00:00:00 2001 From: Thierry Bastian Date: Fri, 26 Jun 2009 12:01:38 +0200 Subject: QVariant::setValue had a bug when changing its type MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If it was shared and you then set it back to an int type, it is still marked as shared,. This might even lead to crashes. The patch was sent through gitorious by Jürgen Starek. Note: autotest added as well --- src/corelib/kernel/qvariant.h | 2 +- tests/auto/qvariant/tst_qvariant.cpp | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/corelib/kernel/qvariant.h b/src/corelib/kernel/qvariant.h index 6844d3e..e923844 100644 --- a/src/corelib/kernel/qvariant.h +++ b/src/corelib/kernel/qvariant.h @@ -448,7 +448,7 @@ inline void qVariantSetValue(QVariant &v, const T &t) //if possible we reuse the current QVariant private const int type = qMetaTypeId(reinterpret_cast(0)); QVariant::Private &d = v.data_ptr(); - if (type <= int(QVariant::Char) || (type == d.type && v.isDetached())) { + if (v.isDetached() && (type <= int(QVariant::Char) || type == d.type)) { d.type = type; T *old = reinterpret_cast(d.is_shared ? d.data.shared->ptr : &d.data.ptr); if (QTypeInfo::isComplex) diff --git a/tests/auto/qvariant/tst_qvariant.cpp b/tests/auto/qvariant/tst_qvariant.cpp index 0310276..63e47ab 100644 --- a/tests/auto/qvariant/tst_qvariant.cpp +++ b/tests/auto/qvariant/tst_qvariant.cpp @@ -255,6 +255,7 @@ private slots: void convertByteArrayToBool() const; void convertByteArrayToBool_data() const; void toIntFromQString() const; + void task256984_setValue(); }; Q_DECLARE_METATYPE(QDate) @@ -2967,5 +2968,21 @@ void tst_QVariant::toIntFromQString() const QVERIFY(ok); } +void tst_QVariant::task256984_setValue() +{ + QTransform t; //we just take a value so that we're sure that it will be shared + QVariant v1 = t; + QVERIFY( v1.isDetached() ); + QVariant v2 = v1; + QVERIFY( !v1.isDetached() ); + QVERIFY( !v2.isDetached() ); + + qVariantSetValue(v2, 3); //set an integer value + + QVERIFY( v1.isDetached() ); + QVERIFY( v2.isDetached() ); +} + + QTEST_MAIN(tst_QVariant) #include "tst_qvariant.moc" -- cgit v0.12