Fix infinite search loop bug (#1016)

* Make sure the search loop in the editor cannot run infinitely
* Add test coverage of infinite search case
* Run GitHub actions against the patch branch
This commit is contained in:
Veronica Berglyd Olsen
2022-03-16 22:34:29 +01:00
committed by GitHub
parent 7532fdb1fc
commit 1c7708babd
6 changed files with 30 additions and 9 deletions
+2
View File
@@ -4,9 +4,11 @@ on:
push: push:
branches: branches:
- main - main
- patch
pull_request: pull_request:
branches: branches:
- main - main
- patch
jobs: jobs:
checkSyntax: checkSyntax:
+2
View File
@@ -4,9 +4,11 @@ on:
push: push:
branches: branches:
- main - main
- patch
pull_request: pull_request:
branches: branches:
- main - main
- patch
jobs: jobs:
testLinux: testLinux:
+2
View File
@@ -4,9 +4,11 @@ on:
push: push:
branches: branches:
- main - main
- patch
pull_request: pull_request:
branches: branches:
- main - main
- patch
jobs: jobs:
testMac: testMac:
+2
View File
@@ -4,9 +4,11 @@ on:
push: push:
branches: branches:
- main - main
- patch
pull_request: pull_request:
branches: branches:
- main - main
- patch
jobs: jobs:
testWin: testWin:
+10 -4
View File
@@ -1436,10 +1436,16 @@ class GuiDocEditor(QTextEdit):
theCursor.setPosition(0) theCursor.setPosition(0)
self.setTextCursor(theCursor) self.setTextCursor(theCursor)
while self.find(searchFor, findOpt): # Search up to a maximum of 1000, and make sure certain special
# searches like a regex search for .* turns into an infinite loop
while self.find(searchFor, findOpt) and len(resE) <= 1000:
theCursor = self.textCursor() theCursor = self.textCursor()
resS.append(theCursor.selectionStart()) if theCursor.hasSelection():
resE.append(theCursor.selectionEnd()) resS.append(theCursor.selectionStart())
resE.append(theCursor.selectionEnd())
else:
logger.warning("The search returned an empty result")
break
if hasSelection: if hasSelection:
theCursor.setPosition(origA, QTextCursor.MoveAnchor) theCursor.setPosition(origA, QTextCursor.MoveAnchor)
@@ -2410,7 +2416,7 @@ class GuiDocEditSearch(QFrame):
"""Set the count values for the current search. """Set the count values for the current search.
""" """
currRes = "?" if currRes is None else currRes currRes = "?" if currRes is None else currRes
resCount = "?" if resCount is None else resCount resCount = "?" if resCount is None else "1000+" if resCount > 1000 else resCount
minWidth = self.theTheme.getTextWidth(f"{resCount}//{resCount}", self.boxFont) minWidth = self.theTheme.getTextWidth(f"{resCount}//{resCount}", self.boxFont)
self.resultLabel.setText(f"{currRes}/{resCount}") self.resultLabel.setText(f"{currRes}/{resCount}")
self.resultLabel.setMinimumWidth(minWidth) self.resultLabel.setMinimumWidth(minWidth)
+12 -5
View File
@@ -1286,13 +1286,13 @@ def testGuiEditor_Search(qtbot, monkeypatch, nwGUI, nwLipsum):
monkeypatch.setattr(GuiDocEditor, "hasFocus", lambda *a: True) monkeypatch.setattr(GuiDocEditor, "hasFocus", lambda *a: True)
nwGUI.theProject.projTree.setSeed(42) nwGUI.theProject.projTree.setSeed(42)
assert nwGUI.openProject(nwLipsum) assert nwGUI.openProject(nwLipsum) is True
assert nwGUI.openDocument("4c4f28287af27") assert nwGUI.openDocument("4c4f28287af27") is True
origText = nwGUI.docEditor.getText() origText = nwGUI.docEditor.getText()
qtbot.wait(stepDelay) qtbot.wait(stepDelay)
# Select the Word "est" # Select the Word "est"
assert nwGUI.docEditor.setCursorPosition(630) nwGUI.docEditor.setCursorPosition(630)
nwGUI.docEditor._makeSelection(QTextCursor.WordUnderCursor) nwGUI.docEditor._makeSelection(QTextCursor.WordUnderCursor)
theCursor = nwGUI.docEditor.textCursor() theCursor = nwGUI.docEditor.textCursor()
assert theCursor.selectedText() == "est" assert theCursor.selectedText() == "est"
@@ -1331,7 +1331,7 @@ def testGuiEditor_Search(qtbot, monkeypatch, nwGUI, nwLipsum):
assert nwGUI.docEditor.docSearch.isVisible() is True assert nwGUI.docEditor.docSearch.isVisible() is True
# Search for non-existing # Search for non-existing
assert nwGUI.docEditor.setCursorPosition(0) nwGUI.docEditor.setCursorPosition(0)
assert nwGUI.docEditor.docSearch.setSearchText("abcdef") assert nwGUI.docEditor.docSearch.setSearchText("abcdef")
qtbot.mouseClick(nwGUI.docEditor.docSearch.searchButton, Qt.LeftButton, delay=keyDelay) qtbot.mouseClick(nwGUI.docEditor.docSearch.searchButton, Qt.LeftButton, delay=keyDelay)
assert nwGUI.docEditor.getCursorPosition() < 3 # No result assert nwGUI.docEditor.getCursorPosition() < 3 # No result
@@ -1342,11 +1342,18 @@ def testGuiEditor_Search(qtbot, monkeypatch, nwGUI, nwLipsum):
assert nwGUI.docEditor.docSearch.isRegEx is True assert nwGUI.docEditor.docSearch.isRegEx is True
# Set invalid RegEx # Set invalid RegEx
assert nwGUI.docEditor.setCursorPosition(0) nwGUI.docEditor.setCursorPosition(0)
assert nwGUI.docEditor.docSearch.setSearchText(r"\bSus[") assert nwGUI.docEditor.docSearch.setSearchText(r"\bSus[")
qtbot.mouseClick(nwGUI.docEditor.docSearch.searchButton, Qt.LeftButton, delay=keyDelay) qtbot.mouseClick(nwGUI.docEditor.docSearch.searchButton, Qt.LeftButton, delay=keyDelay)
assert nwGUI.docEditor.getCursorPosition() < 3 # No result assert nwGUI.docEditor.getCursorPosition() < 3 # No result
# Set dangerous RegEx (issue #1015)
# If this doesn't get caught, the app will hang
nwGUI.docEditor.setCursorPosition(0)
assert nwGUI.docEditor.docSearch.setSearchText(r".*")
qtbot.mouseClick(nwGUI.docEditor.docSearch.searchButton, Qt.LeftButton, delay=keyDelay)
assert abs(nwGUI.docEditor.getCursorPosition() - 14) < 3
# Set valid RegEx # Set valid RegEx
assert nwGUI.docEditor.docSearch.setSearchText(r"\bSus") assert nwGUI.docEditor.docSearch.setSearchText(r"\bSus")
qtbot.mouseClick(nwGUI.docEditor.docSearch.searchButton, Qt.LeftButton, delay=keyDelay) qtbot.mouseClick(nwGUI.docEditor.docSearch.searchButton, Qt.LeftButton, delay=keyDelay)