From eacbc7805e937e64b7e117705919b214aed4f736 Mon Sep 17 00:00:00 2001 From: Chris Adams Date: Tue, 12 Jun 2012 17:47:15 +1000 Subject: [PATCH] Check for null ptr in qmlobject_cast definition Previously, the input object wasn't checked for nullness, and thus the qmlobject_cast could assert. Change-Id: I3552150953cef8411a860a381e28b1d681494b08 Reviewed-by: Kent Hansen Reviewed-by: Aaron Kennedy --- src/qml/qml/qqmlglobal_p.h | 2 +- tests/auto/qml/qqmlcpputils/tst_qqmlcpputils.cpp | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/qml/qml/qqmlglobal_p.h b/src/qml/qml/qqmlglobal_p.h index b52e55c..c237af6 100644 --- a/src/qml/qml/qqmlglobal_p.h +++ b/src/qml/qml/qqmlglobal_p.h @@ -158,7 +158,7 @@ QT_BEGIN_NAMESPACE template T qmlobject_cast(QObject *object) { - if (QQmlMetaObject::canConvert(object, &reinterpret_cast(object)->staticMetaObject)) + if (object && QQmlMetaObject::canConvert(object, &reinterpret_cast(object)->staticMetaObject)) return static_cast(object); else return 0; diff --git a/tests/auto/qml/qqmlcpputils/tst_qqmlcpputils.cpp b/tests/auto/qml/qqmlcpputils/tst_qqmlcpputils.cpp index 4189f44..40c93fe 100644 --- a/tests/auto/qml/qqmlcpputils/tst_qqmlcpputils.cpp +++ b/tests/auto/qml/qqmlcpputils/tst_qqmlcpputils.cpp @@ -51,6 +51,7 @@ public: private slots: void fastConnect(); + void fastCast(); }; class MyObject : public QObject { @@ -101,6 +102,25 @@ void tst_qqmlcpputils::fastConnect() } } +void tst_qqmlcpputils::fastCast() +{ + { + QObject *myObj = new MyObject; + MyObject *obj = qmlobject_cast(myObj); + QVERIFY(obj); + QCOMPARE(obj->metaObject(), myObj->metaObject()); + obj->slot1(); + QCOMPARE(obj->slotCount, 1); + delete myObj; + } + + { + QObject *nullObj = 0; + QObject *obj = qmlobject_cast(nullObj); + QCOMPARE(obj, nullObj); // shouldn't crash/assert. + } +} + QTEST_MAIN(tst_qqmlcpputils) #include "tst_qqmlcpputils.moc" -- 2.7.4