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

Fix String#to_f out of range #10425

Merged

Conversation

straight-shoota
Copy link
Member

@straight-shoota straight-shoota commented Feb 19, 2021

String#to_f generally does not accept values representing infinity or NaN. It would allow them if the first character is whitespace (and whitespace is enabled), them.

String#to_f returns infinity for values outside of the data type's range. That's incorrect, it should be an error.

This patch fixes that and makes sure it fails when strtod/strtof return a non-finite value.

We should probably consider an option for parsing infinity and NaN. But for now this is just a simple bug fix.

@straight-shoota straight-shoota added kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:stdlib:numeric labels Feb 19, 2021
@bcardiff
Copy link
Member

Can we add specs that NaN INF without whitespaces also return nil?

Parsing those values should also consider localization. I agree to leave that out for now.

@straight-shoota
Copy link
Member Author

Seems I misinterpreted the condition. Infinity and NaN values are always accepted if whitespace: true (the default). So this fix is probably not good. But I really can't make any sense why toggling whitespace should have an effect on infinity and NaN.

@straight-shoota
Copy link
Member Author

The main bug is handling of values outside of range. That should fail instead of returning infinity and should be high priority for correctness.

The minimal fix for that would be to recognize if infinity value returned from strtod/strtof is actually infinity or just represents over- or underflow.

src/string.cr Outdated
unless v.finite?
startptr = to_unsafe
if whitespace
while(startptr.value.chr.ascii_whitespace?)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Despite #10367 this only checks for ascii whitespace because strtod/strtof only support ascii whitespace.

@straight-shoota straight-shoota changed the title Fix String#to_f for infinity, NaN and out of range Fix String#to_f out of range Feb 19, 2021
@bcardiff
Copy link
Member

String#to_f returns infinity for values outside of the data type's range.

You mean this as in before this PR, right? Because

Before this PR: Float64::MAX.to_s.to_f32 # => Infinity
After this PR: Float64::MAX.to_s.to_f32 # => Unhandled exception: Invalid Float32: 1.7976931348623157e+308 (ArgumentError)

@bcardiff bcardiff added this to the 1.0.0 milestone Feb 19, 2021
@straight-shoota
Copy link
Member Author

You mean this as in before this PR, right?

Yeah, the edit was probably somewhat disruptive to the message. I tried to fix that.

@straight-shoota straight-shoota merged commit 4053923 into crystal-lang:master Feb 20, 2021
@straight-shoota straight-shoota deleted the fix/string-to_f-range branch February 20, 2021 17:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:stdlib:numeric
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants