Skip to content

Commit

Permalink
[BugFix]: fix to catch panic when parsing inner portion of a string (#…
Browse files Browse the repository at this point in the history
…426)

* [BugFix]: fix to catch panic when parseing inner portion of a string

* [Misc]: adding to parse_string test case to validate fix
  • Loading branch information
joshfried-aws authored Nov 30, 2023
1 parent 7f06a0b commit 05f4793
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 9 deletions.
45 changes: 36 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions guard/src/rules/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,15 @@ fn parse_string_inner(ch: char) -> impl Fn(Span) -> IResult<Span, Value> {
if frag.ends_with('\\') {
completed.push_str(frag.slice(0..frag.len() - 1));
completed.push(ch);

if remainder.is_empty() {
return Err(nom::Err::Error(ParserError {
context: String::from("Could not parse string"),
kind: ErrorKind::Char,
span: input,
}));
}

span = remainder.slice(1..);
continue;
}
Expand Down
4 changes: 4 additions & 0 deletions guard/src/rules/parser_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ fn test_parse_string() {
parse_string(from_str2(s)),
Ok((cmp, Value::String("Hi there".to_string())))
);

let s = r#""\"#;
let res = parse_string(from_str2(s));
assert!(res.is_err());
}

#[test]
Expand Down

0 comments on commit 05f4793

Please sign in to comment.