Repository navigation
GH-46557: [C++][R] Check overflow in float to int casts that allow truncation #52469
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
Open
advitrocks9
wants to merge
6
commits into
apache:main
Choose a base branch
from
advitrocks9:fix-float-int-cast-overflow
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d490bd6
GH-46557: [C++][R] Check overflow in float to int casts that allow tr…
advitrocks9 d255ed4
Keep safe cast error messages unchanged
advitrocks9 a7a29ee
Disable float-cast-overflow sanitizer in half float to int cast
advitrocks9 95a2688
Decode half float values in cast error messages
advitrocks9 f2a1baa
GH-46557: [C++] Check the range before converting when truncation is …
advitrocks9 f65ceaa
GH-46557: [C++] Check the range after the conversion
advitrocks9 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
Some comments aren't visible on the classic Files Changed page.
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 |
|---|---|---|
|
|
@@ -398,6 +398,66 @@ TEST(Cast, FloatingToInt) { | |
| } | ||
| } | ||
|
|
||
| TEST(Cast, FloatingToIntOverflow) { | ||
| for (auto from : {float16(), float32(), float64()}) { | ||
| for (bool allow_float_truncate : {false, true}) { | ||
| auto opts = CastOptions::Safe(int8()); | ||
| opts.allow_float_truncate = allow_float_truncate; | ||
| for (auto json : {"[128.0]", "[-129.0]", "[NaN]", "[Inf]", "[-Inf]"}) { | ||
|
Member
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. Can we also test |
||
| CheckCastFails(ArrayFromJSON(from, json), opts); | ||
| } | ||
| CheckCast(ArrayFromJSON(from, "[127.0, null, -128.0]"), | ||
| ArrayFromJSON(int8(), "[127, null, -128]"), opts); | ||
| CheckCast(TweakValidityBit(ArrayFromJSON(from, "[1000.0, 1.0]"), 0, false), | ||
| ArrayFromJSON(int8(), "[null, 1]"), opts); | ||
|
|
||
| opts.to_type = uint8(); | ||
| CheckCastFails(ArrayFromJSON(from, "[256.0]"), opts); | ||
| CheckCastFails(ArrayFromJSON(from, "[-1.0]"), opts); | ||
|
Comment on lines
+415
to
+416
Member
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. What about |
||
| CheckCast(ArrayFromJSON(from, "[255.0, null, 0.0]"), | ||
| ArrayFromJSON(uint8(), "[255, null, 0]"), opts); | ||
| } | ||
|
|
||
| auto opts = CastOptions::Safe(int8()); | ||
| CheckCastFails(ArrayFromJSON(from, "[127.5]"), opts); | ||
| CheckCastFails(ArrayFromJSON(from, "[-128.5]"), opts); | ||
| opts.allow_float_truncate = true; | ||
| CheckCast(ArrayFromJSON(from, "[127.5, -128.5]"), | ||
| ArrayFromJSON(int8(), "[127, -128]"), opts); | ||
|
|
||
| opts = CastOptions::Safe(uint8()); | ||
| CheckCastFails(ArrayFromJSON(from, "[255.5]"), opts); | ||
| CheckCastFails(ArrayFromJSON(from, "[-0.5]"), opts); | ||
| opts.allow_float_truncate = true; | ||
| CheckCast(ArrayFromJSON(from, "[255.5, -0.5]"), ArrayFromJSON(uint8(), "[255, 0]"), | ||
| opts); | ||
| } | ||
|
|
||
| for (auto from : {float32(), float64()}) { | ||
| auto opts = CastOptions::Safe(int64()); | ||
| opts.allow_float_truncate = true; | ||
| for (auto json : {"[9223372036854775807]", "[9223372036854775807.5]", | ||
| "[9223372036854775808.0]"}) { | ||
| CheckCastFails(ArrayFromJSON(from, json), opts); | ||
| } | ||
| CheckCast(ArrayFromJSON(from, "[-9223372036854775808.0, -9223372036854775808.5]"), | ||
| ArrayFromJSON(int64(), "[-9223372036854775808, -9223372036854775808]"), | ||
| opts); | ||
| opts.to_type = uint64(); | ||
| CheckCastFails(ArrayFromJSON(from, "[18446744073709551616.0]"), opts); | ||
| } | ||
|
|
||
| auto opts = CastOptions::Safe(int8()); | ||
| EXPECT_RAISES_WITH_MESSAGE_THAT( | ||
| Invalid, | ||
| ::testing::HasSubstr("Float value 1.500000 was truncated converting to int8"), | ||
| Cast(ArrayFromJSON(float16(), "[1.5]"), opts)); | ||
| opts.allow_float_truncate = true; | ||
| EXPECT_RAISES_WITH_MESSAGE_THAT( | ||
| Invalid, ::testing::HasSubstr("Float value inf out of range converting to int8"), | ||
| Cast(ArrayFromJSON(float16(), "[Inf]"), opts)); | ||
| } | ||
|
|
||
| TEST(Cast, FloatingToFloating) { | ||
| for (auto from : {float16(), float32(), float64()}) { | ||
| for (auto to : {float16(), float32(), float64()}) { | ||
|
|
||
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.
These seems to assume that
OutTis integral, can we add astatic_assertabout that?