From abddfe7b1481ca43f336567eaff51320fb62d524 Mon Sep 17 00:00:00 2001
From: David Schulz <david.schulz@digia.com>
Date: Tue, 6 Nov 2012 11:34:31 +0100
Subject: [PATCH] Editor: Fix infinite loop in BaseTextFind

Searching in a blockselection for the regular expressions
start of line '^' or line end '$' results in an infinite loop.

Only occures when the blockselection didn't contain the searched
regular expression.

Task-number: QTCREATORBUG-8159
Change-Id: I36412387ecce381300b75d0cd0a452ce5bf1094e
Reviewed-by: Eike Ziller <eike.ziller@digia.com>
---
 src/plugins/find/basetextfind.cpp | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/src/plugins/find/basetextfind.cpp b/src/plugins/find/basetextfind.cpp
index c89bdd08f48..e567769d1e2 100644
--- a/src/plugins/find/basetextfind.cpp
+++ b/src/plugins/find/basetextfind.cpp
@@ -338,7 +338,18 @@ QTextCursor BaseTextFind::findOne(const QRegExp &expr, const QTextCursor &from,
                                   Q_ARG(QTextCursor, candidate));
         if (inVerticalFindScope)
             return candidate;
-        candidate = document()->find(expr, candidate, options);
+
+        QTextCursor newCandidate = document()->find(expr, candidate, options);
+        if (newCandidate == candidate) {
+            // When searching for regular expressions that match "zero length" strings (like ^ or \b)
+            // we need to move away from the match before searching for the next one.
+            candidate.movePosition(options & QTextDocument::FindBackward
+                                   ? QTextCursor::PreviousCharacter
+                                   : QTextCursor::NextCharacter);
+            candidate = document()->find(expr, candidate, options);
+        } else {
+            candidate = newCandidate;
+        }
     }
     return candidate;
 }
-- 
GitLab