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

            Bug ID: 70930
           Summary: VLAs in stucts in loop headers are not evaluated each
                    iteration
           Product: gcc
           Version: 7.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ch3root at openwall dot com
  Target Milestone: ---

Source code:

----------------------------------------------------------------------
#include <stdio.h>

int main()
{
  int n, z;

  // (1) `while` with a variably modified array type
  n = 1;
  while ((z = sizeof(char[n])) && n < 10) {
    printf("%d ", z);
    n++;
  }
  printf("\n");

  // (2) `while` with a variably modified structure type
  n = 1;
  while ((z = sizeof(struct { char a[n]; })) && n < 10) {
    printf("%d ", z);
    n++;
  }
  printf("\n");

  // (3) an equivalent for the latter with `goto`
  n = 1;
  {
  loop:;
    _Bool ctrl_expr = (z = sizeof(struct { char a[n]; })) && n < 10;
    if (!ctrl_expr)
      goto after_loop;
    {
      printf("%d ", z);
      n++;
    }
    goto loop;
  }
 after_loop:
  printf("\n");
}
----------------------------------------------------------------------

Results:

----------------------------------------------------------------------
$ gcc -std=c11 -Wall -Wextra -O3 test.c && ./a.out
1 2 3 4 5 6 7 8 9 
1 1 1 1 1 1 1 1 1 
1 2 3 4 5 6 7 8 9 
----------------------------------------------------------------------

gcc version: gcc (GCC) 7.0.0 20160502 (experimental)

The testcase includes three separate tests.

(1) contains a straight VLA in a loop header (inside of sizeof). The output
from this part of the program ("1 2 3 4 5 6 7 8 9 ") shows that it's
reevaluated on each iteration. Fine.

(2) is the same but VLA is wrapped in a struct. The output ("1 1 1 1 1 1 1 1 1
") shows that it's not reevaluated. Not fine.

(3) is AFAICT an equivalent to (2) with respect to blocks, scopes and such. It
works fine.

Thus, it's the combination of loops and VLAs in structs which is broken.

Reply via email to