Skip to content

Fix constant integer overflow in irk_rand_uint32_vec - #155

Closed
antonwolfy wants to merge 44 commits into
masterfrom
fix-coverity-shift-overflow
Closed

Fix constant integer overflow in irk_rand_uint32_vec#155
antonwolfy wants to merge 44 commits into
masterfrom
fix-coverity-shift-overflow

Conversation

@antonwolfy

@antonwolfy antonwolfy commented Aug 10, 2026

Copy link
Copy Markdown
Collaborator

Summary

Fixes Coverity INTEGER_OVERFLOW in irk_rand_uint32_vec in mkl_random/src/mkl_distributions.cpp.

The shift variable was declared as npy_int32, but its initializer:

npy_int32 shift = ((npy_uint32)intm) + ((npy_uint32)1);  // intm = INT_MAX

evaluates to 2**31 = 2147483648, which does not fit in a signed 32-bit integer. Storing it wrapped to INT32_MIN — an out-of-range signed conversion that Coverity flags as an overflowed constant.

Fix

Declare shift as npy_uint32. Both 2**31 and 2**31 + 1 (the if (lo) shift++; case) fit in an unsigned 32-bit integer, so the overflow disappears.

ndgrigorian and others added 30 commits May 15, 2026 13:12
* removes setup.py and generate_mklrand_c.py
* updates pyproject.toml
* adds meson.build
we search for MKL with cmake. Also drop pkg-config from meta.yamls and use cmake
remove tbb from build-with-clang workflow
aligns more closely with conda-forge feedstock
vlad-perevezentsev and others added 14 commits August 5, 2026 03:49
Declare the shift variable as npy_uint32 instead of npy_int32. The
right-hand side ((npy_uint32)INT_MAX + 1) equals 2**31, which does not
fit in a signed 32-bit integer and wrapped to INT32_MIN when stored.

All downstream uses (lo - shift, hi - shift + 1U, res[i] += shift)
already operate on the unsigned bit pattern via modulo-2**32 arithmetic,
so behavior is bit-for-bit identical. This removes the Coverity
INTEGER_OVERFLOW finding (CID 652701) and the out-of-range signed
conversion it relied on.
@antonwolfy

Copy link
Copy Markdown
Collaborator Author

Recreating this PR on top of master instead of the build-with-meson branch.

@antonwolfy antonwolfy closed this Aug 10, 2026
@antonwolfy antonwolfy removed this from the 1.5.0 release milestone Aug 10, 2026
@antonwolfy antonwolfy removed their assignment Aug 10, 2026
@antonwolfy
antonwolfy deleted the fix-coverity-shift-overflow branch August 10, 2026 11:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants