Skip to content

Commit 85effab

Browse files
authoredAug 2, 2023
fix(utils): Avoid pre_context and context_line overlap if frame lineno is out of bounds (#8722)
1 parent 4b383d0 commit 85effab

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed
 

‎packages/utils/src/misc.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ export function addContextToFrame(lines: string[], frame: StackFrame, linesOfCon
153153
}
154154

155155
const maxLines = lines.length;
156-
const sourceLine = Math.max(Math.min(maxLines, frame.lineno - 1), 0);
156+
const sourceLine = Math.max(Math.min(maxLines - 1, frame.lineno - 1), 0);
157157

158158
frame.pre_context = lines
159159
.slice(Math.max(0, sourceLine - linesOfContext), sourceLine)

‎packages/utils/test/misc.test.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,20 @@ describe('addContextToFrame', () => {
181181
lineno: 999,
182182
};
183183
addContextToFrame(lines, frame);
184-
expect(frame.pre_context).toEqual(['10: j', '11: k', '12: l', '13: m', '14: n']);
184+
expect(frame.pre_context).toEqual(['9: i', '10: j', '11: k', '12: l', '13: m']);
185185
expect(frame.context_line).toEqual('14: n');
186186
expect(frame.post_context).toEqual([]);
187187
});
188+
189+
test('truncate line if too long', () => {
190+
const frame: StackFrame = {
191+
lineno: 1,
192+
};
193+
addContextToFrame(['1234567890'.repeat(100)], frame);
194+
expect(frame.pre_context).toEqual([]);
195+
expect(frame.context_line).toEqual(`${'1234567890'.repeat(14)} {snip}`);
196+
expect(frame.post_context).toEqual([]);
197+
});
188198
});
189199

190200
describe('addExceptionMechanism', () => {

0 commit comments

Comments
 (0)
Please sign in to comment.