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

QT_RESTRICTED_CAST_FROM_ASCII for QVariant

    XMLWordPrintable

Details

    Description

      QT_RESTRICTED_CAST_FROM_ASCII doesn't prevent casting string into QVariant as it does for QString. Customer reports:

      "Consider the following code snippet:

      ====================================
      #include <QtCore/QVariant>

      void foo(const char* p)
      {
      QString s1("ABCDE");
      QString s2(p);
      QVariant v1("ABCDE");
      QVariant v2(p);
      }
      ====================================

      If I compile it with -DQT_NO_CAST_FROM_ASCII I get 4 compile errors, one from each of constructor calls for s1, s2, v1, v2. And that's what is expected to happen.

      However, if I compile it with -DQT_RESTRICTED_CAST_FROM_ASCII I only get an error for s2. The fact that I don't get an error for v2 here means that the protection that the macro QT_RESTRICTED_CAST_FROM_ASCII
      offers is broken. That's a pity because the macro would help to simplify our code a lot.

      The problem is that QVariant only checks for the macro QT_NO_CAST_FROM_ASCII as in qvariant.h, line 223ff:

      #ifndef QT_NO_CAST_FROM_ASCII
      QT_ASCII_CAST_WARN QVariant(const char *str);
      #endif

      IMHO it would make sense to check both macros here instead:

      #if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
      QT_ASCII_CAST_WARN QVariant(const char *str);
      #endif

      That would mean that I would have to write

      QVariant v1(QString("ABCDE"));

      instead of just

      QVariant v1("ABCDE");

      to make all lines with string literals compile, but IMHO that's better than to have no protection against unwanted conversions.

      A nicer solution would be to use the template magic with character arrays as in qstring.h, line 685ff:

      #if defined(QT_RESTRICTED_CAST_FROM_ASCII)
      template <int N>
      inline QString(const char (&ch;)[N])
      ....
      #endif

      But that might be too much effort for not enough benefit.

      Btw, there are other files that only check for QT_NO_CAST_FROM_ASCII but not for QT_RESTRICTED_CAST_FROM_ASCII, for example:

      qbytearray.h
      qcbormap.h
      qcborstream.h
      qcborvalue.h
      qjsonvalue.h
      qstringbuilder.h"

      Attachments

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

        Activity

          People

            hjk hjk
            izero Tero Siironen
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes