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

QVariant::convert may crash even if QVariant::canConvert returns true

    XMLWordPrintable

Details

    • All
    • a8792feaa (dev), 08d1a474c (6.5), 3af876d53 (6.6)

    Description

      Playing with QVariant in ActiveQt, I found that QVariant::canConvert returns true when converting from a Type* to a Type even if the type is not copy-constructible. In such cases, attempting to call QVariant::convert crashes (access violation). To provoke the crash, the target type's QMetaType needs to have an associated meta object.
       
      As I see it, there are two issues
       

      • QVariant::canConvert returns true for non-copyable objects 
      • QVariant::convert crashes with noncopyable objects (Update: they have to have an associated MetaObject (gadget or QObject))
         
        Reproducer:
        #include <QtCore>
        
        struct MyType : QObject
        {
            Q_OBJECT
        };
        
        int main()
        {
            QMetaType valueType{ qRegisterMetaType<MyType>("MyType") };
            QMetaType pointerType{ qRegisterMetaType<MyType *>("MyType*") };
        
            MyType instance;
            MyType *pointer = &instance;
        
            QVariant ptrAsVariant{ pointerType, &pointer };
        
            // Why is MyType* considered convertible to MyType?
            if (ptrAsVariant.canConvert(valueType)) {
        
                // The following line crashes and logs 'QVariant: Provided
                // metatype for 'MyType' does not support destruction and
                // copy construction'
                ptrAsVariant.convert(valueType);
            }
        }
        
        #include "main.moc" 

      Attachments

        Issue Links

          No reviews matched the request. Check your Options in the drop-down menu of this sections header.

          Activity

            People

              johanseg Jøger Hansegård
              johanseg Jøger Hansegård
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Gerrit Reviews

                  There are no open Gerrit changes