From 91bc03a0dc30a013b46b4b3a697fbd426cd0dec1 Mon Sep 17 00:00:00 2001 From: Robert Knight Date: Sat, 28 Apr 2007 11:49:34 +0000 Subject: [PATCH] Abort filter processing if the filter regexp matches the empty string. Fixes filter getting stuck in infinite loop. Use a static const QRegExp for the URL filter regexp since it never changes. svn path=/branches/work/konsole-split-view/; revision=658740 --- konsole/Filter.cpp | 27 ++++++++++++++++----------- konsole/Filter.h | 21 +++++++++++++++++---- 2 files changed, 33 insertions(+), 15 deletions(-) diff --git a/konsole/Filter.cpp b/konsole/Filter.cpp index a5f4e35ff..127387807 100644 --- a/konsole/Filter.cpp +++ b/konsole/Filter.cpp @@ -345,7 +345,7 @@ QStringList RegExpFilter::HotSpot::capturedTexts() const void RegExpFilter::setRegExp(const QRegExp& regExp) { - _searchText = QRegExp(regExp); + _searchText = regExp; } QRegExp RegExpFilter::regExp() const { @@ -362,8 +362,10 @@ void RegExpFilter::process() Q_ASSERT( text ); - // empty regexp does not match - if ( _searchText.isEmpty() ) + // ignore any regular expressions which match an empty string. + // otherwise the while loop below will run indefinitely + static const QString emptyString(""); + if ( _searchText.exactMatch(emptyString) ) return; while(pos >= 0) @@ -394,6 +396,8 @@ void RegExpFilter::process() addHotSpot( spot ); pos += _searchText.matchedLength(); + // if matchedLength == 0, the program will get stuck in an infinite loop + Q_ASSERT( _searchText.matchedLength() > 0 ); } } } @@ -433,9 +437,9 @@ UrlFilter::HotSpot::UrlType UrlFilter::HotSpot::urlType() const { QString url = capturedTexts().first(); - if ( QRegExp(FullUrlRegExp).exactMatch(url) ) + if ( FullUrlRegExp.exactMatch(url) ) return StandardUrl; - else if ( QRegExp(EmailAddressRegExp).exactMatch(url) ) + else if ( EmailAddressRegExp.exactMatch(url) ) return Email; else return Unknown; @@ -480,17 +484,18 @@ void UrlFilter::HotSpot::activate(QObject* object) //regexp matches: // full url: // protocolname:// or www. followed by numbers, letters dots and dashes or the '@' character. -const QString UrlFilter::FullUrlRegExp("([a-z]+://|www\\.)[a-zA-Z0-9@\\-\\./]+"); +const QRegExp UrlFilter::FullUrlRegExp("([a-z]+://|www\\.)[a-zA-Z0-9@\\-\\./]+"); // email address: // [word chars, dots or dashes]@[word chars, dots or dashes].[word chars] -const QString UrlFilter::EmailAddressRegExp("(\\w|\\.|-)+@(\\w|\\.|-)+\\.\\w+"); +const QRegExp UrlFilter::EmailAddressRegExp("(\\w|\\.|-)+@(\\w|\\.|-)+\\.\\w+"); + +// matches full url or email address +const QRegExp UrlFilter::CompleteUrlRegExp('('+FullUrlRegExp.pattern()+'|'+ + EmailAddressRegExp.pattern()+')'); UrlFilter::UrlFilter() { - //FIXME - There is a bug where URLs are not identified if they occur at the very - //end of the output ( the last characters before any trailing whitespace ) - - setRegExp( QRegExp('('+FullUrlRegExp+'|'+EmailAddressRegExp+')') ); + setRegExp( CompleteUrlRegExp ); } UrlFilter::HotSpot::~HotSpot() { diff --git a/konsole/Filter.h b/konsole/Filter.h index 734a09475..bba98d5ce 100644 --- a/konsole/Filter.h +++ b/konsole/Filter.h @@ -211,12 +211,22 @@ public: /** Constructs a new regular expression filter */ RegExpFilter(); - /** Sets the regular expression which the filter searches for in blocks of text */ + /** + * Sets the regular expression which the filter searches for in blocks of text. + * + * Regular expressions which match the empty string are treated as not matching + * anything. + */ void setRegExp(const QRegExp& text); /** Returns the regular expression which the filter searches for in blocks of text */ QRegExp regExp() const; - /** Reimplemented to search the filter's text buffer for text matching regExp() */ + /** + * Reimplemented to search the filter's text buffer for text matching regExp() + * + * If regexp matches the empty string, then process() will return immediately + * without finding results. + */ virtual void process(); protected: @@ -275,8 +285,11 @@ protected: private: - static const QString FullUrlRegExp; - static const QString EmailAddressRegExp; + static const QRegExp FullUrlRegExp; + static const QRegExp EmailAddressRegExp; + + // combined OR of FullUrlRegExp and EmailAddressRegExp + static const QRegExp CompleteUrlRegExp; }; class FilterObject : public QObject