Skip to content

Commit 32ca293

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

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
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: 1 addition & 0 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() {

0 commit comments

Comments
 (0)