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; }
Attachments
For Gerrit Dashboard: QTBUG-98653 | ||||||
---|---|---|---|---|---|---|
# | Subject | Branch | Project | Status | CR | V |
383616,6 | Check that QStringView::split() w/rvalue QRegularExpression returns valid data | dev | qt/qtbase | Status: MERGED | +2 | 0 |
383644,2 | Check that QStringView::split() w/rvalue QRegularExpression returns valid data | 6.2 | qt/qtbase | Status: MERGED | +2 | 0 |
383645,3 | Check that QStringView::split() w/rvalue QRegularExpression returns valid data | tqtc/lts-5.15 | qt/tqtc-qtbase | Status: MERGED | +2 | 0 |
383712,3 | QStringView: fix split(QRegularExpression) returning invalid data | tqtc/lts-5.15 | qt/tqtc-qtbase | Status: MERGED | +2 | 0 |