(root)/
gcc-13.2.0/
gcc/
testsuite/
c-c++-common/
ubsan/
bounds-1.c
       1  /* { dg-do run } */
       2  /* { dg-options "-fsanitize=bounds -fno-sanitize-recover=bounds -Wall" } */
       3  
       4  /* Don't fail on valid uses.  */
       5  
       6  struct S { int a[10]; };
       7  struct T { int l; int a[]; };
       8  struct U { int l; int a[0]; };
       9  struct V { int l; int a[1]; };
      10  
      11  __attribute__ ((noinline, noclone))
      12  void
      13  fn_p (int p)
      14  {
      15  }
      16  
      17  __attribute__ ((noinline, noclone))
      18  void
      19  fn_a (volatile int a[])
      20  {
      21    /* This is not instrumented.  */
      22    a[4] = 5;
      23  }
      24  
      25  __attribute__ ((noinline, noclone))
      26  int
      27  foo_i (int i)
      28  {
      29    int a[5] = { };
      30    int k = i ? a[i] : i;
      31    return k;
      32  }
      33  
      34  int
      35  main (void)
      36  {
      37    volatile int a[5];
      38    a[4] = 1;
      39    a[2] = a[3];
      40    fn_p (a[4]);
      41    fn_a (a);
      42  
      43    int i = 4;
      44    a[i] = 1;
      45    a[2] = a[i];
      46    fn_p (a[i]);
      47    foo_i (i);
      48  
      49    const int n = 5;
      50    volatile int b[n];
      51    b[4] = 1;
      52    b[2] = b[3];
      53    fn_p (b[4]);
      54    fn_a (b);
      55  
      56    volatile int c[n][n][n];
      57    c[2][2][2] = 2;
      58    i = c[4][4][4];
      59  
      60    volatile struct S s;
      61    s.a[9] = 1;
      62    i = s.a[9];
      63  
      64    /* Don't instrument flexible array members.  */
      65    struct T *t = (struct T *) __builtin_malloc (sizeof (struct T) + 10);
      66    t->a[1] = 1;
      67  
      68    /* Don't instrument zero-sized arrays (GNU extension).  */
      69    struct U *u = (struct U *) __builtin_malloc (sizeof (struct U) + 10);
      70    u->a[1] = 1;
      71  
      72    /* Don't instrument last array in a struct.  */
      73    struct V *v = (struct V *) __builtin_malloc (sizeof (struct V) + 10);
      74    v->a[1] = 1;
      75  
      76    long int *d[10][5];
      77    d[9][0] = (long int *) 0;
      78    d[8][3] = d[9][0];
      79  
      80    return 0;
      81  }