about summary refs log tree commit diff
path: root/compiler
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2020-09-13 11:08:41 +0000
committerbors <bors@rust-lang.org>2020-09-13 11:08:41 +0000
commit17d32770649fefe8efe6510f7dbff6123f544ba6 (patch)
treeb135999cff4666b2b182c288f85dda8ae5f53cbc /compiler
parentb6c84553c4fa47174d2510541a90243000fb44d8 (diff)
parent62068a59eea44ff78c4293d5fcc676279b1deab0 (diff)
downloadrust-17d32770649fefe8efe6510f7dbff6123f544ba6.tar.gz
rust-17d32770649fefe8efe6510f7dbff6123f544ba6.zip
Auto merge of #76598 - ad-anssi:diagnostic_errors_fix, r=estebank
Fixing memory exhaustion when formatting short code suggestion

Details can be found in issue #76597. This PR replaces substractions with `saturating_sub`'s to avoid usize wrapping leading to memory exhaustion when formatting short suggestion messages.
Diffstat (limited to 'compiler')
-rw-r--r--compiler/rustc_errors/src/emitter.rs6
-rw-r--r--compiler/rustc_parse/src/parser/mod.rs6
2 files changed, 8 insertions, 4 deletions
diff --git a/compiler/rustc_errors/src/emitter.rs b/compiler/rustc_errors/src/emitter.rs
index 5a654e83aed..4555168af0a 100644
--- a/compiler/rustc_errors/src/emitter.rs
+++ b/compiler/rustc_errors/src/emitter.rs
@@ -959,15 +959,15 @@ impl EmitterWriter {
                         '_',
                         line_offset + pos,
                         width_offset + depth,
-                        code_offset + annotation.start_col - left,
+                        (code_offset + annotation.start_col).saturating_sub(left),
                         style,
                     );
                 }
                 _ if self.teach => {
                     buffer.set_style_range(
                         line_offset,
-                        code_offset + annotation.start_col - left,
-                        code_offset + annotation.end_col - left,
+                        (code_offset + annotation.start_col).saturating_sub(left),
+                        (code_offset + annotation.end_col).saturating_sub(left),
                         style,
                         annotation.is_primary,
                     );
diff --git a/compiler/rustc_parse/src/parser/mod.rs b/compiler/rustc_parse/src/parser/mod.rs
index 5eefae3af60..7340c574480 100644
--- a/compiler/rustc_parse/src/parser/mod.rs
+++ b/compiler/rustc_parse/src/parser/mod.rs
@@ -694,9 +694,13 @@ impl<'a> Parser<'a> {
                                 Ok(t) => {
                                     // Parsed successfully, therefore most probably the code only
                                     // misses a separator.
+                                    let mut exp_span = self.sess.source_map().next_point(sp);
+                                    if self.sess.source_map().is_multiline(exp_span) {
+                                        exp_span = sp;
+                                    }
                                     expect_err
                                         .span_suggestion_short(
-                                            self.sess.source_map().next_point(sp),
+                                            exp_span,
                                             &format!("missing `{}`", token_str),
                                             token_str,
                                             Applicability::MaybeIncorrect,