-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Fix String#to_f
out of range
#10425
Conversation
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. |
Seems I misinterpreted the condition. Infinity and NaN values are always accepted if |
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?) |
There was a problem hiding this comment.
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.
String#to_f
for infinity, NaN and out of rangeString#to_f
out of range
You mean this as in before this PR, right? Because Before this PR: |
Yeah, the edit was probably somewhat disruptive to the message. I tried to fix that. |
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.