diff options
author | Fabian Kosmale <fabian.kosmale@qt.io> | 2019-07-03 08:37:16 +0200 |
---|---|---|
committer | Fabian Kosmale <fabian.kosmale@qt.io> | 2019-07-03 09:42:12 +0200 |
commit | d73497f136fbfad5d367e5af429adb7d38af6dfe (patch) | |
tree | e808df1ba96b836e27d7ea8831da21a24bb4d712 | |
parent | 6dffa5400fd056e3862a2f8905362679c1bb6be7 (diff) |
QQmlComponent: Check for existence of engine
Avoid a crash when setData or create are
called after a user mistakenly used the internal constructor of
QQmlComponent which does not take an engine.
Fixes: QTBUG-55407
Change-Id: Ia4295d1b044723efce1a95607349561d4f1640da
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
-rw-r--r-- | src/qml/qml/qqmlcomponent.cpp | 12 | ||||
-rw-r--r-- | tests/auto/qml/qqmlcomponent/tst_qqmlcomponent.cpp | 12 |
2 files changed, 24 insertions, 0 deletions
diff --git a/src/qml/qml/qqmlcomponent.cpp b/src/qml/qml/qqmlcomponent.cpp index a23a322df9..7ad9310160 100644 --- a/src/qml/qml/qqmlcomponent.cpp +++ b/src/qml/qml/qqmlcomponent.cpp @@ -571,6 +571,12 @@ void QQmlComponent::setData(const QByteArray &data, const QUrl &url) { Q_D(QQmlComponent); + if (!d->engine) { + // ###Qt6: In Qt 6, it should be impossible for users to create a QQmlComponent without an engine, and we can remove this check + qWarning("QQmlComponent: Must provide an engine before calling setData"); + return; + } + d->clear(); d->url = url; @@ -777,6 +783,12 @@ QObject *QQmlComponent::create(QQmlContext *context) Q_D(QQmlComponent); QML_MEMORY_SCOPE_URL(url()); + if (!d->engine) { + // ###Qt6: In Qt 6, it should be impossible for users to create a QQmlComponent without an engine, and we can remove this check + qWarning("QQmlComponent: Must provide an engine before calling create"); + return nullptr; + } + if (!context) context = d->engine->rootContext(); diff --git a/tests/auto/qml/qqmlcomponent/tst_qqmlcomponent.cpp b/tests/auto/qml/qqmlcomponent/tst_qqmlcomponent.cpp index 7c7c7d3bd0..a872cece96 100644 --- a/tests/auto/qml/qqmlcomponent/tst_qqmlcomponent.cpp +++ b/tests/auto/qml/qqmlcomponent/tst_qqmlcomponent.cpp @@ -121,6 +121,7 @@ private slots: void setNonExistentInitialProperty(); void relativeUrl_data(); void relativeUrl(); + void setDataNoEngineNoSegfault(); private: QQmlEngine engine; @@ -654,6 +655,17 @@ void tst_qqmlcomponent::relativeUrl() QVERIFY2(!component.isError(), qPrintable(component.errorString())); } +void tst_qqmlcomponent::setDataNoEngineNoSegfault() +{ + QQmlEngine eng; + QQmlComponent comp; + QTest::ignoreMessage(QtWarningMsg, "QQmlComponent: Must provide an engine before calling setData"); + comp.setData("import QtQuick 1.0; QtObject { }", QUrl("")); + QTest::ignoreMessage(QtWarningMsg, "QQmlComponent: Must provide an engine before calling create"); + auto c = comp.create(); + QVERIFY(!c); +} + QTEST_MAIN(tst_qqmlcomponent) #include "tst_qqmlcomponent.moc" |