Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ES lexer crashes on numbers in e notation with large exponent #3060

Closed
asterite3 opened this issue Dec 17, 2021 · 1 comment · Fixed by #3061
Closed

ES lexer crashes on numbers in e notation with large exponent #3060

asterite3 opened this issue Dec 17, 2021 · 1 comment · Fixed by #3061
Labels
Milestone

Comments

@asterite3
Copy link
Contributor

asterite3 commented Dec 17, 2021

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 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

write!(raw_val, "{}", exp).unwrap();

Because strings like 1e+inf are invalid, code raw_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:

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

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

  • either Infinity - when mantissa is nonzero and exponent is positive;
  • or as 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 as 0.

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

kdy1 pushed a commit that referenced this issue Dec 18, 2021
swc_ecma_parser:
 - Fix lexing of numbers where exponents are large enough to be parsed as `Infinity`. (Closes #3060)
@kdy1 kdy1 added this to the v1.2.121 milestone Dec 18, 2021
@swc-bot
Copy link
Collaborator

swc-bot commented Oct 20, 2022

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.

@swc-project swc-project locked as resolved and limited conversation to collaborators Oct 20, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

Successfully merging a pull request may close this issue.

3 participants