Skip to content

Commit

Permalink
Merge pull request #715 from epage/fuzz
Browse files Browse the repository at this point in the history
fix(parser): Ensure error spans are valid for escape sequences
  • Loading branch information
epage committed Apr 19, 2024
2 parents 90471b0 + f4f7eb1 commit ed26084
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 ed26084

Please sign in to comment.