Skip to content

Fix some undefined behavior from casting in dt_arith.c#6456

Open
jhendersonHDF wants to merge 1 commit into
HDFGroup:developfrom
jhendersonHDF:dt_arith_undefined
Open

Fix some undefined behavior from casting in dt_arith.c#6456
jhendersonHDF wants to merge 1 commit into
HDFGroup:developfrom
jhendersonHDF:dt_arith_undefined

Conversation

@jhendersonHDF

@jhendersonHDF jhendersonHDF commented Jun 15, 2026

Copy link
Copy Markdown
Collaborator

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.

@jhendersonHDF jhendersonHDF added the Component - Testing Code in test or testpar directories, GitHub workflows label Jun 15, 2026
@github-project-automation github-project-automation Bot moved this to To be triaged in HDF5 - TRIAGE & TRACK Jun 15, 2026
@github-actions github-actions Bot requested a review from fortnern June 15, 2026 20:16
@github-actions

github-actions Bot commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

Review Checklist

This PR touches the following areas. Each needs at least one
sign-off from its listed owners before merging — an approval
covering only one area does not satisfy the others.

@jhendersonHDF jhendersonHDF marked this pull request as ready for review June 15, 2026 21:12
Comment thread test/dt_arith.c Outdated
Comment thread test/dt_arith.c
float f;
memcpy(&f, src_buf + idx * sizeof(float), sizeof(float));
aligned = (long long)f;
if (f > (float)(LLONG_MAX))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Should this be >=

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

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

Comment thread test/dt_arith.c
aligned = (unsigned char)f16;
if (f16 > (H5__Float16)(UCHAR_MAX))
aligned = UCHAR_MAX;
else if (f16 < (H5__Float16)0)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Don't you also need this for to_ushort/to_uint/to_ulong/to_ullong

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Component - Testing Code in test or testpar directories, GitHub workflows

Projects

Status: To be triaged

Development

Successfully merging this pull request may close these issues.

2 participants