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

Strange behaviour QString to const char * conversation

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Invalid
    • Icon: Not Evaluated Not Evaluated
    • None
    • 5.4.0
    • None

      Well... Might not be a bug, but this is kind of strange. at 5.3.2 never encounter a problem like this. After upgrading 5.4, it behaves strangely.

      #include <QGuiApplication>
      #include <QDebug>
      
      class ConvertBug {
      public:
          ConvertBug() {}
      
          QString getTestStr() const {
              QString str = QLatin1String("TEST STRING");
              return str;
          }
      };
      
      int main(int argc, char *argv[])
      {
          Q_UNUSED(argc);
          Q_UNUSED(argv);
      
          const ConvertBug instance;
          QString testStr = instance.getTestStr();
      
          qDebug() << "STRING DATA via QString:" << testStr;
          qDebug() << "STRING DATA via QString::toUtf8():" << testStr.toUtf8();
          qDebug() << "STRING DATA via QString::toUtf8().data():" << testStr.toUtf8().data();
      
          const char *cTestStr = testStr.toUtf8().data();
          qDebug() << "STRING DATA via const char*:" << cTestStr;
      
          cTestStr = static_cast<QByteArray>(testStr.toUtf8()).data();
          qDebug() << "STRING DATA via const char* after static_cast:" << cTestStr;
      
          QByteArray byteArray = testStr.toUtf8();
          qDebug() << "STRING DATA via QByteArray:" << byteArray;
      
          const char *cTestStr2 = byteArray.data();
          qDebug() << "STRING DATA via const char * after ByteArray conversation:" << cTestStr2;
      }
      

      Expected result is:

      STRING DATA via QString: "TEST STRING"
      STRING DATA via QString::toUtf8(): "TEST STRING"
      STRING DATA via QString::toUtf8().data(): TEST STRING
      STRING DATA via const char*: TEST STRING
      STRING DATA via const char* after static_cast: TEST STRING
      STRING DATA via QByteArray: "TEST STRING"
      STRING DATA via const char * after ByteArray conversation: TEST STRING
      

      Actual result is:

      STRING DATA via QString: "TEST STRING"
      STRING DATA via QString::toUtf8(): "TEST STRING"
      STRING DATA via QString::toUtf8().data(): TEST STRING
      STRING DATA via const char*: T
      STRING DATA via const char* after static_cast: T
      STRING DATA via QByteArray: "TEST STRING"
      STRING DATA via const char * after ByteArray conversation: TEST STRING
      

      in here, I think there is a heap corruption.

          const char *cTestStr = testStr.toUtf8().data();
          qDebug() << "STRING DATA via const char*:" << cTestStr;
      

      Can you confirm? Or am i doing wrong something?

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

            laknoll Lars Knoll
            firatagdas Firat Agdas
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved:

                There are no open Gerrit changes