highlight: Bound zero-width tokenizer loop

cbdbfe7a20bd07a378629ce6e3b6de3e95c33064
Mutually recursive zero-width begin/end rules could nest at one
position indefinitely without consuming input. Bail forward after
too many consecutive zero-width events.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Alexis Sellier committed ago 1 parent a10a38f0
crates/highlight/src/grammar/tokenize.rs +18 -1
7 7
use super::end::EndRegexCache;
8 8
use super::pattern::{InsideEvent, Match, OpenRule, Pattern, PatternSearch};
9 9
use super::selector::InjectionPriority;
10 10
use super::*;
11 11
12 +
/// Maximum consecutive grammar events allowed without consuming source text.
13 +
///
14 +
/// Zero-width begin and end rules can legitimately nest at one position, but
15 +
/// a malformed or mutually recursive pair must not trap the tokenizer there.
16 +
const MAX_ZERO_WIDTH_EVENTS: usize = MAX_INCLUDE_DEPTH * 2;
17 +
12 18
/// Reusable line tokenizer for one compiled grammar.
13 19
///
14 20
/// The tokenizer keeps runtime scratch that is not part of immutable grammar
15 21
/// data, such as dynamically resolved end regexes. Reuse one tokenizer across
16 22
/// a blob or visible range to avoid recompiling those regexes unnecessarily.
62 68
        line.push_str(visible);
63 69
        line.push('\n');
64 70
        let mut anchors = AnchorState::new(state.first_line);
65 71
        let mut pos = self.check_while_conditions(state, &line, spans, &mut anchors);
66 72
73 +
        let mut zero_width_events = 0;
67 74
        while pos < line.len() {
68 -
            pos = self.advance(state, &line, pos, spans, &mut anchors);
75 +
            let next = self.advance(state, &line, pos, spans, &mut anchors);
76 +
            if next == pos {
77 +
                zero_width_events += 1;
78 +
                if zero_width_events >= MAX_ZERO_WIDTH_EVENTS {
79 +
                    pos = next_char_boundary(&line, pos.saturating_add(1));
80 +
                    zero_width_events = 0;
81 +
                }
82 +
            } else {
83 +
                pos = next;
84 +
                zero_width_events = 0;
85 +
            }
69 86
        }
70 87
        self.close_empty_rules_at_end(state, &line, spans, &mut anchors);
71 88
        clamp_spans(spans, visible_len);
72 89
        state.first_line = false;
73 90
    }
crates/highlight/src/tests.rs +25 -0
652 652
653 653
    assert!(state.is_empty());
654 654
    assert!(scopes.contains(&"keyword.inner.test"));
655 655
}
656 656
657 +
#[test]
658 +
fn zero_width_nested_cycle_makes_forward_progress() {
659 +
    let grammar = Grammar::from_str(
660 +
        r#"{
661 +
            "name":"Test",
662 +
            "scopeName":"source.test",
663 +
            "patterns":[
664 +
                {
665 +
                    "begin":"a",
666 +
                    "end":"z",
667 +
                    "patterns":[{"begin":"(?=b)","end":"(?=b)"}]
668 +
                }
669 +
            ],
670 +
            "repository":{}
671 +
        }"#,
672 +
    )
673 +
    .unwrap();
674 +
    let mut state = LineState::default();
675 +
676 +
    let _ = grammar.tokenize_line(&mut state, "ab");
677 +
    let _ = grammar.tokenize_line(&mut state, "z");
678 +
679 +
    assert!(state.is_empty());
680 +
}
681 +
657 682
#[test]
658 683
fn while_rules_emit_while_captures_on_continued_lines() {
659 684
    let grammar = Grammar::from_str(
660 685
        r#"{
661 686
            "name":"Test",