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 <matthias.rauter@qt.io>
(cherry picked from commit 92c2ebdbcca9cfae6e4048004b3bdb58af972209)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Ivan Solovev 2025-04-07 17:31:15 +02:00 committed by Qt Cherry-pick Bot
parent b5582df317
commit adbb269eb9
2 changed files with 9 additions and 4 deletions

View File

@ -1065,9 +1065,10 @@ void QDBusXmlToCpp::writeAdaptor(const QString &filename,
if (usingInvokeMethod) { if (usingInvokeMethod) {
// we are using QMetaObject::invokeMethod // we are using QMetaObject::invokeMethod
if (!returnType.isEmpty()) if (!returnType.isEmpty()) {
cs << " " << returnType << " " << argNames.at(method.inputArgs.size()) cs << " " << returnType << " " << argNames.at(method.inputArgs.size())
<< ";\n"; << "{};\n";
}
static const char invoke[] = " QMetaObject::invokeMethod(parent(), \""; static const char invoke[] = " QMetaObject::invokeMethod(parent(), \"";
cs << invoke << name << "\""; cs << invoke << name << "\"";

View File

@ -201,7 +201,9 @@ void tst_qdbusxml2cpp::process_data()
.arg(basicTypeList[i].dbusType) .arg(basicTypeList[i].dbusType)
<< QRegularExpression(QString("Q_SLOTS:.*\\bQDBusPendingReply<%1> Method\\((const )?%1 ") << QRegularExpression(QString("Q_SLOTS:.*\\bQDBusPendingReply<%1> Method\\((const )?%1 ")
.arg(basicTypeList[i].cppType), QRegularExpression::DotMatchesEverythingOption) .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); .arg(basicTypeList[i].cppType), QRegularExpression::DotMatchesEverythingOption);
} }
@ -224,7 +226,9 @@ void tst_qdbusxml2cpp::process_data()
"</method>" "</method>"
<< QRegularExpression("Q_SLOTS:.*\\bQDBusPendingReply<Point> Method\\(PointF ", << QRegularExpression("Q_SLOTS:.*\\bQDBusPendingReply<Point> Method\\(PointF ",
QRegularExpression::DotMatchesEverythingOption) 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); QRegularExpression::DotMatchesEverythingOption);
QTest::newRow("method-ss") QTest::newRow("method-ss")