about summary refs log tree commit diff
path: root/src/librustc_errors
diff options
context:
space:
mode:
authorRyan Cumming <etaoins@gmail.com>2018-01-23 19:08:16 +1100
committerRyan Cumming <etaoins@gmail.com>2018-01-23 19:08:16 +1100
commitc6377f8e17cd7f3cf906397f3f48b522b0521471 (patch)
tree1f7f5665069becdbb55ff659f07f8103ab9c09ae /src/librustc_errors
parent47a8eb7c4e24b61a8a9ab4eaff60ef65291aaa56 (diff)
downloadrust-c6377f8e17cd7f3cf906397f3f48b522b0521471.tar.gz
rust-c6377f8e17cd7f3cf906397f3f48b522b0521471.zip
Avoid underflow in render_source_line
While testing rust-lang/rust#47655 I was able to make the compiler panic
when it's compiled with debug assertions:

```shell
> rustc /dev/null --crate-type proc-macro

error: internal compiler error: unexpected panic

note: the compiler unexpectedly panicked. this is a bug.

note: we would appreciate a bug report: https://github.com/rust-lang/rust/blob/master/CONTRIBUTING.md#bug-reports

note: rustc 1.25.0-dev running on x86_64-apple-darwin

note: run with `RUST_BACKTRACE=1` for a backtrace

thread 'rustc' panicked at 'attempt to subtract with overflow', librustc_errors/emitter.rs:287:49
```

Without debug assertions the following warning is emitted:

```shell
> rustc /dev/null --crate-type proc-macro

warning: unused variable: `registrar`
 --> /dev/null:0:1
  |
  |
  = note: #[warn(unused_variables)] on by default
  = note: to avoid this warning, consider using `_registrar` instead
```

The panic is due to the unused variable warning being spanned to
`/dev/null:0:1`. When `render_source_line` subtracts 1 from the line
number to look up the source line it panics due to underflow. Without
debug assertions this would wrap and cause us to return a blank string
instead.

Fix by explicitly testing for 0 and exiting early. I'm unsure how to
automatically test this now that rust-lang/rust#46655 has been approved.
Diffstat (limited to 'src/librustc_errors')
-rw-r--r--src/librustc_errors/emitter.rs4
1 files changed, 4 insertions, 0 deletions
diff --git a/src/librustc_errors/emitter.rs b/src/librustc_errors/emitter.rs
index 58f851aea38..62ae8d85589 100644
--- a/src/librustc_errors/emitter.rs
+++ b/src/librustc_errors/emitter.rs
@@ -284,6 +284,10 @@ impl EmitterWriter {
                           line: &Line,
                           width_offset: usize,
                           code_offset: usize) -> Vec<(usize, Style)> {
+        if line.line_index == 0 {
+            return Vec::new();
+        }
+
         let source_string = match file.get_line(line.line_index - 1) {
             Some(s) => s,
             None => return Vec::new(),