From 51c6db3f3d9843faa7ab3a1839bce17cd4418bd2 Mon Sep 17 00:00:00 2001 From: Ulf Hermann Date: Thu, 16 Mar 2023 13:35:59 +0100 Subject: [PATCH] QtQml: Check CU for null on isBound() If the component has an error or was clear()'d its CU will be null. Since isBound() is a public method, it should take that into account rather than just crash. Pick-to: 6.5 Change-Id: I4a3e7417da3c87f6ce7dbb615f984815bc2b0b0b Reviewed-by: Fabian Kosmale Reviewed-by: Tim Jenssen --- src/qml/qml/qqmlcomponent_p.h | 3 ++- tests/auto/qml/qqmlcomponent/tst_qqmlcomponent.cpp | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/qml/qml/qqmlcomponent_p.h b/src/qml/qml/qqmlcomponent_p.h index d76e1c24a3..31cbf0a4fc 100644 --- a/src/qml/qml/qqmlcomponent_p.h +++ b/src/qml/qml/qqmlcomponent_p.h @@ -164,7 +164,8 @@ public: QQmlContext *context, CreateBehavior behavior = CreateDefault); bool isBound() const { - return compilationUnit->unitData()->flags & QV4::CompiledData::Unit::ComponentsBound; + return compilationUnit + && (compilationUnit->unitData()->flags & QV4::CompiledData::Unit::ComponentsBound); } }; diff --git a/tests/auto/qml/qqmlcomponent/tst_qqmlcomponent.cpp b/tests/auto/qml/qqmlcomponent/tst_qqmlcomponent.cpp index 5203ba9615..d76768c5d7 100644 --- a/tests/auto/qml/qqmlcomponent/tst_qqmlcomponent.cpp +++ b/tests/auto/qml/qqmlcomponent/tst_qqmlcomponent.cpp @@ -1245,12 +1245,14 @@ void tst_qqmlcomponent::boundComponent() { QQmlComponent component(&engine, testFileUrl("nestedBoundComponent.qml")); QVERIFY2(component.isReady(), qPrintable(component.errorString())); + QVERIFY(component.isBound()); QScopedPointer o(component.create()); QVERIFY(!o.isNull()); QQmlComponent *nestedComponent = o->property("c").value(); QVERIFY(nestedComponent != nullptr); + QVERIFY(nestedComponent->isBound()); QObject *nestedObject = o->property("o").value(); QVERIFY(nestedObject != nullptr); @@ -1269,6 +1271,7 @@ void tst_qqmlcomponent::boundComponent() { QQmlComponent component(&engine, testFileUrl("BoundInlineComponent.qml")); QVERIFY2(component.isReady(), qPrintable(component.errorString())); + QVERIFY(component.isBound()); QScopedPointer o(component.create()); QVERIFY2(!o.isNull(), qPrintable(component.errorString())); @@ -1282,11 +1285,22 @@ void tst_qqmlcomponent::boundComponent() { QQmlComponent component(&engine, testFileUrl("boundInlineComponentUser.qml")); QVERIFY2(component.isReady(), qPrintable(component.errorString())); + QVERIFY(!component.isBound()); QScopedPointer o(component.create()); QVERIFY(o.isNull()); QVERIFY(component.errorString().contains( QLatin1String("Cannot instantiate bound inline component in different file"))); + + } + + { + QQmlComponent component(&engine); + QVERIFY(!component.isBound()); + + component.setData("pragma ComponentBehavior: Bound\nsyntax error", QUrl()); + QCOMPARE(component.errorString(), ":2 Syntax error\n"_L1); + QVERIFY(!component.isBound()); } }