diff --git a/random.c b/random.c index 3be22a4ec0..1093a103c1 100644 --- a/random.c +++ b/random.c @@ -991,7 +991,11 @@ random_ulong_limited(VALUE obj, rb_random_t *rnd, unsigned long limit) const int w = sizeof(limit) * CHAR_BIT - nlz_long(limit); const int n = w > 32 ? sizeof(unsigned long) : sizeof(uint32_t); const unsigned long mask = ~(~0UL << w); - const unsigned long full = ~(~0UL << n * CHAR_BIT); + const unsigned long full = +#if SIZEOF_LONG == 4 + (size_t)n >= sizeof(unsigned long) ? ~0UL : +#endif + ~(~0UL << n * CHAR_BIT); unsigned long val, bits = 0, rest = 0; do { if (mask & ~rest) {