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

--- Comment #3 from Yu Zhige <zhige.yu18 at imperial dot ac.uk> ---
(In reply to Thomas Koenig from comment #1)
> 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.

Hi. The bug still exists even if we remove the UB in the inner for-loop. That
is, for this program:

#include <stdio.h>
char a = 0, f = 0, c = 5;
unsigned long d = 0;
int g = 0;
int *e = &g;
int main() {
  char  b = 0;
  for (;;) {
    for (a = 0; a < 2; a++) { // no UB I believe
      if (c) {
        printf("%lu\n", d);
        return 0; 
      }
    }
    f = (d++, *e);
  }
}

The program would still trigger the bug with GCC-trunk.

Reply via email to