https://bugs.kde.org/show_bug.cgi?id=528753
Bug ID: 528753
Summary: Crash in QSyntaxHighlighter::rehighlightBlock(): the
QTextBlock queued by
SyntaxHighlighter::highlightBlock() goes stale when
the text is replaced
Classification: Frameworks and Libraries
Product: frameworks-syntax-highlighting
Version First 6.31.0
Reported In:
Platform: Fedora RPMs
OS: Linux
Status: REPORTED
Severity: crash
Priority: NOR
Component: framework
Assignee: [email protected]
Reporter: [email protected]
Target Milestone: ---
Created attachment 198400
--> https://bugs.kde.org/attachment.cgi?id=198400&action=edit
Standalone QML reproducer (run with QT_QPA_PLATFORM=offscreen qml crash.qml)
SUMMARY
When SyntaxHighlighter::highlightBlock() finds that a block's end state or
folding regions changed, it queues a re-highlight of the next block:
QMetaObject::invokeMethod(this, "rehighlightBlock", Qt::QueuedConnection,
Q_ARG(QTextBlock, nextBlock));
A QTextBlock is only a document pointer and a fragment index, so it does not
survive changes to the document's block structure. If the text is replaced (for
example with setPlainText()) before the queued call is delivered,
rehighlightBlock() receives a stale block. QSyntaxHighlighter's isValid() and
document() checks still pass, and QTextCursor(block) reads freed memory in
QTextBlock::position().
A common way to hit it: change the definition while the old text is still in
the document (setDefinition() rehighlights the existing blocks, which queues
these calls), then replace the text. The QML SyntaxHighlighter does exactly
this when both `definition` and the TextEdit's `text` are bound to the file
being shown and the file changes.
The queued call was added in 7ec03321 ("Make partial re-highlighting work with
the QSyntaxHighlighter", 2016) and is unchanged on master.
STEPS TO REPRODUCE
1. Save the attached crash.qml. It only needs QtQuick and
org.kde.syntaxhighlighting.
2. Run: QT_QPA_PLATFORM=offscreen qml crash.qml
(the binary is qml-qt6 on Fedora)
It switches a TextEdit between 400 lines of plain text and a short Markdown
text, updating the SyntaxHighlighter definition first and the text second, once
per millisecond.
OBSERVED RESULT
Segmentation fault within a few switches (5 of 5 runs here):
#0 QTextBlock::position() const (libQt6Gui.so.6)
#1 QTextCursor::QTextCursor(QTextBlock const&) (libQt6Gui.so.6)
#2 QSyntaxHighlighter::rehighlightBlock(QTextBlock const&)
(libQt6Gui.so.6)
#3 QObject::event(QEvent*) (libQt6Core.so.6)
#4 QCoreApplication::notifyInternal2(QObject*, QEvent*) (libQt6Core.so.6)
#5 QCoreApplicationPrivate::sendPostedEvents(QObject*, int, QThreadData*)
(libQt6Core.so.6)
#6 postEventSourceDispatch (libQt6Core.so.6)
...
#11 QEventLoop::exec (libQt6Core.so.6)
The same crash happened in a real application (a file previewer that reuses one
TextEdit and SyntaxHighlighter while moving between files).
EXPECTED RESULT
No crash. A queued re-highlight for a block that no longer exists should be
skipped or resolved against the current document.
If the text is set before the definition changes, the reproducer does not
crash, which points at the queued block rather than the highlighting itself.
SOFTWARE/OS VERSIONS
Linux/KDE Plasma: Fedora 44, Plasma 6.8.2 (Wayland)
KDE Frameworks Version: 6.31.0
Qt Version: 6.11.2
ADDITIONAL INFORMATION
A possible fix is to queue the block number instead of the QTextBlock and look
the block up again when the call runs:
const int next = nextBlock.blockNumber();
QMetaObject::invokeMethod(this, [this, next] {
if (auto doc = document()) {
const QTextBlock block = doc->findBlockByNumber(next);
if (block.isValid()) {
rehighlightBlock(block);
}
}
}, Qt::QueuedConnection);
If the document changed in between, the worst case is re-highlighting a block
that did not need it. I am preparing a merge request with this change and an
autotest.
--
You are receiving this mail because:
You are watching all bug changes.