Don't use the QRegExp methods that modify the object [QtGui]
authorThiago Macieira <thiago.macieira@intel.com>
Fri, 20 Apr 2012 12:59:30 +0000 (14:59 +0200)
committerQt by Nokia <qt-info@nokia.com>
Tue, 24 Apr 2012 17:36:55 +0000 (19:36 +0200)
QRegExp matching methods modify the object, which we don't want to. In
particular, when we receive a QRegExp from the user or we store in a
context that might require thread-safety, make sure we make a copy
before using it.

QRegularExpression has no such shortcoming.

Task-number: QTBUG-25064
Change-Id: If119e06221ca99e57c5ad1a1d4cc6468e9f68c7b
Reviewed-by: Gunnar Sletta <gunnar.sletta@nokia.com>
src/gui/text/qtextdocument.cpp
src/gui/util/qvalidator.cpp
tests/auto/other/lancelot/paintcommands.cpp

index a8991d5..f9fe166 100644 (file)
@@ -1281,7 +1281,7 @@ QTextCursor QTextDocument::find(const QString &subString, const QTextCursor &fro
 static bool findInBlock(const QTextBlock &block, const QRegExp &expression, int offset,
                         QTextDocument::FindFlags options, QTextCursor &cursor)
 {
-    const QRegExp expr(expression);
+    QRegExp expr(expression);
     QString text = block.text();
     text.replace(QChar::Nbsp, QLatin1Char(' '));
 
index d0bd1cf..3103cc7 100644 (file)
@@ -871,10 +871,11 @@ QRegExpValidator::~QRegExpValidator()
 
 QValidator::State QRegExpValidator::validate(QString &input, int& pos) const
 {
-    if (r.exactMatch(input)) {
+    QRegExp copy = r;
+    if (copy.exactMatch(input)) {
         return Acceptable;
     } else {
-        if (const_cast<QRegExp &>(r).matchedLength() == input.size()) {
+        if (copy.matchedLength() == input.size()) {
             return Intermediate;
         } else {
             pos = input.size();
index 969db62..4e45c75 100644 (file)
@@ -694,7 +694,7 @@ void PaintCommands::runCommand(const QString &scriptLine)
     QString firstWord = scriptLine.section(QRegExp("\\s"), 0, 0);
     QList<int> indices = s_commandHash.values(firstWord);
     foreach(int idx, indices) {
-        const PaintCommandInfos &command = s_commandInfoTable.at(idx);
+        PaintCommandInfos command = s_commandInfoTable.at(idx);
         if (command.regExp.indexIn(scriptLine) >= 0) {
             (this->*(command.paintMethod))(command.regExp);
             return;