You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
ECMAScript lexer crashes on numbers in scientific (e) notation with large exponents: namely, exponents that become infinite when parsed as Rust's f64. An example of such number is 1e10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000. Another example is 1e-10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 (with negative exponent)
The reason for this bug is that, when such exponent is parsed as Rust's f64 and is converted to string, it becomes the string inf. It is then appended to a string forming the whole resulting number, which becomes something like 1e+inf, which is invalid. It happens here
thread 'main' panicked at 'failed to parse float literal: ParseFloatError { kind: Invalid }', .../swc/crates/swc_ecma_parser/src/lexer/number.rs:146:35
stack backtrace:
0: rust_begin_unwind
at /rustc/ad442399756573dccacb314b6bf8079964bcc72a/library/std/src/panicking.rs:498:5
1: core::panicking::panic_fmt
at /rustc/ad442399756573dccacb314b6bf8079964bcc72a/library/core/src/panicking.rs:106:14
2: core::result::unwrap_failed
at /rustc/ad442399756573dccacb314b6bf8079964bcc72a/library/core/src/result.rs:1613:5
3: core::result::Result<T,E>::expect
at /rustc/ad442399756573dccacb314b6bf8079964bcc72a/library/core/src/result.rs:1255:23
4: swc_ecma_parser::lexer::number::<impl swc_ecma_parser::lexer::Lexer<I>>::read_number
at ./crates/swc_ecma_parser/src/lexer/number.rs:146:19
5: swc_ecma_parser::lexer::Lexer<I>::read_token
at ./crates/swc_ecma_parser/src/lexer/mod.rs:281:24
6: swc_ecma_parser::lexer::state::<impl core::iter::traits::iterator::Iterator for swc_ecma_parser::lexer::Lexer<I>>::next::{{closure}}
at ./crates/swc_ecma_parser/src/lexer/state.rs:292:13
7: swc_ecma_parser::lexer::state::<impl core::iter::traits::iterator::Iterator for swc_ecma_parser::lexer::Lexer<I>>::next
at ./crates/swc_ecma_parser/src/lexer/state.rs:172:19
8: swc_ecma_parser::parser::input::Buffer<I>::bump_inner::{{closure}}
at ./crates/swc_ecma_parser/src/parser/input.rs:297:48
9: core::option::Option<T>::or_else
at /rustc/ad442399756573dccacb314b6bf8079964bcc72a/library/core/src/option.rs:1147:21
10: swc_ecma_parser::parser::input::Buffer<I>::bump_inner
at ./crates/swc_ecma_parser/src/parser/input.rs:297:20
11: swc_ecma_parser::parser::input::Buffer<I>::cur
at ./crates/swc_ecma_parser/src/parser/input.rs:378:13
12: swc_ecma_parser::parser::input::Buffer<I>::cur_pos
at ./crates/swc_ecma_parser/src/parser/input.rs:426:17
13: swc_ecma_parser::parser::Parser<I>::parse_program
at ./crates/swc_ecma_parser/src/parser/mod.rs:127:21
14: tst::main
at ./crates/swc/examples/tst.rs:20:11
15: core::ops::function::FnOnce::call_once
at /rustc/ad442399756573dccacb314b6bf8079964bcc72a/library/core/src/ops/function.rs:227:5
This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.
Describe the bug
ECMAScript lexer crashes on numbers in scientific (e) notation with large exponents: namely, exponents that become infinite when parsed as Rust's
f64
. An example of such number is1e10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
. Another example is1e-10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
(with negative exponent)The reason for this bug is that, when such exponent is parsed as Rust's
f64
and is converted to string, it becomes the stringinf
. It is then appended to a string forming the whole resulting number, which becomes something like1e+inf
, which is invalid. It happens hereswc/crates/swc_ecma_parser/src/lexer/number.rs
Line 143 in 1ea965c
Because strings like
1e+inf
are invalid, coderaw_val.parse().expect("failed to parse float literal")
, that comes next, panics.Crash can be reproduced with this program https://gist.github.com/asterite3/ca2208452b398fa7914c53581f01488d (or, by running SWC as NodeJS module).
Backtrace is:
Input code
1e10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Config
No response
Playground link
https://play.swc.rs/?version=1.2.115&code=H4sIAAAAAAAAAzNMNTQYBWQAAMkHImI5AQAA&config=H4sIAAAAAAAAA0WMTQrEIAxG75K1286id5hDBCctFv9IUhgR714tlu7C915ehUMsrBUyshCPS0pU%2FMMKZAOKZZcVTNf6tKEXagYUeScdiiyd%2BZSEJjUQXHRbGSWbQmYSeRHG3T9m66GQfucYKmjJdAc%2F0N7G%2FHPynaLySe0Cj9ke9LUAAAA%3D
Expected behavior
Expected behavior is to parse such numbers as
Infinity
- when mantissa is nonzero and exponent is positive;0
otherwise (when mantissa is zero or exponent is negative).A somewhat edge case is when mantissa is itself so large that it parses as
Infinity
and exponent is negative. Both browsers and Babel parse this as0
.This is what browsers do (I checked Chrome and Firefox), and Babel parses them like this too.
Version
1.2.120
Additional context
No response
The text was updated successfully, but these errors were encountered: