Skip to content

Commit f0fd44f

Browse files
fix: Add bounds checks to codemirror code folding extension and other places to avoid crashes.
1 parent ea655f4 commit f0fd44f

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

packages/web/src/features/chat/components/chatThread/codeFoldingExtension.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,17 @@ const createDecorations = (state: EditorState, foldingState: FoldingState): Deco
308308

309309
// Create decorations for each hidden region
310310
foldingState.hiddenRegions.forEach((region, index) => {
311+
312+
// Catch cases where the region is outside the document bounds.
313+
if (
314+
region.startLine < 1 ||
315+
region.startLine > state.doc.lines ||
316+
region.endLine < 1 ||
317+
region.endLine > state.doc.lines
318+
) {
319+
return;
320+
}
321+
311322
const from = state.doc.line(region.startLine).from;
312323
const to = state.doc.line(region.endLine).to;
313324
const hiddenLineCount = region.endLine - region.startLine + 1;

packages/web/src/features/chat/components/chatThread/referencedSourcesListView.tsx

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,11 @@ export const ReferencedSourcesListView = ({
147147

148148
// If we have a range, we can scroll to the starting line number.
149149
if (
150-
selectedReference.range
151-
&& editorRef
152-
&& editorRef.view
153-
&& scrollAreaViewport
150+
selectedReference.range &&
151+
editorRef &&
152+
editorRef.view &&
153+
scrollAreaViewport &&
154+
selectedReference.range.startLine <= editorRef.view.state.doc.lines
154155
) {
155156
const view = editorRef.view;
156157
const lineNumber = selectedReference.range.startLine;
@@ -393,6 +394,11 @@ const CodeMirrorCodeBlock = ({
393394
const isSelected = id === selectedReference?.id;
394395

395396
for (let line = range.startLine; line <= range.endLine; line++) {
397+
// Skip lines that are outside the document bounds.
398+
if (line > state.doc.lines) {
399+
continue;
400+
}
401+
396402
if (isSelected) {
397403
decorations.push(selectedLineDecoration.range(state.doc.line(line).from));
398404
} else {

0 commit comments

Comments
 (0)