diff options
author | Ulf Hermann <ulf.hermann@qt.io> | 2022-02-21 14:46:57 +0100 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2022-03-02 21:50:35 +0000 |
commit | d9a492469abb9bf6e3621e7a0630b5978e52de38 (patch) | |
tree | 12935828100572798f5e63f12f92a16208944d0f | |
parent | e14866222a4c07bd4b71344d4a83034e17c51984 (diff) |
qmltyperegistrar: Tolerate and warn about invalid URIs
If you give qmltyperegistrar an invalid URI it should still not generate
invalid C++ code. In fact the module will still be somewhat usable. You
just cannot import it.
Task-number: QTBUG-101072
Change-Id: I21232f99c1ef486a62dbe339f7d0ae1b9abc8871
Reviewed-by: Andrei Golubev <andrei.golubev@qt.io>
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
(cherry picked from commit 88e96debfdad3961db3225de7b4c7f90afe7698e)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r-- | src/qmltyperegistrar/qmltyperegistrar.cpp | 13 | ||||
-rw-r--r-- | tests/auto/qml/qmltyperegistrar/CMakeLists.txt | 10 |
2 files changed, 21 insertions, 2 deletions
diff --git a/src/qmltyperegistrar/qmltyperegistrar.cpp b/src/qmltyperegistrar/qmltyperegistrar.cpp index cc3d4932b1..eb7883f054 100644 --- a/src/qmltyperegistrar/qmltyperegistrar.cpp +++ b/src/qmltyperegistrar/qmltyperegistrar.cpp @@ -250,11 +250,20 @@ int main(int argc, char **argv) fprintf(output, "\n\n"); + // Keep this in sync with _qt_internal_get_escaped_uri in CMake QString moduleAsSymbol = module; - moduleAsSymbol.replace(QLatin1Char('.'), QLatin1Char('_')); + moduleAsSymbol.replace(QRegularExpression(QStringLiteral("[^A-Za-z0-9]")), QStringLiteral("_")); - const QString functionName = QStringLiteral("qml_register_types_") + moduleAsSymbol; + QString underscoredModuleAsSymbol = module; + underscoredModuleAsSymbol.replace(QLatin1Char('.'), QLatin1Char('_')); + + if (underscoredModuleAsSymbol != moduleAsSymbol + || underscoredModuleAsSymbol.isEmpty() + || underscoredModuleAsSymbol.front().isDigit()) { + qWarning() << module << "is an invalid QML module URI. You cannot import this."; + } + const QString functionName = QStringLiteral("qml_register_types_") + moduleAsSymbol; fprintf(output, "#if !defined(QT_STATIC)\n" "#define Q_QMLTYPE_EXPORT Q_DECL_EXPORT\n" diff --git a/tests/auto/qml/qmltyperegistrar/CMakeLists.txt b/tests/auto/qml/qmltyperegistrar/CMakeLists.txt index c7326c30c4..ad22f51cd9 100644 --- a/tests/auto/qml/qmltyperegistrar/CMakeLists.txt +++ b/tests/auto/qml/qmltyperegistrar/CMakeLists.txt @@ -62,3 +62,13 @@ set_target_properties(tst_qmltyperegistrar PROPERTIES # yet, so we have to call it directly to test that code path for now. _qt_internal_qml_type_registration(tst_qmltyperegistrar MANUAL_MOC_JSON_FILES ${json_list}) add_subdirectory(foreign) + +qt_add_library(tst-qmltyperegistrar-with-dashes) +target_link_libraries(tst-qmltyperegistrar-with-dashes PRIVATE Qt::Core Qt::Qml) +qt_enable_autogen_tool(tst-qmltyperegistrar-with-dashes "moc" ON) +qt_add_qml_module(tst-qmltyperegistrar-with-dashes + URI Module-With-Dashes + VERSION 1.0 + SOURCES + foo.cpp foo.h +) |