fix: Add bounds checks to codemirror code folding extension and other places to avoid crashes.

This commit is contained in:
bkellam 2025-07-23 16:32:39 -07:00
parent ea655f4d4a
commit f0fd44fac1
2 changed files with 21 additions and 4 deletions

View file

@ -308,6 +308,17 @@ const createDecorations = (state: EditorState, foldingState: FoldingState): Deco
// Create decorations for each hidden region
foldingState.hiddenRegions.forEach((region, index) => {
// Catch cases where the region is outside the document bounds.
if (
region.startLine < 1 ||
region.startLine > state.doc.lines ||
region.endLine < 1 ||
region.endLine > state.doc.lines
) {
return;
}
const from = state.doc.line(region.startLine).from;
const to = state.doc.line(region.endLine).to;
const hiddenLineCount = region.endLine - region.startLine + 1;

View file

@ -147,10 +147,11 @@ export const ReferencedSourcesListView = ({
// If we have a range, we can scroll to the starting line number.
if (
selectedReference.range
&& editorRef
&& editorRef.view
&& scrollAreaViewport
selectedReference.range &&
editorRef &&
editorRef.view &&
scrollAreaViewport &&
selectedReference.range.startLine <= editorRef.view.state.doc.lines
) {
const view = editorRef.view;
const lineNumber = selectedReference.range.startLine;
@ -393,6 +394,11 @@ const CodeMirrorCodeBlock = ({
const isSelected = id === selectedReference?.id;
for (let line = range.startLine; line <= range.endLine; line++) {
// Skip lines that are outside the document bounds.
if (line > state.doc.lines) {
continue;
}
if (isSelected) {
decorations.push(selectedLineDecoration.range(state.doc.line(line).from));
} else {