From 427da064144a63cebb8f4ad0f053f96c9aceacd8 Mon Sep 17 00:00:00 2001 From: Giuseppe D'Angelo Date: Wed, 2 Dec 2020 12:24:41 +0100 Subject: [PATCH] QRE: discourage users from assuming that QRE stores the subject When matching over a QString we store a (shallow) copy of it. That has always been an implementation detail and people should've never relied on it, but Hyrum's law, we don't want to actually exploit this by NOT taking the copy. Converesely, matching over QStringView already requires that the string data is kept alive as long as QRE(Match) objects are alive. Add a doc note to give us the freedom to do the change in Qt 7. [ChangeLog][QtCore][QRegularExpression] QRegularExpression takes a shallow copy of a QString subject when matching over it. This means that users can destroy or modify the string data while still having match results over it. This behavior is deprecated; in a future version of Qt, QRegularExpression will no longer take a copy. Note that behavior has always been undocumented and users were never supposed to modify a subject string while match objects were alive on it. In practice, it's very unlikely that your code is relying on the existing behavior. Change-Id: Ibc5f900c09a007139fb12fc4d7f11e4a8f31bf38 Reviewed-by: Thiago Macieira --- src/corelib/text/qregularexpression.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/corelib/text/qregularexpression.cpp b/src/corelib/text/qregularexpression.cpp index 827884d709..fd0e10dfe6 100644 --- a/src/corelib/text/qregularexpression.cpp +++ b/src/corelib/text/qregularexpression.cpp @@ -1571,6 +1571,11 @@ qsizetype QRegularExpression::patternErrorOffset() const The returned QRegularExpressionMatch object contains the results of the match. + \note The data referenced by \a subject should remain valid as long + as there are QRegularExpressionMatch objects using it. At the moment + Qt makes a (shallow) copy of the data, but this behavior may change + in a future version of Qt. + \sa QRegularExpressionMatch, {normal matching} */ QRegularExpressionMatch QRegularExpression::match(const QString &subject, @@ -1628,6 +1633,11 @@ QRegularExpressionMatch QRegularExpression::match(QStringView subjectView, The returned QRegularExpressionMatchIterator is positioned before the first match result (if any). + \note The data referenced by \a subject should remain valid as long + as there are QRegularExpressionMatch objects using it. At the moment + Qt makes a (shallow) copy of the data, but this behavior may change + in a future version of Qt. + \sa QRegularExpressionMatchIterator, {global matching} */ QRegularExpressionMatchIterator QRegularExpression::globalMatch(const QString &subject,