Skip to content

Commit 194a6af

Browse files
committed
Guard against overflow in codemap::span_to_lines.
There a number of recent issues that report the bug here. See e.g. #24761 and #24954. This change does *not* fix them; it just makes the assert more immediate (and also injects a more conservative check, in that we are also checking that the files match up). So, this does not directly fix any bugs, and is expected ot actually inject new ICE's. But those will all represent latent bugs that need fixing.
1 parent 551a74d commit 194a6af

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

src/libsyntax/codemap.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,13 @@ impl CodeMap {
670670
pub fn span_to_lines(&self, sp: Span) -> FileLines {
671671
let lo = self.lookup_char_pos(sp.lo);
672672
let hi = self.lookup_char_pos(sp.hi);
673+
674+
// If you are hitting these errors, perhaps change your
675+
// call-site to call span_to_snippet instead. (Or revise this
676+
// code to return a similar Result...)
677+
assert_eq!(lo.file.name, hi.file.name);
678+
assert!(hi.line >= lo.line);
679+
673680
let mut lines = Vec::with_capacity(hi.line - lo.line + 1);
674681

675682
// The span starts partway through the first line,

0 commit comments

Comments
 (0)