Skip to content

Commit

Permalink
fix(parser): Ensure error spans are valid for escape sequences
Browse files Browse the repository at this point in the history
After a `\` in a string, the parser consumes the next character and
then validates it.
If the validation fails, it means that the parser is now pointing to the
next byte after it which could be within a byte sequence.

We likely want to fix up some of these cases so errors point to the
right thing but in the mean time, we should make sure this doesn't
produce a bad span.
  • Loading branch information
epage committed Apr 19, 2024
1 parent 90471b0 commit f4f7eb1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
5 changes: 5 additions & 0 deletions crates/toml_edit/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ impl TomlError {
let raw = String::from_utf8(raw.to_owned()).expect("original document was utf8");

let offset = error.offset();
let offset = (0..=offset)
.rev()
.find(|index| raw.is_char_boundary(*index))
.unwrap_or(0);

let mut indices = raw[offset..].char_indices();
indices.next();
let len = if let Some((index, _)) = indices.next() {
Expand Down
9 changes: 9 additions & 0 deletions crates/toml_edit/tests/testsuite/invalid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,12 @@ fn text_error_span() {
let actual = &input[err.span().unwrap()];
assert_eq!(actual, "");
}

#[test]
fn fuzzed_68144_error_span() {
let input = "\"\\ᾂr\"";
let err = input.parse::<toml_edit::DocumentMut>().unwrap_err();
dbg!(err.span());
let actual = &input[err.span().unwrap()];
assert_eq!(actual, "ᾂ");
}

0 comments on commit f4f7eb1

Please sign in to comment.