From 194a6af1234039122e455a22f5728b327707db9b Mon Sep 17 00:00:00 2001 From: "Felix S. Klock II" Date: Thu, 30 Apr 2015 10:23:50 +0200 Subject: [PATCH] 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. --- src/libsyntax/codemap.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/libsyntax/codemap.rs b/src/libsyntax/codemap.rs index 5e0cb647c8b41..95efc0c03df03 100644 --- a/src/libsyntax/codemap.rs +++ b/src/libsyntax/codemap.rs @@ -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,