From 4f7b0fa9fc92f00fd3f89fd27ec51104f12da50a Mon Sep 17 00:00:00 2001 From: visualfc Date: Wed, 21 Oct 2026 14:52:40 +0800 Subject: [PATCH] texteditor: prevent infinite syntax highlighting rule loops --- .../generichighlighter/highlighter.cpp | 37 +++++- .../generichighlighter/highlighter.h | 6 +- .../qtc_texteditor/tests/highlighter.pro | 15 +++ .../qtc_texteditor/tests/tst_highlighter.cpp | 125 ++++++++++++++++++ 4 files changed, 178 insertions(+), 5 deletions(-) create mode 100644 liteidex/src/3rdparty/qtc_texteditor/tests/highlighter.pro create mode 100644 liteidex/src/3rdparty/qtc_texteditor/tests/tst_highlighter.cpp diff --git a/liteidex/src/3rdparty/qtc_texteditor/generichighlighter/highlighter.cpp b/liteidex/src/3rdparty/qtc_texteditor/generichighlighter/highlighter.cpp index e2c465a0b..e35f226fc 100644 --- a/liteidex/src/3rdparty/qtc_texteditor/generichighlighter/highlighter.cpp +++ b/liteidex/src/3rdparty/qtc_texteditor/generichighlighter/highlighter.cpp @@ -105,6 +105,8 @@ void Highlighter::highlightBlock(const QString &text) m_currentContext->definition()); ProgressData progress; + m_ruleProgressOffset = -1; + m_ruleProgressCount = 0; const int length = text.length(); m_lastRegionChanged = -1; while (progress.offset() < length) @@ -125,6 +127,7 @@ void Highlighter::highlightBlock(const QString &text) } } catch (const HighlighterException &) { m_isBroken = true; + m_contexts.clear(); } } @@ -205,12 +208,37 @@ void Highlighter::setupFromPersistent() setCurrentBlockState(previousBlockState()); } +void Highlighter::checkRuleProgress(const ProgressData *progress) +{ + // Look-ahead and empty matches may legitimately change contexts without + // consuming text. Bound that work per offset, including recursive calls. + if (progress->offset() > m_ruleProgressOffset) { + m_ruleProgressOffset = progress->offset(); + m_ruleProgressCount = 0; + } + if (++m_ruleProgressCount > 1024) { + qWarning() << "Syntax highlighter stopped: rules made no progress at block" + << currentBlock().blockNumber() << "offset" << progress->offset() + << "context" << m_currentContext->name(); + throw HighlighterException(); + } +} + void Highlighter::iterateThroughRules(const QString &text, const int length, ProgressData *progress, const bool childRule, - const QList > &rules) -{ + const QList > &rules, + const int depth) +{ + // Fallthrough and child rules can recurse before returning to the block loop. + if (depth > 128) { + qWarning() << "Syntax highlighter stopped: rule recursion limit at block" + << currentBlock().blockNumber() << "offset" << progress->offset() + << "context" << m_currentContext->name(); + throw HighlighterException(); + } + checkRuleProgress(progress); typedef QList >::const_iterator RuleIterator; bool contextChanged = false; @@ -231,6 +259,7 @@ void Highlighter::iterateThroughRules(const QString &text, } if (rule->matchSucceed(text, length, progress)) { + checkRuleProgress(progress); atLeastOneMatch = true; if (!m_stringOrComment) { if (!rule->beginRegion().isEmpty()) { @@ -277,7 +306,7 @@ void Highlighter::iterateThroughRules(const QString &text, progress->setWillContinueLine(false); } else { if (rule->hasChildren()) - iterateThroughRules(text, length, progress, true, rule->children()); + iterateThroughRules(text, length, progress, true, rule->children(), depth + 1); if (!rule->context().isEmpty() && contextChangeRequired(rule->context())) { m_currentCaptures = progress->captures(); @@ -313,7 +342,7 @@ void Highlighter::iterateThroughRules(const QString &text, if (m_currentContext->isFallthrough()) { handleContextChange(m_currentContext->fallthroughContext(), m_currentContext->definition()); - iterateThroughRules(text, length, progress, false, m_currentContext->rules()); + iterateThroughRules(text, length, progress, false, m_currentContext->rules(), depth + 1); } else { applyFormat(progress->offset(), 1, m_currentContext->itemData(), m_currentContext->definition()); diff --git a/liteidex/src/3rdparty/qtc_texteditor/generichighlighter/highlighter.h b/liteidex/src/3rdparty/qtc_texteditor/generichighlighter/highlighter.h index f0523b408..e3b3493fe 100644 --- a/liteidex/src/3rdparty/qtc_texteditor/generichighlighter/highlighter.h +++ b/liteidex/src/3rdparty/qtc_texteditor/generichighlighter/highlighter.h @@ -79,12 +79,14 @@ class Highlighter : public TextEditor::SyntaxHighlighter void setupFromWillContinue(); void setupFromContinued(); void setupFromPersistent(); + void checkRuleProgress(const ProgressData *progress); void iterateThroughRules(const QString &text, const int length, ProgressData *progress, const bool childRule, - const QList > &rules); + const QList > &rules, + const int depth = 0); void assignCurrentContext(); bool contextChangeRequired(const QString &contextName) const; @@ -166,6 +168,8 @@ class Highlighter : public TextEditor::SyntaxHighlighter int m_dynamicContextsCounter; bool m_isBroken; + int m_ruleProgressOffset; + int m_ruleProgressCount; bool m_stringOrComment; TextFormatId m_currentFormat; diff --git a/liteidex/src/3rdparty/qtc_texteditor/tests/highlighter.pro b/liteidex/src/3rdparty/qtc_texteditor/tests/highlighter.pro new file mode 100644 index 000000000..26843e6c2 --- /dev/null +++ b/liteidex/src/3rdparty/qtc_texteditor/tests/highlighter.pro @@ -0,0 +1,15 @@ +TEMPLATE = app +TARGET = tst_highlighter +QT += widgets testlib printsupport +greaterThan(QT_MAJOR_VERSION, 5): QT += core5compat +CONFIG += testcase console c++11 +CONFIG -= app_bundle + +INCLUDEPATH += .. ../../../utils ../../../api +SOURCES += tst_highlighter.cpp ../syntaxhighlighter.cpp ../basetextdocumentlayout.cpp ../tabsettings.cpp +HEADERS += ../syntaxhighlighter.h ../basetextdocumentlayout.h +HEADERS += ../generichighlighter/highlighter.h + +for(source, $$list(highlighter context highlightdefinition rule specificrules progressdata dynamicrule keywordlist itemdata includerulesinstruction)) { + SOURCES += ../generichighlighter/$${source}.cpp +} diff --git a/liteidex/src/3rdparty/qtc_texteditor/tests/tst_highlighter.cpp b/liteidex/src/3rdparty/qtc_texteditor/tests/tst_highlighter.cpp new file mode 100644 index 000000000..67cd606a3 --- /dev/null +++ b/liteidex/src/3rdparty/qtc_texteditor/tests/tst_highlighter.cpp @@ -0,0 +1,125 @@ +#include +#include +#include "generichighlighter/highlighter.h" +#include "generichighlighter/highlightdefinition.h" +#include "generichighlighter/specificrules.h" +#include "generichighlighter/itemdata.h" + +using namespace TextEditor::Internal; + +class TestHighlighter : public Highlighter +{ +public: + TestHighlighter(QTextDocument *document) : Highlighter(document) {} + bool isBroken() const { return m_isBroken; } + bool hasContexts() const { return !m_contexts.isEmpty(); } +}; + +static QSharedPointer addContext(const QSharedPointer &definition, + const QString &name, bool initial = false) +{ + QSharedPointer context = definition->createContext(name, initial); + context->setDefinition(definition); + context->setItemData("Normal"); + context->setLineEndContext("#stay"); + return context; +} + +static QSharedPointer addRule(const QSharedPointer &context, + const QString &target, bool lookAhead = true) +{ + QSharedPointer rule(new DetectCharRule); + rule->setChar("x"); + rule->setContext(target); + rule->setLookAhead(lookAhead ? "true" : "false"); + rule->setItemData("Normal"); + rule->setDefinition(context->definition()); + context->addRule(rule); + return rule; +} + +class HighlighterTest : public QObject +{ + Q_OBJECT +private slots: + void ruleLoops_data() + { + QTest::addColumn("mode"); + QTest::addColumn("broken"); + QTest::newRow("lookahead-stay") << QString("stay") << true; + QTest::newRow("lookahead-base-pop") << QString("pop") << true; + QTest::newRow("empty-string-match") << QString("empty") << true; + QTest::newRow("lookahead-context-cycle") << QString("cycle") << true; + QTest::newRow("fallthrough-stay") << QString("fallthrough") << true; + QTest::newRow("fallthrough-context-cycle") << QString("fallthrough-cycle") << true; + QTest::newRow("recursive-children") << QString("children") << true; + QTest::newRow("consuming-rule") << QString("consume") << false; + QTest::newRow("valid-lookahead") << QString("valid") << false; + } + + void ruleLoops() + { + QFETCH(QString, mode); + QFETCH(bool, broken); + QSharedPointer definition(new HighlightDefinition); + definition->createItemData("Normal")->setStyle("dsNormal"); + definition->itemData("Normal")->setColor("#ff0000"); + QSharedPointer base = addContext(definition, "base", true); + if (mode == "fallthrough" || mode == "fallthrough-cycle") { + base->setFallthrough("true"); + base->setFallthroughContext(mode == "fallthrough" ? "#stay" : "other"); + if (mode == "fallthrough-cycle") { + QSharedPointer other = addContext(definition, "other"); + other->setFallthrough("true"); + other->setFallthroughContext("#pop"); + } + } else if (mode == "cycle" || mode == "valid") { + QSharedPointer other = addContext(definition, "other"); + addRule(base, "other"); + addRule(other, "#pop", mode == "cycle"); + } else if (mode == "empty") { + QSharedPointer rule(new StringDetectRule); + rule->setString(""); + rule->setInsensitive("false"); + rule->setContext("#stay"); + rule->setDefinition(definition); + base->addRule(rule); + } else if (mode == "children") { + QSharedPointer parent = addRule(base, "#stay"); + for (int i = 0; i < 140; ++i) { + QSharedPointer child(new DetectCharRule); + child->setChar("x"); + child->setLookAhead("true"); + child->setDefinition(definition); + parent->addChild(child); + parent = child; + } + } else { + addRule(base, mode == "pop" ? "#pop" : "#stay", mode != "consume"); + } + + QTextDocument document; + // More characters than the no-progress budget verifies that consuming + // matches reset the budget rather than disabling normal long lines. + const QString text = QString(4096, QLatin1Char('x')) + "\nx"; + document.setPlainText(text); + TestHighlighter highlighter(&document); + highlighter.setDefaultContext(base); + highlighter.rehighlight(); + QCOMPARE(highlighter.isBroken(), broken); + QVERIFY(!highlighter.hasContexts()); + QCOMPARE(document.toPlainText(), text); + if (!broken) { + const auto formats = document.begin().layout()->formats(); + QVERIFY(!formats.isEmpty()); + QCOMPARE(formats.first().start, 0); + QCOMPARE(formats.first().length, 4096); + QCOMPARE(formats.first().format.foreground().color(), QColor(Qt::red)); + } + highlighter.rehighlight(); + QCOMPARE(highlighter.isBroken(), broken); + } +}; + +QTEST_MAIN(HighlighterTest) +#include "tst_highlighter.moc"