From f5e289d854eec6c2e6ba02a5468acf4807eab808 Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Mon, 10 Aug 2026 13:02:43 +0200 Subject: [PATCH] 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. --- CHANGELOG.md | 1 + mkl_random/src/mkl_distributions.cpp | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 63f0c3d0..4e5a5f68 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed * 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) +* 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) ## [1.4.1] (05/11/2026) diff --git a/mkl_random/src/mkl_distributions.cpp b/mkl_random/src/mkl_distributions.cpp index 20c37a35..8fa67d96 100644 --- a/mkl_random/src/mkl_distributions.cpp +++ b/mkl_random/src/mkl_distributions.cpp @@ -1981,7 +1981,7 @@ void irk_rand_uint32_vec(irk_state *state, if (hi >= intm) { - npy_int32 shift = ((npy_uint32)intm) + ((npy_uint32)1); + npy_uint32 shift = ((npy_uint32)intm) + ((npy_uint32)1); int i; /* if lo is non-zero, shift one more to accommodate possibility of hi