Crash fix in QMetaType::typeName.
authorJędrzej Nowacki <jedrzej.nowacki@nokia.com>
Thu, 8 Mar 2012 14:36:38 +0000 (15:36 +0100)
committerQt by Nokia <qt-info@nokia.com>
Thu, 8 Mar 2012 15:04:02 +0000 (16:04 +0100)
The function is public, so it should validate input instead of crashing

Change-Id: Ifd9f1110f8631f942929d85db6a57eee7afffb6a
Reviewed-by: Stephen Kelly <stephen.kelly@kdab.com>
src/corelib/kernel/qmetatype.cpp
tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp

index 410a5cc..0be813f 100644 (file)
@@ -377,8 +377,9 @@ void QMetaType::registerStreamOperators(int idx, SaveOperator saveOp,
 
     \sa type(), isRegistered(), Type
 */
-const char *QMetaType::typeName(int type)
+const char *QMetaType::typeName(int typeId)
 {
+    const uint type = typeId;
     // In theory it can be filled during compilation time, but for some reason template code
     // that is able to do it causes GCC 4.6 to generate additional 3K of executable code. Probably
     // it is not worth of it.
@@ -400,7 +401,7 @@ const char *QMetaType::typeName(int type)
         } else {
             const QVector<QCustomTypeInfo> * const ct = customTypes();
             QReadLocker locker(customTypesLock());
-            return ct && ct->count() > type - QMetaType::User && !ct->at(type - QMetaType::User).typeName.isEmpty()
+            return ct && uint(ct->count()) > type - QMetaType::User && !ct->at(type - QMetaType::User).typeName.isEmpty()
                     ? ct->at(type - QMetaType::User).typeName.constData()
                     : 0;
         }
index f8403f1..3543988 100644 (file)
@@ -305,16 +305,20 @@ void tst_QMetaType::normalizedTypes()
 #define TYPENAME_DATA(MetaTypeName, MetaTypeId, RealType)\
     QTest::newRow(#RealType) << QMetaType::MetaTypeName << #RealType;
 
-#define TYPENAME_DATA_ALIAS(MetaTypeName, MetaTypeId, AliasType, RealTypeString)\
-    QTest::newRow(RealTypeString) << QMetaType::MetaTypeName << #AliasType;
-
 void tst_QMetaType::typeName_data()
 {
     QTest::addColumn<QMetaType::Type>("aType");
     QTest::addColumn<QString>("aTypeName");
 
     QT_FOR_EACH_STATIC_TYPE(TYPENAME_DATA)
-    QT_FOR_EACH_STATIC_ALIAS_TYPE(TYPENAME_DATA_ALIAS)
+
+    QTest::newRow("Whity<double>") << static_cast<QMetaType::Type>(::qMetaTypeId<Whity<double> >()) << QString::fromLatin1("Whity<double>");
+    QTest::newRow("Whity<int>") << static_cast<QMetaType::Type>(::qMetaTypeId<Whity<int> >()) << QString::fromLatin1("Whity<int>");
+    QTest::newRow("Testspace::Foo") << static_cast<QMetaType::Type>(::qMetaTypeId<TestSpace::Foo>()) << QString::fromLatin1("TestSpace::Foo");
+
+    QTest::newRow("-1") << QMetaType::Type(-1) << QString();
+    QTest::newRow("-124125534") << QMetaType::Type(-124125534) << QString();
+    QTest::newRow("124125534") << QMetaType::Type(124125534) << QString();
 }
 
 void tst_QMetaType::typeName()