Check for null ptr in qmlobject_cast definition
authorChris Adams <christopher.adams@nokia.com>
Tue, 12 Jun 2012 07:47:15 +0000 (17:47 +1000)
committerQt by Nokia <qt-info@nokia.com>
Tue, 12 Jun 2012 12:10:24 +0000 (14:10 +0200)
Previously, the input object wasn't checked for nullness, and thus
the qmlobject_cast could assert.

Change-Id: I3552150953cef8411a860a381e28b1d681494b08
Reviewed-by: Kent Hansen <kent.hansen@nokia.com>
Reviewed-by: Aaron Kennedy <aaron.kennedy@nokia.com>
src/qml/qml/qqmlglobal_p.h
tests/auto/qml/qqmlcpputils/tst_qqmlcpputils.cpp

index b52e55c..c237af6 100644 (file)
@@ -158,7 +158,7 @@ QT_BEGIN_NAMESPACE
 template<class T>
 T qmlobject_cast(QObject *object)
 {
-    if (QQmlMetaObject::canConvert(object, &reinterpret_cast<T>(object)->staticMetaObject))
+    if (object && QQmlMetaObject::canConvert(object, &reinterpret_cast<T>(object)->staticMetaObject))
         return static_cast<T>(object);
     else
         return 0;
index 4189f44..40c93fe 100644 (file)
@@ -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<MyObject*>(myObj);
+        QVERIFY(obj);
+        QCOMPARE(obj->metaObject(), myObj->metaObject());
+        obj->slot1();
+        QCOMPARE(obj->slotCount, 1);
+        delete myObj;
+    }
+
+    {
+        QObject *nullObj = 0;
+        QObject *obj = qmlobject_cast<QObject *>(nullObj);
+        QCOMPARE(obj, nullObj); // shouldn't crash/assert.
+    }
+}
+
 QTEST_MAIN(tst_qqmlcpputils)
 
 #include "tst_qqmlcpputils.moc"