Skip to content

Guard against overflow in codemap::span_to_lines. #24976

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/libsyntax/codemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,13 @@ impl CodeMap {
pub fn span_to_lines(&self, sp: Span) -> FileLines {
let lo = self.lookup_char_pos(sp.lo);
let hi = self.lookup_char_pos(sp.hi);

// If you are hitting these errors, perhaps change your
// call-site to call span_to_snippet instead. (Or revise this
// code to return a similar Result...)
assert_eq!(lo.file.name, hi.file.name);
assert!(hi.line >= lo.line);

let mut lines = Vec::with_capacity(hi.line - lo.line + 1);

// The span starts partway through the first line,
Expand Down