Skip to content

Commit

Permalink
fix(utils): Avoid pre_context and context_line overlap if frame l…
Browse files Browse the repository at this point in the history
…ineno is out of bounds (#8722)
  • Loading branch information
Lms24 committed Aug 2, 2023
1 parent 4b383d0 commit 85effab
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/utils/src/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ export function addContextToFrame(lines: string[], frame: StackFrame, linesOfCon
}

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

frame.pre_context = lines
.slice(Math.max(0, sourceLine - linesOfContext), sourceLine)
Expand Down
12 changes: 11 additions & 1 deletion packages/utils/test/misc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,20 @@ describe('addContextToFrame', () => {
lineno: 999,
};
addContextToFrame(lines, frame);
expect(frame.pre_context).toEqual(['10: j', '11: k', '12: l', '13: m', '14: n']);
expect(frame.pre_context).toEqual(['9: i', '10: j', '11: k', '12: l', '13: m']);
expect(frame.context_line).toEqual('14: n');
expect(frame.post_context).toEqual([]);
});

test('truncate line if too long', () => {
const frame: StackFrame = {
lineno: 1,
};
addContextToFrame(['1234567890'.repeat(100)], frame);
expect(frame.pre_context).toEqual([]);
expect(frame.context_line).toEqual(`${'1234567890'.repeat(14)} {snip}`);
expect(frame.post_context).toEqual([]);
});
});

describe('addExceptionMechanism', () => {
Expand Down

0 comments on commit 85effab

Please sign in to comment.