Don't use the QRegExp methods that modify the object
authorThiago Macieira <thiago.macieira@intel.com>
Mon, 23 Apr 2012 14:29:18 +0000 (16:29 +0200)
committerQt by Nokia <qt-info@nokia.com>
Tue, 24 Apr 2012 12:39:38 +0000 (14:39 +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: I2c4d5f4b60d6b3569568103cd7107cd2adaa3ae8
Reviewed-by: Giuseppe D'Angelo <dangelog@gmail.com>
Reviewed-by: Lars Knoll <lars.knoll@nokia.com>
14 files changed:
src/xmlpatterns/data/qabstractdatetime_p.h
src/xmlpatterns/data/qabstractduration_p.h
src/xmlpatterns/data/qderivedstring_p.h
src/xmlpatterns/functions/qpatternmatchingfns.cpp
src/xmlpatterns/functions/qpatternplatform.cpp
src/xmlpatterns/functions/qpatternplatform_p.h
src/xmlpatterns/parser/qquerytransformparser.cpp
src/xmlpatterns/parser/querytransformparser.ypp
src/xmlpatterns/schema/qxsdschemachecker.cpp
src/xmlpatterns/schema/qxsdschemaparser.cpp
src/xmlpatterns/schema/qxsdtypechecker.cpp
tests/auto/qxmlquery/tst_qxmlquery.cpp
tests/auto/xmlpatterns/tst_xmlpatterns.cpp
tools/xmlpatterns/qapplicationargumentparser.cpp

index 9c4b9d1..6fc72c7 100644 (file)
@@ -140,7 +140,7 @@ namespace QPatternist
                 Q_ASSERT(exp.isValid());
             }
 
-            const QRegExp regExp;
+            QRegExp regExp;
             const qint8 zoneOffsetSign;
             const qint8 zoneOffsetHour;
             const qint8 zoneOffsetMinute;
index 1667db5..a13d3f7 100644 (file)
@@ -116,7 +116,7 @@ namespace QPatternist
                 Q_ASSERT(yearP == -1 || yearP == 2);
             }
 
-            const QRegExp regExp;
+            QRegExp regExp;
             const qint8 year;
             const qint8 month;
             const qint8 day;
index 87d9a2a..5f54f83 100644 (file)
@@ -262,7 +262,7 @@ namespace QPatternist
                 {
                     const QString simplified(lexical.trimmed());
 
-                    const QRegExp validate(QLatin1String("[a-zA-Z]{1,8}(-[a-zA-Z0-9]{1,8})*"));
+                    QRegExp validate(QLatin1String("[a-zA-Z]{1,8}(-[a-zA-Z0-9]{1,8})*"));
                     Q_ASSERT(validate.isValid());
 
                     if(validate.exactMatch(simplified))
index 41ffe54..27c7dd4 100644 (file)
@@ -59,7 +59,7 @@ MatchesFN::MatchesFN() : PatternPlatform(2)
 
 Item MatchesFN::evaluateSingleton(const DynamicContext::Ptr &context) const
 {
-    const QRegExp regexp(pattern(context));
+    QRegExp regexp(pattern(context));
     QString input;
 
     const Item arg(m_operands.first()->evaluateSingleton(context));
@@ -75,7 +75,7 @@ ReplaceFN::ReplaceFN() : PatternPlatform(3)
 
 Item ReplaceFN::evaluateSingleton(const DynamicContext::Ptr &context) const
 {
-    const QRegExp regexp(pattern(context));
+    QRegExp regexp(pattern(context));
     QString input;
 
     const Item arg(m_operands.first()->evaluateSingleton(context));
@@ -219,7 +219,7 @@ Item::Iterator::Ptr TokenizeFN::evaluateSequence(const DynamicContext::Ptr &cont
     if(input.isEmpty())
         return CommonValues::emptyIterator;
 
-    const QRegExp regExp(pattern(context));
+    QRegExp regExp(pattern(context));
     const QStringList result(input.split(regExp, QString::KeepEmptyParts));
 
     return makeItemMappingIterator<Item>(ConstPtr(this),
index 08dc2f1..e9bbfbb 100644 (file)
@@ -115,7 +115,7 @@ PatternPlatform::PatternPlatform(const qint8 flagsPosition) : m_compiledParts(No
 {
 }
 
-const QRegExp PatternPlatform::pattern(const DynamicContext::Ptr &context) const
+QRegExp PatternPlatform::pattern(const DynamicContext::Ptr &context) const
 {
     if(m_compiledParts == FlagsAndPattern) /* This is the most common case. */
     {
index ede3c4e..4a7f087 100644 (file)
@@ -113,7 +113,7 @@ namespace QPatternist
          * settings its flags, and everything else required for getting it ready to use. If an error
          * occurs, an appropriate error is raised via @p context.
          */
-        const QRegExp pattern(const DynamicContext::Ptr &context) const;
+        QRegExp pattern(const DynamicContext::Ptr &context) const;
 
         /**
          * @returns the number of captures, also called parenthesized sub-expressions, the pattern has.
index 25821ca..781b899 100644 (file)
@@ -3735,7 +3735,7 @@ yyreduce:
 /* Line 1269 of yacc.c.  */
 #line 1412 "querytransformparser.ypp"
     {
-        const QRegExp encNameRegExp(QLatin1String("[A-Za-z][A-Za-z0-9._\\-]*"));
+        QRegExp encNameRegExp(QLatin1String("[A-Za-z][A-Za-z0-9._\\-]*"));
 
         if(!encNameRegExp.exactMatch((yyvsp[(2) - (2)].sval)))
         {
index 1814f28..d69868e 100644 (file)
@@ -1470,7 +1470,7 @@ VersionDecl: /* empty */
 Encoding: /* empty */                                                               /* [X] */
 | ENCODING StringLiteral
     {
-        const QRegExp encNameRegExp(QLatin1String("[A-Za-z][A-Za-z0-9._\\-]*"));
+        QRegExp encNameRegExp(QLatin1String("[A-Za-z][A-Za-z0-9._\\-]*"));
 
         if(!encNameRegExp.exactMatch($2))
         {
index 0536fca..33e2679 100644 (file)
@@ -1124,7 +1124,7 @@ void XsdSchemaChecker::checkConstrainingFacets(const XsdFacet::Hash &facets, con
 
         for (int i = 0; i < multiValue.count(); ++i) {
             const DerivedString<TypeString>::Ptr value = multiValue.at(i);
-            const QRegExp exp = PatternPlatform::parsePattern(value->stringValue(), m_context, &reflection);
+            QRegExp exp = PatternPlatform::parsePattern(value->stringValue(), m_context, &reflection);
             if (!exp.isValid()) {
                 m_context->error(QtXmlPatterns::tr("%1 facet contains invalid regular expression").arg(formatKeyword("pattern.")), XsdSchemaContext::XSDError, sourceLocation(simpleType));
                 return;
index a00b77e..292886e 100644 (file)
@@ -511,7 +511,7 @@ void XsdSchemaParser::parseSchema(ParserType parserType)
     if (hasAttribute(CommonNamespaces::XML, QString::fromLatin1("lang"))) {
         const QString value = readAttribute(QString::fromLatin1("lang"), CommonNamespaces::XML);
 
-        const QRegExp exp(QString::fromLatin1("[a-zA-Z]{1,8}(-[a-zA-Z0-9]{1,8})*"));
+        QRegExp exp(QString::fromLatin1("[a-zA-Z]{1,8}(-[a-zA-Z0-9]{1,8})*"));
         if (!exp.exactMatch(value)) {
             attributeContentError("xml:lang", "schema", value);
             return;
@@ -1286,7 +1286,7 @@ XsdDocumentation::Ptr XsdSchemaParser::parseDocumentation()
     if (hasAttribute(CommonNamespaces::XML, QString::fromLatin1("lang"))) {
         const QString value = readAttribute(QString::fromLatin1("lang"), CommonNamespaces::XML);
 
-        const QRegExp exp(QString::fromLatin1("[a-zA-Z]{1,8}(-[a-zA-Z0-9]{1,8})*"));
+        QRegExp exp(QString::fromLatin1("[a-zA-Z]{1,8}(-[a-zA-Z0-9]{1,8})*"));
         if (!exp.exactMatch(value)) {
             attributeContentError("xml:lang", "documentation", value);
             return documentation;
index e0ce55b..af09816 100644 (file)
@@ -452,7 +452,7 @@ bool XsdTypeChecker::checkConstrainingFacetsString(const QString &value, const X
         bool found = false;
         for (int j = 0; j < multiValue.count(); ++j) {
             const QString pattern = multiValue.at(j)->as<DerivedString<TypeString> >()->stringValue();
-            const QRegExp exp = PatternPlatform::parsePattern(pattern, m_context, m_reflection);
+            QRegExp exp = PatternPlatform::parsePattern(pattern, m_context, m_reflection);
             if (exp.exactMatch(value)) {
                 found = true;
                 break;
@@ -547,7 +547,7 @@ bool XsdTypeChecker::checkConstrainingFacetsSignedInteger(long long value, const
         bool found = false;
         for (int j = 0; j < multiValue.count(); ++j) {
             const QString pattern = multiValue.at(j)->as<DerivedString<TypeString> >()->stringValue();
-            const QRegExp exp = PatternPlatform::parsePattern(pattern, m_context, m_reflection);
+            QRegExp exp = PatternPlatform::parsePattern(pattern, m_context, m_reflection);
             if (exp.exactMatch(lexicalValue)) {
                 found = true;
                 break;
@@ -633,7 +633,7 @@ bool XsdTypeChecker::checkConstrainingFacetsUnsignedInteger(unsigned long long v
         bool found = false;
         for (int j = 0; j < multiValue.count(); ++j) {
             const QString pattern = multiValue.at(j)->as<DerivedString<TypeString> >()->stringValue();
-            const QRegExp exp = PatternPlatform::parsePattern(pattern, m_context, m_reflection);
+            QRegExp exp = PatternPlatform::parsePattern(pattern, m_context, m_reflection);
             if (exp.exactMatch(lexicalValue)) {
                 found = true;
                 break;
@@ -719,7 +719,7 @@ bool XsdTypeChecker::checkConstrainingFacetsDouble(double value, const QString &
         bool found = false;
         for (int j = 0; j < multiValue.count(); ++j) {
             const QString pattern = multiValue.at(j)->as<DerivedString<TypeString> >()->stringValue();
-            const QRegExp exp = PatternPlatform::parsePattern(pattern, m_context, m_reflection);
+            QRegExp exp = PatternPlatform::parsePattern(pattern, m_context, m_reflection);
             if (exp.exactMatch(lexicalValue)) {
                 found = true;
                 break;
@@ -820,7 +820,7 @@ bool XsdTypeChecker::checkConstrainingFacetsDateTime(const QDateTime &value, con
         bool found = false;
         for (int j = 0; j < multiValue.count(); ++j) {
             const QString pattern = multiValue.at(j)->as<DerivedString<TypeString> >()->stringValue();
-            const QRegExp exp = PatternPlatform::parsePattern(pattern, m_context, m_reflection);
+            QRegExp exp = PatternPlatform::parsePattern(pattern, m_context, m_reflection);
             if (exp.exactMatch(lexicalValue)) {
                 found = true;
                 break;
@@ -898,7 +898,7 @@ bool XsdTypeChecker::checkConstrainingFacetsDuration(const AtomicValue::Ptr&, co
         bool found = false;
         for (int j = 0; j < multiValue.count(); ++j) {
             const QString pattern = multiValue.at(j)->as<DerivedString<TypeString> >()->stringValue();
-            const QRegExp exp = PatternPlatform::parsePattern(pattern, m_context, m_reflection);
+            QRegExp exp = PatternPlatform::parsePattern(pattern, m_context, m_reflection);
             if (exp.exactMatch(lexicalValue)) {
                 found = true;
                 break;
@@ -925,7 +925,7 @@ bool XsdTypeChecker::checkConstrainingFacetsBoolean(bool, const QString &lexical
         bool found = false;
         for (int j = 0; j < multiValue.count(); ++j) {
             const QString pattern = multiValue.at(j)->as<DerivedString<TypeString> >()->stringValue();
-            const QRegExp exp = PatternPlatform::parsePattern(pattern, m_context, m_reflection);
+            QRegExp exp = PatternPlatform::parsePattern(pattern, m_context, m_reflection);
             if (exp.exactMatch(lexicalValue)) {
                 found = true;
                 break;
@@ -1038,7 +1038,7 @@ bool XsdTypeChecker::checkConstrainingFacetsQName(const QXmlName &value, const Q
         bool found = false;
         for (int j = 0; j < multiValue.count(); ++j) {
             const QString pattern = multiValue.at(j)->as<DerivedString<TypeString> >()->stringValue();
-            const QRegExp exp = PatternPlatform::parsePattern(pattern, m_context, m_reflection);
+            QRegExp exp = PatternPlatform::parsePattern(pattern, m_context, m_reflection);
             if (exp.exactMatch(lexicalValue)) {
                 found = true;
                 break;
@@ -1215,7 +1215,7 @@ bool XsdTypeChecker::checkConstrainingFacetsList(const QStringList &values, cons
         bool found = false;
         for (int j = 0; j < multiValue.count(); ++j) {
             const QString pattern = multiValue.at(j)->as<DerivedString<TypeString> >()->stringValue();
-            const QRegExp exp = PatternPlatform::parsePattern(pattern, m_context, m_reflection);
+            QRegExp exp = PatternPlatform::parsePattern(pattern, m_context, m_reflection);
             if (exp.exactMatch(lexicalValue)) {
                 found = true;
                 break;
@@ -1272,7 +1272,7 @@ bool XsdTypeChecker::checkConstrainingFacetsUnion(const QString &value, const QS
         bool found = false;
         for (int j = 0; j < multiValue.count(); ++j) {
             const QString pattern = multiValue.at(j)->as<DerivedString<TypeString> >()->stringValue();
-            const QRegExp exp = PatternPlatform::parsePattern(pattern, m_context, m_reflection);
+            QRegExp exp = PatternPlatform::parsePattern(pattern, m_context, m_reflection);
             if (exp.exactMatch(lexicalValue)) {
                 found = true;
                 break;
index 372bcdb..1b5d424 100644 (file)
@@ -1728,7 +1728,7 @@ void tst_QXmlQuery::messageXML() const
 
     query.setQuery(QLatin1String("1basicSyntaxError"));
 
-    const QRegExp removeFilename(QLatin1String("Location: file:.*\\#"));
+    QRegExp removeFilename(QLatin1String("Location: file:.*\\#"));
     QVERIFY(removeFilename.isValid());
 
     QVERIFY(messageValidator.success());
index abae5d8..29efbe4 100644 (file)
@@ -182,7 +182,7 @@ void tst_XmlPatterns::xquerySupport()
         {
             /* There's a wide range of different version strings used. For
              * instance, "4.4.0-rc1". */
-            const QRegExp removeVersion(QLatin1String(" Qt \\d\\.\\d.*"));
+            QRegExp removeVersion(QLatin1String(" Qt \\d\\.\\d.*"));
             QVERIFY(removeVersion.isValid());
             QCOMPARE(QString(fixedStderr).remove(removeVersion) + QChar('|'), rawExpectedStdErr + QChar('|'));
         }
index 62bd3e2..008d905 100644 (file)
@@ -824,7 +824,7 @@ QVariant QApplicationArgumentParser::convertToValue(const QApplicationArgument &
         }
         case QVariant::RegExp:
         {
-            const QRegExp exp(input);
+            QRegExp exp(input);
 
             if(exp.isValid())
                 return QVariant(exp);