Fix crash in QVariant::canConvert.
authorJędrzej Nowacki <jedrzej.nowacki@nokia.com>
Tue, 6 Mar 2012 12:34:30 +0000 (13:34 +0100)
committerQt by Nokia <qt-info@nokia.com>
Thu, 8 Mar 2012 15:03:52 +0000 (16:03 +0100)
The function was crashing when an unsupported type id was given
as an input argument.

Change-Id: I2b0e3e6d43f6f248dc71532f8e6485efe68e8120
Reviewed-by: Stephen Kelly <stephen.kelly@kdab.com>
src/corelib/kernel/qvariant.cpp
tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp

index 029a261..c845619 100644 (file)
@@ -2445,6 +2445,8 @@ bool QVariant::canConvert(int targetTypeId) const
 
     if (currentType == uint(targetTypeId))
         return true;
+    if (targetTypeId < 0 || targetTypeId >= QMetaType::User)
+        return false;
 
     // FIXME It should be LastCoreType intead of Uuid
     if (currentType > int(QMetaType::QUuid) || targetTypeId > int(QMetaType::QUuid)) {
index ccdab17..d78759e 100644 (file)
@@ -612,6 +612,12 @@ void tst_QVariant::canConvert()
     QCOMPARE(val.canConvert(QVariant::Time), TimeCast);
     QCOMPARE(val.canConvert(QVariant::UInt), UIntCast);
     QCOMPARE(val.canConvert(QVariant::ULongLong), ULongLongCast);
+
+    // Invalid type ids
+    QCOMPARE(val.canConvert(-1), false);
+    QCOMPARE(val.canConvert(-23), false);
+    QCOMPARE(val.canConvert(-23876), false);
+    QCOMPARE(val.canConvert(23876), false);
 }
 
 void tst_QVariant::toInt_data()