-
Notifications
You must be signed in to change notification settings - Fork 0
Reject numbers the type cannot faithfully hold #32
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
Merged
+98
−3
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -469,23 +469,54 @@ def cast_integer(value) | |
|
|
||
| def cast_float(value) | ||
| case value | ||
| when Numeric then [:ok, value.to_f] | ||
| when String then [:ok, Float(value)] | ||
| when Numeric then finite_float(value.to_f) | ||
| when String then finite_float(Float(value), source: value) | ||
| else [:error, "invalid_type"] | ||
| end | ||
| rescue ArgumentError | ||
| [:error, "invalid_type"] | ||
| end | ||
|
|
||
| # A Float that is not finite does not represent what was sent. "1e400" | ||
| # overflows to Infinity and "1e-400" underflows to zero — both silently, | ||
| # and both leaving a value no column can faithfully store. | ||
| # | ||
| # Underflow is only visible against the source text, since the result is | ||
| # an ordinary 0.0: a zero result is rejected when the string it came from | ||
| # named a nonzero SIGNIFICAND. Only the significand, because "0e10" is a | ||
| # genuine zero whose exponent digits say nothing about the value — as are | ||
| # "0", "0.0" and "0.0000". | ||
| def finite_float(result, source: nil) | ||
| return [:error, "invalid_type"] unless result.finite? | ||
| return [:error, "invalid_type"] if result.zero? && nonzero_significand?(source) | ||
|
|
||
| [:ok, result] | ||
| end | ||
|
|
||
| def nonzero_significand?(source) | ||
| return false unless source | ||
|
|
||
| source.split(/[eE]/, 2).first.match?(/[1-9]/) | ||
| end | ||
|
|
||
| def cast_decimal(value) | ||
| case value | ||
| when Numeric, String then [:ok, BigDecimal(value.to_s)] | ||
| when Numeric, String then finite_decimal(BigDecimal(value.to_s)) | ||
| else [:error, "invalid_type"] | ||
| end | ||
| rescue ArgumentError | ||
| [:error, "invalid_type"] | ||
| end | ||
|
|
||
| # BigDecimal has no exponent limit, so a :decimal cannot overflow — but | ||
| # BigDecimal("NaN") and BigDecimal("Infinity") SUCCEED where Float() | ||
| # raises, so a client could send the literal string "NaN" for a price and | ||
| # have it stored. Nothing else in the gem disagreed with itself this | ||
| # loudly: :float rejected those strings and :decimal did not. | ||
| def finite_decimal(result) | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Explicitly checking |
||
| result.finite? ? [:ok, result] : [:error, "invalid_type"] | ||
| end | ||
|
|
||
| def cast_boolean(value) | ||
| return [:ok, true] if TRUE_VALUES.include?(value) | ||
| return [:ok, false] if FALSE_VALUES.include?(value) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Detecting Float underflow by comparing
result.zero?againstnonzero_significand?(source)is a clever check to reject inputs like"1e-400"that underflow to 0.0 while preserving legitimate zeroes ("0.0","0e10").