Skip to content

Commit ae65ecc

Browse files
committed
Avoid infinite loop when a diagnostic points after the end of the document
FIX: Fix an infinite loop that would occur when a diagnostic pointed beyond the end of the document. Closes 'codemirror/dev#1633
1 parent 818aedf commit ae65ecc

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

src/lint.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ class LintState {
103103

104104
let sorted = diagnostics.slice().sort((a, b) => a.from - b.from || a.to - b.to)
105105
let deco = new RangeSetBuilder<Decoration>(), active: Diagnostic[] = [], pos = 0
106-
let scan = state.doc.iter(), scanPos = 0
106+
let scan = state.doc.iter(), scanPos = 0, docLen = state.doc.length
107107
for (let i = 0;;) {
108108
let next = i == sorted.length ? null : sorted[i]
109109
if (!next && !active.length) break
@@ -113,6 +113,7 @@ class LintState {
113113
to = active.reduce((p, d) => Math.min(p, d.to), next && next.from > from ? next.from : 1e8)
114114
} else {
115115
from = next!.from
116+
if (from > docLen) break
116117
to = next!.to
117118
active.push(next!)
118119
i++
@@ -128,8 +129,9 @@ class LintState {
128129
break
129130
}
130131
}
132+
to = Math.min(to, docLen)
131133
let widget = false
132-
if (active.some(d => d.from == from && d.to == to)) {
134+
if (active.some(d => d.from == from && (d.to == to || to == docLen))) {
133135
widget = from == to
134136
if (!widget && to - from < 10) {
135137
let behind = from - (scanPos + scan.value.length)
@@ -158,6 +160,7 @@ class LintState {
158160
}))
159161
}
160162
pos = to
163+
if (pos == docLen) break
161164
for (let i = 0; i < active.length; i++) if (active[i].to <= pos) active.splice(i--, 1)
162165
}
163166
let set = deco.finish()

0 commit comments

Comments
 (0)