Skip to content

Commit 83d2cbb

Browse files
committed
fix!: Panic if Patch span is beyond the end of buffer
1 parent b461247 commit 83d2cbb

File tree

2 files changed

+15
-21
lines changed

2 files changed

+15
-21
lines changed

src/renderer/source_map.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,19 @@ impl<'a> SourceMap<'a> {
379379
}
380380
line_count
381381
}
382+
383+
let source_len = self.source.len();
384+
if let Some(bigger) = patches.iter().find_map(|x| {
385+
// Allow patching one past the last character in the source.
386+
if source_len + 1 < x.span.end {
387+
Some(&x.span)
388+
} else {
389+
None
390+
}
391+
}) {
392+
panic!("Patch span `{bigger:?}` is beyond the end of buffer `{source_len}`")
393+
}
394+
382395
// Assumption: all spans are in the same file, and all spans
383396
// are disjoint. Sort in ascending order.
384397
patches.sort_by_key(|p| p.span.start);

tests/formatter.rs

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2706,6 +2706,7 @@ help: or use `IntoIterator::into_iter(..)` instead of `.into_iter()` to explicit
27062706
}
27072707

27082708
#[test]
2709+
#[should_panic = "Patch span `47..47` is beyond the end of buffer `45`"]
27092710
fn suggestion_span_bigger_than_source() {
27102711
let snippet_source = r#"#![allow(unused)]
27112712
fn main() {
@@ -2749,26 +2750,6 @@ fn main() {
27492750
),
27502751
];
27512752

2752-
let expected = str![[r#"
2753-
warning: this method call resolves to `<&[T; N] as IntoIterator>::into_iter` (due to backwards compatibility), but will resolve to `<[T; N] as IntoIterator>::into_iter` in Rust 2021
2754-
--> lint_example.rs:3:11
2755-
|
2756-
3 | [1, 2, 3].into_iter().for_each(|n| { *n; });
2757-
| ^^^^^^^^^
2758-
|
2759-
= warning: this changes meaning in Rust 2021
2760-
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/IntoIterator-for-arrays.html>
2761-
= note: `#[warn(array_into_iter)]` on by default
2762-
help: use `.iter()` instead of `.into_iter()` to avoid ambiguity
2763-
|
2764-
3 - [1, 2, 3].into_iter().for_each(|n| { *n; });
2765-
3 + [1, 2, 3].iter().for_each(|n| { *n; });
2766-
|
2767-
help: or use `IntoIterator::into_iter(..)` instead of `.into_iter()` to explicitly iterate by value
2768-
|
2769-
3 | IntoIterator::into_iter(
2770-
|
2771-
"#]];
27722753
let renderer = Renderer::plain();
2773-
assert_data_eq!(renderer.render(input), expected);
2754+
renderer.render(input);
27742755
}

0 commit comments

Comments
 (0)