From 623a78dbf9acfc4ce5de441d1fb99098de70fdc2 Mon Sep 17 00:00:00 2001 From: Dennis Zhuang Date: Sun, 19 Jun 2022 22:34:34 +0800 Subject: [PATCH] fix panic when parsing invalid decimal, close #3796 --- extra_tests/snippets/syntax_decimal.py | 6 ++++++ parser/src/lexer.rs | 5 ++++- 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 extra_tests/snippets/syntax_decimal.py diff --git a/extra_tests/snippets/syntax_decimal.py b/extra_tests/snippets/syntax_decimal.py new file mode 100644 index 000000000..d7320f2ca --- /dev/null +++ b/extra_tests/snippets/syntax_decimal.py @@ -0,0 +1,6 @@ +try: + eval("0.E") +except SyntaxError: + pass +else: + assert False diff --git a/parser/src/lexer.rs b/parser/src/lexer.rs index b3fda9264..ac46b10bc 100644 --- a/parser/src/lexer.rs +++ b/parser/src/lexer.rs @@ -300,7 +300,10 @@ where value_text.push_str(&self.radix_run(10)); } - let value = f64::from_str(&value_text).unwrap(); + let value = f64::from_str(&value_text).map_err(|_| LexicalError { + error: LexicalErrorType::OtherError("Invalid decimal literal".to_owned()), + location: self.get_pos(), + })?; // Parse trailing 'j': if self.chr0 == Some('j') || self.chr0 == Some('J') { self.next_char();