[Bug c/96760] Faulty optimization in nested loops with -O2

tkoenig at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Mon Aug 24 09:50:04 GMT 2020


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

Thomas Koenig <tkoenig at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
                 CC|                            |tkoenig at gcc dot gnu.org
         Resolution|---                         |INVALID

--- Comment #1 from Thomas Koenig <tkoenig at gcc dot gnu.org> ---
The loop

for (a = 20; a; a++) {

increases a, which is a char, beyond its value range, and then tests
against zero.

This is undefined behavior.

N4659, Clause 8:

# If during the evaluation of an expression, the result is not mathematically
# defined or not in the range of representable values for its type, the
# behavior is undefined.

If you had made a an unsigned type (for example an unsigned char), then
the results would probably have been closer to what you expected.


More information about the Gcc-bugs mailing list