Skip to content

Fix constant integer overflow in irk_rand_uint32_vec - #156

Merged
antonwolfy merged 1 commit into
masterfrom
fix-coverity-shift-overflow-master
Aug 11, 2026
Merged

Fix constant integer overflow in irk_rand_uint32_vec#156
antonwolfy merged 1 commit into
masterfrom
fix-coverity-shift-overflow-master

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.

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.

@vlad-perevezentsev vlad-perevezentsev left a comment

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.

LGTM
Thank you @antonwolfy

@antonwolfy
antonwolfy merged commit d061d8f into master Aug 11, 2026
102 of 104 checks passed
@antonwolfy
antonwolfy deleted the fix-coverity-shift-overflow-master branch August 11, 2026 10:56
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.

2 participants