Fix UB in tst_QMetaType

Don't pass around meta-type IDs in QMetaType::Type
variables. It leads to reading values from an enum
variable that are invalid.

Fix by passing the IDs around as int.

Found by UBSan:
  tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp:408:5: runtime error: load of value 4028, which is not a valid value for type 'Type'

Change-Id: Idd106ee3d7960fe3d8fefc0fc5830fc22d38a513
Reviewed-by: Olivier Goffart (Woboq GmbH) <ogoffart@woboq.com>
bb10
Marc Mutz 2016-01-06 16:56:26 +01:00
parent 033205bb59
commit bccbb70de5
1 changed files with 43 additions and 43 deletions

View File

@ -369,40 +369,40 @@ void tst_QMetaType::normalizedTypes()
}
#define TYPENAME_DATA(MetaTypeName, MetaTypeId, RealType)\
QTest::newRow(#RealType) << QMetaType::MetaTypeName << #RealType;
QTest::newRow(#RealType) << int(QMetaType::MetaTypeName) << #RealType;
void tst_QMetaType::typeName_data()
{
QTest::addColumn<QMetaType::Type>("aType");
QTest::addColumn<int>("aType");
QTest::addColumn<QString>("aTypeName");
QT_FOR_EACH_STATIC_TYPE(TYPENAME_DATA)
QTest::newRow("QMetaType::UnknownType") << QMetaType::UnknownType << static_cast<const char*>(0);
QTest::newRow("QMetaType::UnknownType") << int(QMetaType::UnknownType) << static_cast<const char*>(0);
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("Whity<double>") << ::qMetaTypeId<Whity<double> >() << QString::fromLatin1("Whity<double>");
QTest::newRow("Whity<int>") << ::qMetaTypeId<Whity<int> >() << QString::fromLatin1("Whity<int>");
QTest::newRow("Testspace::Foo") << ::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();
QTest::newRow("-1") << -1 << QString();
QTest::newRow("-124125534") << -124125534 << QString();
QTest::newRow("124125534") << 124125534 << QString();
// automatic registration
QTest::newRow("QList<int>") << static_cast<QMetaType::Type>(::qMetaTypeId<QList<int> >()) << QString::fromLatin1("QList<int>");
QTest::newRow("QHash<int,int>") << static_cast<QMetaType::Type>(::qMetaTypeId<QHash<int, int> >()) << QString::fromLatin1("QHash<int,int>");
QTest::newRow("QMap<int,int>") << static_cast<QMetaType::Type>(::qMetaTypeId<QMap<int, int> >()) << QString::fromLatin1("QMap<int,int>");
QTest::newRow("QVector<QList<int>>") << static_cast<QMetaType::Type>(::qMetaTypeId<QVector<QList<int> > >()) << QString::fromLatin1("QVector<QList<int> >");
QTest::newRow("QVector<QMap<int,int>>") << static_cast<QMetaType::Type>(::qMetaTypeId<QVector<QMap<int, int> > >()) << QString::fromLatin1("QVector<QMap<int,int> >");
QTest::newRow("QList<int>") << ::qMetaTypeId<QList<int> >() << QString::fromLatin1("QList<int>");
QTest::newRow("QHash<int,int>") << ::qMetaTypeId<QHash<int, int> >() << QString::fromLatin1("QHash<int,int>");
QTest::newRow("QMap<int,int>") << ::qMetaTypeId<QMap<int, int> >() << QString::fromLatin1("QMap<int,int>");
QTest::newRow("QVector<QList<int>>") << ::qMetaTypeId<QVector<QList<int> > >() << QString::fromLatin1("QVector<QList<int> >");
QTest::newRow("QVector<QMap<int,int>>") << ::qMetaTypeId<QVector<QMap<int, int> > >() << QString::fromLatin1("QVector<QMap<int,int> >");
QTest::newRow("CustomQObject*") << static_cast<QMetaType::Type>(::qMetaTypeId<CustomQObject*>()) << QString::fromLatin1("CustomQObject*");
QTest::newRow("CustomGadget") << static_cast<QMetaType::Type>(::qMetaTypeId<CustomGadget>()) << QString::fromLatin1("CustomGadget");
QTest::newRow("CustomQObject::CustomQEnum") << static_cast<QMetaType::Type>(::qMetaTypeId<CustomQObject::CustomQEnum>()) << QString::fromLatin1("CustomQObject::CustomQEnum");
QTest::newRow("Qt::ArrowType") << static_cast<QMetaType::Type>(::qMetaTypeId<Qt::ArrowType>()) << QString::fromLatin1("Qt::ArrowType");
QTest::newRow("CustomQObject*") << ::qMetaTypeId<CustomQObject*>() << QString::fromLatin1("CustomQObject*");
QTest::newRow("CustomGadget") << ::qMetaTypeId<CustomGadget>() << QString::fromLatin1("CustomGadget");
QTest::newRow("CustomQObject::CustomQEnum") << ::qMetaTypeId<CustomQObject::CustomQEnum>() << QString::fromLatin1("CustomQObject::CustomQEnum");
QTest::newRow("Qt::ArrowType") << ::qMetaTypeId<Qt::ArrowType>() << QString::fromLatin1("Qt::ArrowType");
}
void tst_QMetaType::typeName()
{
QFETCH(QMetaType::Type, aType);
QFETCH(int, aType);
QFETCH(QString, aTypeName);
QString name = QString::fromLatin1(QMetaType::typeName(aType));
@ -413,15 +413,15 @@ void tst_QMetaType::typeName()
void tst_QMetaType::type_data()
{
QTest::addColumn<QMetaType::Type>("aType");
QTest::addColumn<int>("aType");
QTest::addColumn<QByteArray>("aTypeName");
#define TST_QMETATYPE_TYPE_DATA(MetaTypeName, MetaTypeId, RealType)\
QTest::newRow(#RealType) << QMetaType::MetaTypeName << QByteArray( #RealType );
QTest::newRow(#RealType) << int(QMetaType::MetaTypeName) << QByteArray( #RealType );
#define TST_QMETATYPE_TYPE_DATA_ALIAS(MetaTypeName, MetaTypeId, AliasType, RealTypeString)\
QTest::newRow(RealTypeString) << QMetaType::MetaTypeName << QByteArray( #AliasType );
QTest::newRow(RealTypeString) << int(QMetaType::MetaTypeName) << QByteArray( #AliasType );
QTest::newRow("empty") << QMetaType::UnknownType << QByteArray();
QTest::newRow("empty") << int(QMetaType::UnknownType) << QByteArray();
QT_FOR_EACH_STATIC_TYPE(TST_QMETATYPE_TYPE_DATA)
QT_FOR_EACH_STATIC_ALIAS_TYPE(TST_QMETATYPE_TYPE_DATA_ALIAS)
@ -432,13 +432,13 @@ void tst_QMetaType::type_data()
void tst_QMetaType::type()
{
QFETCH(QMetaType::Type, aType);
QFETCH(int, aType);
QFETCH(QByteArray, aTypeName);
// QMetaType::type(QByteArray)
QCOMPARE(QMetaType::type(aTypeName), int(aType));
QCOMPARE(QMetaType::type(aTypeName), aType);
// QMetaType::type(const char *)
QCOMPARE(QMetaType::type(aTypeName.constData()), int(aType));
QCOMPARE(QMetaType::type(aTypeName.constData()), aType);
}
void tst_QMetaType::type_fromSubString_data()
@ -727,9 +727,9 @@ template<> struct TestValueFactory<QMetaType::QVariant> {
void tst_QMetaType::create_data()
{
QTest::addColumn<QMetaType::Type>("type");
QTest::addColumn<int>("type");
#define ADD_METATYPE_TEST_ROW(MetaTypeName, MetaTypeId, RealType) \
QTest::newRow(QMetaType::typeName(QMetaType::MetaTypeName)) << QMetaType::MetaTypeName;
QTest::newRow(QMetaType::typeName(QMetaType::MetaTypeName)) << int(QMetaType::MetaTypeName);
FOR_EACH_CORE_METATYPE(ADD_METATYPE_TEST_ROW)
#undef ADD_METATYPE_TEST_ROW
}
@ -781,7 +781,7 @@ FOR_EACH_CORE_METATYPE(RETURN_CREATE_FUNCTION)
}
};
QFETCH(QMetaType::Type, type);
QFETCH(int, type);
TypeTestFunctionGetter::get(type)();
}
@ -832,33 +832,33 @@ FOR_EACH_CORE_METATYPE(RETURN_CREATE_COPY_FUNCTION)
}
};
QFETCH(QMetaType::Type, type);
QFETCH(int, type);
TypeTestFunctionGetter::get(type)();
}
void tst_QMetaType::sizeOf_data()
{
QTest::addColumn<QMetaType::Type>("type");
QTest::addColumn<int>("type");
QTest::addColumn<size_t>("size");
QTest::newRow("QMetaType::UnknownType") << QMetaType::UnknownType << size_t(0);
QTest::newRow("QMetaType::UnknownType") << int(QMetaType::UnknownType) << size_t(0);
#define ADD_METATYPE_TEST_ROW(MetaTypeName, MetaTypeId, RealType) \
QTest::newRow(#RealType) << QMetaType::MetaTypeName << size_t(QTypeInfo<RealType>::sizeOf);
QTest::newRow(#RealType) << int(QMetaType::MetaTypeName) << size_t(QTypeInfo<RealType>::sizeOf);
FOR_EACH_CORE_METATYPE(ADD_METATYPE_TEST_ROW)
#undef ADD_METATYPE_TEST_ROW
QTest::newRow("Whity<double>") << static_cast<QMetaType::Type>(::qMetaTypeId<Whity<double> >()) << sizeof(Whity<double>);
QTest::newRow("Whity<int>") << static_cast<QMetaType::Type>(::qMetaTypeId<Whity<int> >()) << sizeof(Whity<int>);
QTest::newRow("Testspace::Foo") << static_cast<QMetaType::Type>(::qMetaTypeId<TestSpace::Foo>()) << sizeof(TestSpace::Foo);
QTest::newRow("Whity<double>") << ::qMetaTypeId<Whity<double> >() << sizeof(Whity<double>);
QTest::newRow("Whity<int>") << ::qMetaTypeId<Whity<int> >() << sizeof(Whity<int>);
QTest::newRow("Testspace::Foo") << ::qMetaTypeId<TestSpace::Foo>() << sizeof(TestSpace::Foo);
QTest::newRow("-1") << QMetaType::Type(-1) << size_t(0);
QTest::newRow("-124125534") << QMetaType::Type(-124125534) << size_t(0);
QTest::newRow("124125534") << QMetaType::Type(124125534) << size_t(0);
QTest::newRow("-1") << -1 << size_t(0);
QTest::newRow("-124125534") << -124125534 << size_t(0);
QTest::newRow("124125534") << 124125534 << size_t(0);
}
void tst_QMetaType::sizeOf()
{
QFETCH(QMetaType::Type, type);
QFETCH(int, type);
QFETCH(size_t, size);
QCOMPARE(size_t(QMetaType::sizeOf(type)), size);
}
@ -870,7 +870,7 @@ void tst_QMetaType::sizeOfStaticLess_data()
void tst_QMetaType::sizeOfStaticLess()
{
QFETCH(QMetaType::Type, type);
QFETCH(int, type);
QFETCH(size_t, size);
QCOMPARE(size_t(QMetaType(type).sizeOf()), size);
}
@ -1126,7 +1126,7 @@ FOR_EACH_CORE_METATYPE(RETURN_CONSTRUCT_FUNCTION)
}
};
QFETCH(QMetaType::Type, type);
QFETCH(int, type);
TypeTestFunctionGetter::get(type)();
}
@ -1194,7 +1194,7 @@ FOR_EACH_CORE_METATYPE(RETURN_CONSTRUCT_COPY_FUNCTION)
}
};
QFETCH(QMetaType::Type, type);
QFETCH(int, type);
TypeTestFunctionGetter::get(type)();
}