Skip to content

Fix more undefined behavior issues#6330

Merged
lrknox merged 1 commit intoHDFGroup:developfrom
jhendersonHDF:fix_more_ubsan
Mar 31, 2026
Merged

Fix more undefined behavior issues#6330
lrknox merged 1 commit intoHDFGroup:developfrom
jhendersonHDF:fix_more_ubsan

Conversation

@jhendersonHDF
Copy link
Copy Markdown
Collaborator

No description provided.

@jhendersonHDF jhendersonHDF added the Component - C Library Core C library issues (usually in the src directory) label Mar 29, 2026
@github-project-automation github-project-automation bot moved this to To be triaged in HDF5 - TRIAGE & TRACK Mar 29, 2026
@jhendersonHDF
Copy link
Copy Markdown
Collaborator Author

With these changes, the default Ubuntu sanitizer build only has a handful of issues left in dt_arith testing, which will be a bit more involved to fix correctly.

Comment thread src/H5Ztrans.c
\
tree_val = \
((RESR).type == H5Z_XFORM_INTEGER ? (double)(RESR).value.int_val : (RESR).value.float_val); \
p = (TYPE *)(RESL).value.dat_val; \
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.

Casting p from void * to TYPE * here can create misaligned pointers. Use memcpy() from/to p and intermediate TYPE variables to avoid this.

Comment thread test/dsets.c
}

static herr_t
scatter_cb(void **src_buf /*out*/, size_t *src_buf_bytes_used /*out*/, void *_scatter_info)
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.

The prototype for an H5Dscatter() callback uses const void **, which is trivial to do correctly here.

Comment thread test/dt_arith.c
\
if (n < SRC_PREC - 2) { \
value1 = (TYPE)(value1 << 1); \
value1 = (TYPE)((uint64_t)value1 << 1); \
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.

Perform shifts in unsigned types before casting back to avoid UBSan warnings about left shifting negative values.

Comment thread test/dtransform.c
-75.0F, -82.0F, -89.0F, -97.0F},
{25.0F, 17.0F, 10.0F, 3.0F, -4.0F, -11.0F, -19.0F, -26.0F, -33.0F, -40.0F, -48.0F, -55.0F, -62.0F, -69.0F,
-76.0F, -84.0F, -91.0F, -98.0F}};
-76.0F, -84.0F, -91.0F, -97.0F}};
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.

Since intermediate data transformation operations are casted to the final type, -98.0F ends up just outside the range of char during a transformation operation. Cap the value to -97.0F.

Comment thread test/hyperslab.c
size[1] = ny;
src_stride[0] = 0;
src_stride[1] = sizeof(*src);
dst_stride[0] = (hsize_t)((1 - nx * ny) * sizeof(*src));
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.

This stride value, being negative, was being converted to a huge unsigned value when casted to hsize_t. As the intention is for the stride to be negative, use hssize_t values and H5VM_stride_copy_s() instead.

Comment thread test/hyperslab.c

/* Check offset of coordinate */
if (a[off] != off)
if (a[off] != off) {
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.

This is just for debugging a current CI failure

set (ADD_BUILD_OPTIONS "${ADD_BUILD_OPTIONS} -DHDF5_ENABLE_PLUGIN_SUPPORT:BOOL=OFF")
set (ADD_BUILD_OPTIONS "${ADD_BUILD_OPTIONS} -DLIBAEC_USE_LOCALCONTENT:BOOL=OFF")
set (ADD_BUILD_OPTIONS "${ADD_BUILD_OPTIONS} -DZLIB_USE_LOCALCONTENT:BOOL=OFF")
set (ADD_BUILD_OPTIONS "${ADD_BUILD_OPTIONS} -DPLUGIN_USE_LOCALCONTENT:BOOL=OFF")
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.

This CMake option isn't used in these builds and gives a configuration warning when set due to that

@lrknox lrknox merged commit ceca851 into HDFGroup:develop Mar 31, 2026
123 checks passed
@github-project-automation github-project-automation bot moved this from To be triaged to Done in HDF5 - TRIAGE & TRACK Mar 31, 2026
@jhendersonHDF jhendersonHDF deleted the fix_more_ubsan branch April 7, 2026 18:30
brtnfld pushed a commit to brtnfld/hdf5 that referenced this pull request Apr 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Component - C Library Core C library issues (usually in the src directory)

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

6 participants