aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorUlf Hermann <ulf.hermann@qt.io>2024-12-19 15:44:48 +0100
committerUlf Hermann <ulf.hermann@qt.io>2025-01-09 15:05:00 +0100
commit37f3331c7626f2cb5db8a4db2002d9ccaac16a3d (patch)
treeb443f7c0fb00217cc97cbeeedddcbac47b7ed640
parent49ee9d190383fd4a0e11b13fb833da180309247a (diff)
QtQml: Do not crash when loading .js files as QML
Pick-to: 6.5 Fixes: QTBUG-132118 Change-Id: Id5f680b3dfe1118c3af7cf32d1da7492119a4e51 Reviewed-by: Sami Shalayel <sami.shalayel@qt.io> (cherry picked from commit 78ccc84dde92d787a08b7cab6486527e9e57f183) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> (cherry picked from commit 8454f7d2ddb04c5d0253117fe68be933831afb8d)
-rw-r--r--src/qml/qml/qqmltypedata.cpp5
-rw-r--r--tests/auto/qml/qmlcppcodegen/data/Confused/CMakeLists.txt6
-rw-r--r--tests/auto/qml/qmlcppcodegen/data/Confused/Main2.qml6
-rw-r--r--tests/auto/qml/qmlcppcodegen/data/Confused/Test/broken.js3
-rw-r--r--tests/auto/qml/qmlcppcodegen/data/Confused/Test/qmldir1
-rw-r--r--tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp12
6 files changed, 33 insertions, 0 deletions
diff --git a/src/qml/qml/qqmltypedata.cpp b/src/qml/qml/qqmltypedata.cpp
index 6d1596a0d8..96af4b8491 100644
--- a/src/qml/qml/qqmltypedata.cpp
+++ b/src/qml/qml/qqmltypedata.cpp
@@ -671,6 +671,11 @@ void QQmlTypeData::initializeFromCachedUnit(const QQmlPrivate::CachedQmlUnit *un
return;
}
+ if (unit->qmlData->qmlUnit()->nObjects == 0) {
+ setError(QQmlTypeLoader::tr("Cached QML Unit has no objects"));
+ return;
+ }
+
m_document.reset(new QmlIR::Document(isDebugging()));
QQmlIRLoader loader(unit->qmlData, m_document.data());
loader.load();
diff --git a/tests/auto/qml/qmlcppcodegen/data/Confused/CMakeLists.txt b/tests/auto/qml/qmlcppcodegen/data/Confused/CMakeLists.txt
index e60c71678b..04b00708fd 100644
--- a/tests/auto/qml/qmlcppcodegen/data/Confused/CMakeLists.txt
+++ b/tests/auto/qml/qmlcppcodegen/data/Confused/CMakeLists.txt
@@ -4,12 +4,18 @@ qt_autogen_tools_initial_setup(confused_test_module)
qt_policy(SET QTP0001 NEW)
# Not QTP0004, since we have a manually written qmldir in a strange place
+set_source_files_properties("Test/broken.js"
+ PROPERTIES QT_RESOURCE_ALIAS "Test/broken.qml"
+)
+
qt_add_qml_module(confused_test_module
URI Confused
VERSION 1.0
QML_FILES
Main.qml
+ Main2.qml
Test/test.js
+ Test/broken.js
RESOURCES
Test/qmldir
)
diff --git a/tests/auto/qml/qmlcppcodegen/data/Confused/Main2.qml b/tests/auto/qml/qmlcppcodegen/data/Confused/Main2.qml
new file mode 100644
index 0000000000..1878122b91
--- /dev/null
+++ b/tests/auto/qml/qmlcppcodegen/data/Confused/Main2.qml
@@ -0,0 +1,6 @@
+import QtQml
+import "Test" as T
+
+QtObject {
+ Component.onCompleted: T.Broken.Print()
+}
diff --git a/tests/auto/qml/qmlcppcodegen/data/Confused/Test/broken.js b/tests/auto/qml/qmlcppcodegen/data/Confused/Test/broken.js
new file mode 100644
index 0000000000..682e1d6e89
--- /dev/null
+++ b/tests/auto/qml/qmlcppcodegen/data/Confused/Test/broken.js
@@ -0,0 +1,3 @@
+.pragma library
+
+function Print() { console.log("Hello from Broken") }
diff --git a/tests/auto/qml/qmlcppcodegen/data/Confused/Test/qmldir b/tests/auto/qml/qmlcppcodegen/data/Confused/Test/qmldir
index d6665aae75..5315ead60c 100644
--- a/tests/auto/qml/qmlcppcodegen/data/Confused/Test/qmldir
+++ b/tests/auto/qml/qmlcppcodegen/data/Confused/Test/qmldir
@@ -1 +1,2 @@
Test test.js
+Broken broken.qml
diff --git a/tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp b/tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp
index d8867f1327..a67f893df1 100644
--- a/tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp
+++ b/tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp
@@ -1106,6 +1106,18 @@ void tst_QmlCppCodegen::confusedModule()
QTest::ignoreMessage(QtDebugMsg, "Hello from Test");
QScopedPointer<QObject> object(component.create());
QVERIFY(!object.isNull());
+
+ QQmlComponent component2(&engine, QUrl(u"qrc:/qt/qml/Confused/Main2.qml"_s));
+ QVERIFY2(!component2.isError(), component2.errorString().toUtf8());
+
+ // TODO: We would like to have a better error here, but we currently cannot propagate it.
+ QTest::ignoreMessage(
+ QtWarningMsg,
+ "qrc:/qt/qml/Confused/Main2.qml:5: "
+ "TypeError: Property 'Print' of object Broken is not a function");
+
+ QScopedPointer<QObject> object2(component2.create());
+ QVERIFY(!object2.isNull());
}
void tst_QmlCppCodegen::consoleObject()