Skip to content

Commit fc5ae50

Browse files
committed
Fix constant integer overflow in irk_rand_uint32_vec
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.
1 parent 5edd83e commit fc5ae50

2 files changed

Lines changed: 2 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1515

1616
### Fixed
1717
* Fixed compatibility with NumPy 2.5 by replacing the deprecated in-place array `shape` assignment with `reshape`, and by replacing the deprecated `numpy.testing.suppress_warnings` usage in tests with `pytest.warns` [gh-137](https://github.com/IntelPython/mkl_random/pull/137)
18+
* Fixed a constant integer overflow in `irk_rand_uint32_vec` by declaring the `shift` variable as `npy_uint32` instead of `npy_int32`, so `2**31` no longer overflows a signed 32-bit integer (behavior is unchanged as all downstream uses rely on modulo-2^32 arithmetic)
1819

1920
## [1.4.1] (05/11/2026)
2021

mkl_random/src/mkl_distributions.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1981,7 +1981,7 @@ void irk_rand_uint32_vec(irk_state *state,
19811981

19821982
if (hi >= intm) {
19831983

1984-
npy_int32 shift = ((npy_uint32)intm) + ((npy_uint32)1);
1984+
npy_uint32 shift = ((npy_uint32)intm) + ((npy_uint32)1);
19851985
int i;
19861986

19871987
/* if lo is non-zero, shift one more to accommodate possibility of hi

0 commit comments

Comments
 (0)