[Bug libstdc++/106469] Undefined behavior triggered on Mersenne Twister engine due to unsigned integer overflow

redi at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Fri Jul 29 06:23:42 GMT 2022


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106469

--- Comment #5 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Henry from comment #3)
> So far that single line is the only place in all libstdc++ that triggers
> that undefined. 

No it isn't, we "fixed" another one a few days ago, in perfectly correct code,
just to silence this stupid sanitizer.


> I cannot believe that a developer consciously chose to let unsigned
> underflow happen in such simple expression.

You're wrong, twice. The behaviour of that code is intentional, and there is no
underflow. Unsigned integers cannot underflow or overflow, by definition. They
are defined to wrap around with modulus arithmetic, which is exactly the
behaviour desired for Mersenne twister (and most other PRNGs). If you want
modulus arithmetic and unsigned integers have modulus arithmetic, relying on
that makes perfect sense.

This sanitizer is stupid.


More information about the Gcc-bugs mailing list