From 096f3c3097cb2f60c2f44d4d8c734fc33ffd0a3e Mon Sep 17 00:00:00 2001 From: Ulf Hermann Date: Thu, 16 Nov 2023 16:22:38 +0100 Subject: QmlCompiler: Don't access unavailable input accumulator on 'as' MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We only read it if the target type is composite. Therefore, check for composite first, before looking for the accumulator. Pick-to: 6.6 6.5 6.2 Fixes: QTBUG-119122 Change-Id: I2a50214f76ee4ffe2f877dc690704e7475b8bd77 Reviewed-by: Olivier De Cannière Reviewed-by: Fabian Kosmale --- src/qmlcompiler/qqmljscodegenerator.cpp | 5 ++--- tests/auto/qml/qmlcppcodegen/data/asCast.qml | 10 ++++++++++ tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp | 10 ++++++++++ 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/qmlcompiler/qqmljscodegenerator.cpp b/src/qmlcompiler/qqmljscodegenerator.cpp index f3c57276e1..3d87a709d7 100644 --- a/src/qmlcompiler/qqmljscodegenerator.cpp +++ b/src/qmlcompiler/qqmljscodegenerator.cpp @@ -2861,9 +2861,8 @@ void QQmlJSCodeGenerator::generate_As(int lhs) ? input : convertStored(inputContent.storedType(), genericContained, input); - if (m_typeResolver->equals( - m_state.accumulatorIn().storedType(), m_typeResolver->metaObjectType()) - && contained->isComposite()) { + if (contained->isComposite() && m_typeResolver->equals( + m_state.accumulatorIn().storedType(), m_typeResolver->metaObjectType())) { m_body += conversion( genericContained, outputContent, m_state.accumulatorVariableIn + u"->cast("_s + inputConversion + u')'); diff --git a/tests/auto/qml/qmlcppcodegen/data/asCast.qml b/tests/auto/qml/qmlcppcodegen/data/asCast.qml index 1befc08d0a..cb8155ca6c 100644 --- a/tests/auto/qml/qmlcppcodegen/data/asCast.qml +++ b/tests/auto/qml/qmlcppcodegen/data/asCast.qml @@ -28,4 +28,14 @@ Item { property QtObject dummyAsItem: dummy as Item property QtObject dummyAsRectangle: dummy as Rectangle property QtObject dummyAsDummy: dummy as Dummy + + property QtObject nullAsObject: null as QtObject + property QtObject nullAsItem: null as Item + property QtObject nullAsRectangle: null as Rectangle + property QtObject nullAsDummy: null as Dummy + + property QtObject undefinedAsObject: undefined as QtObject + property QtObject undefinedAsItem: undefined as Item + property QtObject undefinedAsRectangle: undefined as Rectangle + property QtObject undefinedAsDummy: undefined as Dummy } diff --git a/tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp b/tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp index 37f874ba91..5d44b84475 100644 --- a/tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp +++ b/tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp @@ -657,6 +657,16 @@ void tst_QmlCppCodegen::asCast() QCOMPARE(qvariant_cast(root->property("dummyAsItem")), dummy); QCOMPARE(qvariant_cast(root->property("dummyAsRectangle")), nullptr); QCOMPARE(qvariant_cast(root->property("dummyAsDummy")), dummy); + + QCOMPARE(qvariant_cast(root->property("nullAsObject")), nullptr); + QCOMPARE(qvariant_cast(root->property("nullAsItem")), nullptr); + QCOMPARE(qvariant_cast(root->property("nullAsRectangle")), nullptr); + QCOMPARE(qvariant_cast(root->property("nullAsDummy")), nullptr); + + QCOMPARE(qvariant_cast(root->property("undefinedAsObject")), nullptr); + QCOMPARE(qvariant_cast(root->property("undefinedAsItem")), nullptr); + QCOMPARE(qvariant_cast(root->property("undefinedAsRectangle")), nullptr); + QCOMPARE(qvariant_cast(root->property("undefinedAsDummy")), nullptr); } void tst_QmlCppCodegen::attachedBaseEnum() -- cgit v1.2.3