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

QStringView::split returns invalid data

    XMLWordPrintable

    Details

    • Platform/s:
      Linux/Wayland, Windows

      Description

      See code example below. The returned QList<QStringView> returned from QStringView::split contains QStringView s that point into a QString local to QStringView::split, rather originalString (the string that view is viewing)

      See line 311 of qtbase\src\corelib\text\qregularexpression.h - the QString is allocated from raw data using the view's data() member, but somehow the string s reallocates before the function returns. (it seems to be on the call to splitRef for some reason)

      The consequence is that the list of views returns by split cannot be safely accessed, as the underlying data has already been freed.

      Example:

      #include <QString>
      #include <QRegularExpression>
      #include <QStringView>
      #include <QDebug>
      
      int main(int argc, char *argv[])
      {
          const QString originalString = "A\nA\nB\nB\n\nC\nC";
          qDebug()<<"String data location:"<<originalString.data();
      
          QStringView view( originalString );
          qDebug()<<"View data location:"<<view.data();
      
          auto split = view.split(QRegularExpression( "(\r\n|\n|\r)" ), Qt::KeepEmptyParts);
      
          //these values should be equal, but they are not
          qDebug()<<"first split element data location:"<<split[0].data();
          qDebug()<<"original string data location:"<<originalString.data();
      
          return 0;
      }
      

        Attachments

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

          Activity

            People

            Assignee:
            mmutz Marc Mutz
            Reporter:
            ereid Elias Reid
            Votes:
            1 Vote for this issue
            Watchers:
            5 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: