Skip to content

Commit 26908f1

Browse files
committed
Fix crash on lexing octal escapes bigger than u8::MAX
1 parent 9676ddb commit 26908f1

1 file changed

Lines changed: 14 additions & 4 deletions

File tree

parser/src/lexer.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ where
447447
}
448448
}
449449

450-
fn parse_octet(&mut self, first: char) -> char {
450+
fn parse_octet(&mut self, first: char) -> Result<char, LexicalError> {
451451
let mut octet_content = String::new();
452452
octet_content.push(first);
453453
while octet_content.len() < 3 {
@@ -457,7 +457,13 @@ where
457457
break;
458458
}
459459
}
460-
u8::from_str_radix(&octet_content, 8).unwrap() as char
460+
match u32::from_str_radix(&octet_content, 8) {
461+
Ok(result) => Ok(char::from_u32(result).unwrap()),
462+
Err(error) => Err(LexicalError {
463+
error: LexicalErrorType::OtherError(error.to_string()),
464+
location: self.get_pos(),
465+
})
466+
}
461467
}
462468

463469
fn parse_unicode_name(&mut self) -> Result<char, LexicalError> {
@@ -549,7 +555,7 @@ where
549555
string_content.push('\t');
550556
}
551557
Some('v') => string_content.push('\x0b'),
552-
Some(o @ '0'..='7') => string_content.push(self.parse_octet(o)),
558+
Some(o @ '0'..='7') => string_content.push(self.parse_octet(o)?),
553559
Some('x') => string_content.push(self.unicode_literal(2)?),
554560
Some('u') if !is_bytes => string_content.push(self.unicode_literal(4)?),
555561
Some('U') if !is_bytes => string_content.push(self.unicode_literal(8)?),
@@ -1596,7 +1602,7 @@ mod tests {
15961602

15971603
#[test]
15981604
fn test_string() {
1599-
let source = r#""double" 'single' 'can\'t' "\\\"" '\t\r\n' '\g' r'raw\'' '\200\0a'"#;
1605+
let source = r#""double" 'single' 'can\'t' "\\\"" '\t\r\n' '\g' r'raw\'' '\420' '\200\0a'"#;
16001606
let tokens = lex_source(source);
16011607
assert_eq!(
16021608
tokens,
@@ -1629,6 +1635,10 @@ mod tests {
16291635
value: String::from("raw\\'"),
16301636
is_fstring: false,
16311637
},
1638+
Tok::String {
1639+
value: String::from("Đ"),
1640+
is_fstring: false,
1641+
},
16321642
Tok::String {
16331643
value: String::from("\u{80}\u{0}a"),
16341644
is_fstring: false,

0 commit comments

Comments
 (0)