Crash fix in QMetaType::typeName.

The function is public, so it should validate input instead of crashing

Change-Id: Ifd9f1110f8631f942929d85db6a57eee7afffb6a
Reviewed-by: Stephen Kelly <stephen.kelly@kdab.com>
This commit is contained in:
Jędrzej Nowacki 2012-03-08 15:36:38 +01:00 committed by Qt by Nokia
parent f71487da3a
commit cf6dd5baca
2 changed files with 11 additions and 6 deletions

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

View 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()