396: Fix the `panic` found whilst fuzzing r=matklad,me a=DJMcNab

This occurred when a non-ascii character was used in an ascii escape, for example in the motivating example: `if'\xɿ`, which can be further simplified to `'\xɿ`.

Co-authored-by: DJMcNab <36049421+djmcnab@users.noreply.github.com>
This commit is contained in:
bors[bot] 2019-01-01 11:11:20 +00:00
commit 5a866a772c
2 changed files with 10 additions and 4 deletions

View File

@ -89,12 +89,17 @@ pub(super) fn is_ascii_escape(code: char) -> bool {
fn validate_ascii_code_escape(text: &str, range: TextRange, errors: &mut Vec<SyntaxError>) {
// An AsciiCodeEscape has 4 chars, example: `\xDD`
if text.len() < 4 {
if !text.is_ascii() {
// TODO: Give a more precise error message (say what the invalid character was)
errors.push(SyntaxError::new(AsciiCodeEscapeOutOfRange, range));
} else if text.chars().count() < 4 {
errors.push(SyntaxError::new(TooShortAsciiCodeEscape, range));
} else {
assert!(
text.chars().count() == 4,
"AsciiCodeEscape cannot be longer than 4 chars"
assert_eq!(
text.chars().count(),
4,
"AsciiCodeEscape cannot be longer than 4 chars, but text '{}' is",
text,
);
match u8::from_str_radix(&text[2..], 16) {

View File

@ -0,0 +1 @@
if'\xɿ