Fix some undefined behavior from casting in dt_arith.c#6456
Open
jhendersonHDF wants to merge 1 commit into
Open
Fix some undefined behavior from casting in dt_arith.c#6456jhendersonHDF wants to merge 1 commit into
jhendersonHDF wants to merge 1 commit into
Conversation
Contributor
Review ChecklistThis PR touches the following areas. Each needs at least one
|
brtnfld
reviewed
Jun 16, 2026
brtnfld
reviewed
Jun 16, 2026
| float f; | ||
| memcpy(&f, src_buf + idx * sizeof(float), sizeof(float)); | ||
| aligned = (long long)f; | ||
| if (f > (float)(LLONG_MAX)) |
Collaborator
Author
There was a problem hiding this comment.
There are possibly some comparison issues here with certain types due to rounding in specific compilers, but this is currently just matching the logic in the library so the testing passes
brtnfld
reviewed
Jun 16, 2026
| aligned = (unsigned char)f16; | ||
| if (f16 > (H5__Float16)(UCHAR_MAX)) | ||
| aligned = UCHAR_MAX; | ||
| else if (f16 < (H5__Float16)0) |
Collaborator
There was a problem hiding this comment.
Don't you also need this for to_ushort/to_uint/to_ulong/to_ullong
Collaborator
Author
There was a problem hiding this comment.
In this case no, because the conversion logic in the library doesn't perform any checks since we're guaranteed that an f16 will fit into ushort and larger
f3a776a to
cc81fcc
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes some more undefined behavior in dt_arith.c due to casting floating-point type values into integer types that can't represent the values. Emulates what the library is doing internally in conversion routines. There is still some remaining undefined behavior in the library itself (exposed by dt_arith.c) from not checking for NaN values before casting them to integer types.