Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR: Prevent segfault when modifying the editor content #18686

Merged
merged 5 commits into from
Jul 20, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions spyder/plugins/editor/panels/scrollflag.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
# Local imports
from spyder.api.panel import Panel
from spyder.plugins.completion.api import DiagnosticSeverity
from spyder.plugins.editor.utils.editor import block_safe


REFRESH_RATE = 1000
Expand Down Expand Up @@ -221,7 +222,7 @@ def paintEvent(self, event):
if editor.verticalScrollBar().maximum() == 0:
# No scroll
for block in dict_flag_lists[flag_type]:
if not block.isValid():
if not block_safe(block):
continue
geometry = editor.blockBoundingGeometry(block)
rect_y = ceil(
Expand All @@ -233,7 +234,7 @@ def paintEvent(self, event):
elif last_line == 0:
# Only one line
for block in dict_flag_lists[flag_type]:
if not block.isValid():
if not block_safe(block):
continue
rect_y = ceil(first_y_pos)
painter.drawRect(rect_x, rect_y, rect_w, rect_h)
Expand All @@ -243,7 +244,7 @@ def paintEvent(self, event):
# If the file is too long, do not freeze the editor
next_line = 0
for block in dict_flag_lists[flag_type]:
if not block.isValid():
if not block_safe(block):
continue
block_line = block.firstLineNumber()
# block_line = -1 if invalid
Expand Down
17 changes: 17 additions & 0 deletions spyder/plugins/editor/utils/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,23 @@ def drift_color(base_color, factor=110):
return base_color.lighter(factor + 10)


def block_safe(block):
impact27 marked this conversation as resolved.
Show resolved Hide resolved
"""
Check if the block is safe to work with.

A BlockUserData must have been set on the block while it was known safe.
impact27 marked this conversation as resolved.
Show resolved Hide resolved

If an editor is cleared by editor.clear() or editor.set_text() for example,
all the old blocks will continue to report block.isValid() == True
but will raise a Segmentation Fault on almost all methods.
impact27 marked this conversation as resolved.
Show resolved Hide resolved

One way to check is that the userData is reset to None or
QTextBlockUserData. So if a block is known to have setUserData to
BlockUserData, this fact can be used to check the block.
impact27 marked this conversation as resolved.
Show resolved Hide resolved
"""
return block.isValid() and isinstance(block.userData(), BlockUserData)


class BlockUserData(QTextBlockUserData):
def __init__(self, editor, color=None, selection_start=None,
selection_end=None):
Expand Down
12 changes: 10 additions & 2 deletions spyder/plugins/editor/widgets/codeeditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2579,7 +2579,11 @@ def __mark_occurrences(self):
extra_selections = self.get_extra_selections('occurrences')
first_occurrence = None
while cursor:
self.occurrences.append(cursor.block())
block = cursor.block()
if not block.userData():
# Add user data to check block validity
block.setUserData(BlockUserData(self))
self.occurrences.append(block)
impact27 marked this conversation as resolved.
Show resolved Hide resolved
selection = self.get_selection(cursor)
if len(selection.cursor.selectedText()) > 0:
extra_selections.append(selection)
Expand Down Expand Up @@ -2623,7 +2627,11 @@ def highlight_found_results(self, pattern, word=False, regexp=False,
selection = TextDecoration(self.textCursor())
selection.format.setBackground(self.found_results_color)
selection.cursor.setPosition(pos1)
self.found_results.append(selection.cursor.block())
block = selection.cursor.block()
if not block.userData():
# Add user data to check block validity
block.setUserData(BlockUserData(self))
self.found_results.append(block)
impact27 marked this conversation as resolved.
Show resolved Hide resolved
selection.cursor.setPosition(pos2, QTextCursor.KeepAnchor)
extra_selections.append(selection)
self.set_extra_selections('find', extra_selections)
Expand Down