Details
-
Bug
-
Resolution: Fixed
-
P2: Important
-
5.15.6, 5.15.7
-
-
8444b1ed1f (qt/tqtc-qtbase/5.15-opensource)
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; }