Skip to content

Commit 17d3277

Browse files
committed
Auto merge of rust-lang#76598 - ad-anssi:diagnostic_errors_fix, r=estebank
Fixing memory exhaustion when formatting short code suggestion Details can be found in issue rust-lang#76597. This PR replaces substractions with `saturating_sub`'s to avoid usize wrapping leading to memory exhaustion when formatting short suggestion messages.
2 parents b6c8455 + 62068a5 commit 17d3277

File tree

5 files changed

+43
-4
lines changed

5 files changed

+43
-4
lines changed

compiler/rustc_errors/src/emitter.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -959,15 +959,15 @@ impl EmitterWriter {
959959
'_',
960960
line_offset + pos,
961961
width_offset + depth,
962-
code_offset + annotation.start_col - left,
962+
(code_offset + annotation.start_col).saturating_sub(left),
963963
style,
964964
);
965965
}
966966
_ if self.teach => {
967967
buffer.set_style_range(
968968
line_offset,
969-
code_offset + annotation.start_col - left,
970-
code_offset + annotation.end_col - left,
969+
(code_offset + annotation.start_col).saturating_sub(left),
970+
(code_offset + annotation.end_col).saturating_sub(left),
971971
style,
972972
annotation.is_primary,
973973
);

compiler/rustc_parse/src/parser/mod.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -694,9 +694,13 @@ impl<'a> Parser<'a> {
694694
Ok(t) => {
695695
// Parsed successfully, therefore most probably the code only
696696
// misses a separator.
697+
let mut exp_span = self.sess.source_map().next_point(sp);
698+
if self.sess.source_map().is_multiline(exp_span) {
699+
exp_span = sp;
700+
}
697701
expect_err
698702
.span_suggestion_short(
699-
self.sess.source_map().next_point(sp),
703+
exp_span,
700704
&format!("missing `{}`", token_str),
701705
token_str,
702706
Applicability::MaybeIncorrect,

src/test/ui/issue-76597.fixed

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// run-rustfix
2+
3+
#![allow(dead_code)]
4+
#![allow(unused_variables)]
5+
fn f(
6+
x: u8,
7+
y: u8,
8+
) {}
9+
//~^^ ERROR: expected one of `!`, `(`, `)`, `+`, `,`, `::`, or `<`, found `y`
10+
11+
fn main() {}

src/test/ui/issue-76597.rs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// run-rustfix
2+
3+
#![allow(dead_code)]
4+
#![allow(unused_variables)]
5+
fn f(
6+
x: u8
7+
y: u8,
8+
) {}
9+
//~^^ ERROR: expected one of `!`, `(`, `)`, `+`, `,`, `::`, or `<`, found `y`
10+
11+
fn main() {}

src/test/ui/issue-76597.stderr

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
error: expected one of `!`, `(`, `)`, `+`, `,`, `::`, or `<`, found `y`
2+
--> $DIR/issue-76597.rs:7:38
3+
|
4+
LL | ... x: u8
5+
| -
6+
| |
7+
| expected one of 7 possible tokens
8+
| help: missing `,`
9+
LL | ... y: u8,
10+
| ^ unexpected token
11+
12+
error: aborting due to previous error
13+

0 commit comments

Comments
 (0)