(root)/
gcc-13.2.0/
gcc/
testsuite/
gcc.dg/
Warray-bounds-11.c
       1  /* { dg-do compile } */
       2  /* { dg-options "-O3 -Warray-bounds=2" } */
       3  
       4  typedef __SIZE_TYPE__ size_t;
       5  extern void* malloc(size_t x);
       6  
       7  int e[3];
       8  
       9  struct f { int f[3]; };
      10  
      11  extern void bar(int v[]);
      12  
      13  struct h {
      14  
      15  	int i;
      16  	int j[];
      17  };
      18  
      19  struct h0 {
      20  
      21  	int i;
      22  	int j[0];
      23  };
      24  
      25  struct h0b {
      26  
      27  	int i;
      28  	int j[0];
      29  	int k;
      30  };
      31  
      32  struct h1 {
      33  
      34  	int i;
      35  	int j[1];
      36  };
      37  
      38  struct h1b {
      39  
      40  	int i;
      41  	int j[1];
      42  	int k;
      43  };
      44  
      45  struct h3 {
      46  
      47  	int i;
      48  	int j[3];
      49  };
      50  
      51  struct h3b {
      52  
      53  	int i;
      54  	int j[3];
      55  	int k;
      56  };
      57  
      58  void foo(int (*a)[3])
      59  {
      60  	(*a)[4] = 1;	/* { dg-warning "subscript 4 is above array bound" } */
      61  	a[0][0] = 1;	// ok
      62  	a[1][0] = 1;	// ok
      63  	a[1][4] = 1;	/* { dg-warning "subscript 4 is above array bound" } */
      64  
      65  	int c[3] = { 0 };
      66  
      67  	c[4] = 1;	/* { dg-warning "subscript 4 is above array bound" } */
      68  
      69  	e[4] = 1;	/* { dg-warning "subscript 4 is above array bound" } */
      70  
      71  	struct f f;
      72  	f.f[4] = 1;	/* { dg-warning "subscript 4 is above array bound" } */
      73  
      74  	struct h* h = malloc(sizeof(struct h) + 3 * sizeof(int));
      75  	struct h0* h0 = malloc(sizeof(struct h0) + 3 * sizeof(int));
      76  	struct h1* h1 = malloc(sizeof(struct h1) + 3 * sizeof(int));
      77  	struct h3* h3 = malloc(sizeof(struct h3));
      78  
      79  	h->j[4] = 1;	// flexible array member
      80  	h0->j[4] = 1;	// zero-sized array extension
      81  	h1->j[4] = 1;	/* { dg-bogus "subscript 4 is above array bound" } */
      82  	h3->j[4] = 1;	/* { dg-warning "subscript 4 is above array bound" } */
      83  
      84  	struct h0b* h0b = malloc(sizeof(struct h) + 3 * sizeof(int));
      85  	struct h1b* h1b = malloc(sizeof(struct h1b) + 3 * sizeof(int));
      86  	struct h3b* h3b = malloc(sizeof(struct h3b));
      87  //	h0b->j[4] = 1;
      88  	h1b->j[4] = 1;;	/* { dg-warning "subscript 4 is above array bound" } */
      89  	h3b->j[4] = 1;;	/* { dg-warning "subscript 4 is above array bound" } */
      90  
      91  	// make sure nothing gets optimized away
      92  	bar(*a);
      93  	bar(c);
      94  	bar(e);
      95  	bar(f.f);
      96  	bar(h1->j);
      97  	bar(h3->j);
      98  	bar(h3b->j);
      99  	bar(h1b->j);
     100  	bar(h->j);
     101  	bar(h0->j);
     102  }
     103