Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-114910

Missing constructor params crashes program

    XMLWordPrintable

Details

    • Windows
    • 3110117bf (dev), ad529b77f (6.5), 390ee7aa2 (6.6), 53bdc05d1 (tqtc/lts-6.2)

    Description

      When a QMetaObject is registered as a global object property, and a new instance of the class is created in the JS program, if the constructor in the JS program is missing parameters, the application will crash immediately.

      In the following example, the Example class constructor takes a single parameter. When the constructor is called with no parameters from JS, the program will crash:

      Example.h:

      #include <QObject>
      
      class Example : public QObject {
          Q_OBJECT
      public:
          Q_INVOKABLE Example (int id) { }
      }; 

      Program:

      #include <QCoreApplication>
      #include <QJSEngine>
      #include <QDebug>
      #include "example.h"
      
      int main (int argc, char *argv[]) {
          QCoreApplication a(argc, argv);
      
          QJSEngine engine;
          QJSValue meta = engine.newQMetaObject(&Example::staticMetaObject);
          engine.globalObject().setProperty("Example", meta);
      
          //QString program = "new Example(123);";   // works fine
          QString program = "new Example();";        // crashes program
          QJSValue result = engine.evaluate(program);
      
          qDebug() << result.toString();
          qDebug() << "finished";
      
          return a.exec();
      } 

       

      Expected results: `evaluate` should return a JS error indicating that a parameter was missing (or some default constructed value should be passed as the parameter, which would be more in line with JS behavior).

      Actual results: The program will crash when `new Example()` is executed.

      Note: The program runs fine if `new Example(123)` is executed instead.

      Attachments

        For Gerrit Dashboard: QTBUG-114910
        # Subject Branch Project Status CR V

        Activity

          People

            ulherman Ulf Hermann
            jasonc Jason Cipriani
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes