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

Int.construct(from:) can crash instead of return nil #258

Closed
stephencelis opened this issue Jul 30, 2024 · 2 comments · Fixed by #259
Closed

Int.construct(from:) can crash instead of return nil #258

stephencelis opened this issue Jul 30, 2024 · 2 comments · Fixed by #259

Comments

@stephencelis
Copy link

While the JSValueCompatible APIs are failable, the underlying conversion of calling SignedInteger.init(_:) and UnsignedInteger.init(_:) directly can crash at runtime if:

  • double.isNan
  • !double.isFinite
  • double > .max || double < .min

Ideally, none of these would crash and return nil instead:

  • Int.construct(from: JSValue.number(.infinity))
  • Int.construct(from: JSValue.number(.nan))
  • Int.construct(from: JSValue.number(numberGreaterThanIntMax)
  • Int.construct(from: JSValue.number(numberLessThanIntMin)
@stephencelis
Copy link
Author

I think this would suffice:

-return Self(number)
+return Self(exactly: number.rounded(.towardZero))

@kateinoigakukun
Copy link
Member

Thanks for reporting!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants