From: Chris Adams Date: Tue, 12 Jun 2012 07:47:15 +0000 (+1000) Subject: Check for null ptr in qmlobject_cast definition X-Git-Tag: 071012131707~171 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=eacbc7805e937e64b7e117705919b214aed4f736;p=profile%2Fivi%2Fqtdeclarative.git 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 --- 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"