From adbb269eb9ee4cd01cc397b5691830906b07486c Mon Sep 17 00:00:00 2001 From: Ivan Solovev Date: Mon, 7 Apr 2025 17:31:15 +0200 Subject: [PATCH] qdbusxml2cpp: initialize the return value for adapters If the interface has a method with a specified return value, the generated adapter will contain a code like: bool out; QMetaObject::invokeMethod(parent(), "Func", Q_RETURN_ARG(bool, out)); return out; In this case Q_RETURN_ARG macro makes sure that the variable `out` is properly initialized before being returned from a function, but only if invokeMethod() call is executed successfully. Update the generator to zero-initialize (or value-initialize) the return variable, so that it returns some reasonable value even if invokeMethod() fails. Extend the unit-tests to make sure that the generated adapters always initialize the return variables. Coverity-Id: 479703 Pick-to: 6.8 6.5 Change-Id: I4d15ccc6844b5ca454ab9f0cf72fd8e3f0c1b704 Reviewed-by: Matthias Rauter (cherry picked from commit 92c2ebdbcca9cfae6e4048004b3bdb58af972209) Reviewed-by: Qt Cherry-pick Bot --- src/tools/qdbusxml2cpp/qdbusxml2cpp.cpp | 5 +++-- tests/auto/tools/qdbusxml2cpp/tst_qdbusxml2cpp.cpp | 8 ++++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/tools/qdbusxml2cpp/qdbusxml2cpp.cpp b/src/tools/qdbusxml2cpp/qdbusxml2cpp.cpp index d637854d2bb..1acb83df36b 100644 --- a/src/tools/qdbusxml2cpp/qdbusxml2cpp.cpp +++ b/src/tools/qdbusxml2cpp/qdbusxml2cpp.cpp @@ -1065,9 +1065,10 @@ void QDBusXmlToCpp::writeAdaptor(const QString &filename, if (usingInvokeMethod) { // we are using QMetaObject::invokeMethod - if (!returnType.isEmpty()) + if (!returnType.isEmpty()) { cs << " " << returnType << " " << argNames.at(method.inputArgs.size()) - << ";\n"; + << "{};\n"; + } static const char invoke[] = " QMetaObject::invokeMethod(parent(), \""; cs << invoke << name << "\""; diff --git a/tests/auto/tools/qdbusxml2cpp/tst_qdbusxml2cpp.cpp b/tests/auto/tools/qdbusxml2cpp/tst_qdbusxml2cpp.cpp index c51a0909f8d..db7d462014d 100644 --- a/tests/auto/tools/qdbusxml2cpp/tst_qdbusxml2cpp.cpp +++ b/tests/auto/tools/qdbusxml2cpp/tst_qdbusxml2cpp.cpp @@ -201,7 +201,9 @@ void tst_qdbusxml2cpp::process_data() .arg(basicTypeList[i].dbusType) << QRegularExpression(QString("Q_SLOTS:.*\\bQDBusPendingReply<%1> Method\\((const )?%1 ") .arg(basicTypeList[i].cppType), QRegularExpression::DotMatchesEverythingOption) - << QRegularExpression(QString("Q_SLOTS:.*\\b%1 Method\\((const )?%1 ") + << QRegularExpression(QString("Q_SLOTS:.*\\b%1 Method\\((const )?%1 &?in0\\);" + ".*%1 .*::Method\\((const )?%1 &?in0\\)\n{\n" + ".*%1 out0{};") .arg(basicTypeList[i].cppType), QRegularExpression::DotMatchesEverythingOption); } @@ -224,7 +226,9 @@ void tst_qdbusxml2cpp::process_data() "" << QRegularExpression("Q_SLOTS:.*\\bQDBusPendingReply Method\\(PointF ", QRegularExpression::DotMatchesEverythingOption) - << QRegularExpression("Q_SLOTS:.*\\bPoint Method\\(PointF ", + << QRegularExpression("Q_SLOTS:.*\\bPoint Method\\(PointF in0\\);" + ".*Point .*::Method\\(PointF in0\\)\n{\n" + ".*Point out0{};", QRegularExpression::DotMatchesEverythingOption); QTest::newRow("method-ss")