Details
-
Bug
-
Resolution: Invalid
-
Not Evaluated
-
None
-
5.15.2, 6.3.0
-
None
-
Clang 11, GCC 10.2.1, MinGW 8.1
Description
Calling QMetaMethod::invoke of one object on different object causes unexpected (yet slightly controllable) behavior. There is no validation.
Consider the following example:
firstclass.h
#ifndef FIRSTCLASS_H #define FIRSTCLASS_H #include <QObject> #include <QDebug> class FirstClass: public QObject { Q_OBJECT public: int firstDigit = 1; Q_INVOKABLE void aMethod() {qInfo() << "First" << firstDigit;} }; #endif // FIRSTCLASS_H
secondclass.h
#ifndef SECONDCLASS_H #define SECONDCLASS_H #include <QObject> class SecondClass: public QObject { Q_OBJECT public: int secondDigit = 2; }; #endif // SECONDCLASS_H
main.cpp
#include <QCoreApplication> #include <QMetaMethod> #include "firstclass.h" #include "secondclass.h" int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); FirstClass first; SecondClass second; auto firstIndex = first.metaObject()->indexOfMethod("aMethod()"); auto firstMethod = first.metaObject()->method(firstIndex); firstMethod.invoke(&first, Qt::DirectConnection); // Returns True // Expected output: "First 1" | Actual output "First 1" firstMethod.invoke(&second, Qt::DirectConnection); // Also returns True // Expected output: No output? Error? "First 1"? | Actual output "First 2" return 0; }
I would expect for invoke to at least verify that such a method (aMethod()) exists in the second object and return false or at least give me a runtime warning. Yet it silently uses FirstClass method with SecondClass data. You can even replace second object with empty QObject and get a random number from memory in the output.