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

Creating a QByteArray from a QJsonDocument containing a string longer than 65535 fails.

    XMLWordPrintable

Details

    • Bug
    • Resolution: Done
    • P2: Important
    • 5.1.0 RC1
    • 5.0.2
    • Core: Other
    • None
    • Windows 8 - QtCreator 2.7.0 - Qt 5.0.2

    Description

      The problem is that creating a QByteArray from a QJsonDocument containing a string longer than 65535 as a value, fails.

      Consider the following code:

          /* Create a QString 70000 chars long */
          QString s;
          for(int i = 0; i < 70000; i++)
              s.append("c");
      
          /* Create a QMap with a single key/value pair */
          QMap <QString, QVariant> map;
          map["key"] = s;
      	
          /* Create a QJsonDocument from the QMap ... */
          QJsonDocument d1 = QJsonDocument::fromVariant(QVariant(map));
          /* ... and a QByteArray from the QJsonDocument */
          QByteArray a1 = d1.toJson();
      
          /* Create a QJsonDocument from the QByteArray ... */
          QJsonDocument d2 = QJsonDocument::fromJson(a1);
          /* ... and a QByteArray from the QJsonDocument */
          QByteArray a2 = d2.toJson();
      

      Now a1 and a2 should be equal but on the contrary a2 is truncated to size(a1) - 65536 = 70000 - 65536 = 4464 bytes.

      The problem is in Latin1String class in qjson_p.h file

      class Latin1String
      {
      public:
          Latin1String(const char *data) { d = (Data *)data; }
      
          struct Data {
              qle_short length;
              char latin1[1];
          };
          Data *d;
      ...
      };
      

      where the length of the string is defined using a qle_short (16bit).

      Attachments

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

        Activity

          People

            laknoll Lars Knoll
            alessandrodl Alessandro Dalla Lana
            Votes:
            2 Vote for this issue
            Watchers:
            5 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes