PR sanitizer/65280
[official-gcc.git] / gcc / testsuite / c-c++-common / ubsan / bounds-1.c
blob5014f6f1f7efc58a5c43dbaa7087aaba595e26ce
1 /* { dg-do run } */
2 /* { dg-options "-fsanitize=bounds -fno-sanitize-recover=bounds -Wall" } */
4 /* Don't fail on valid uses. */
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]; };
11 __attribute__ ((noinline, noclone))
12 void
13 fn_p (int p)
17 __attribute__ ((noinline, noclone))
18 void
19 fn_a (volatile int a[])
21 /* This is not instrumented. */
22 a[4] = 5;
25 __attribute__ ((noinline, noclone))
26 int
27 foo_i (int i)
29 int a[5] = { };
30 int k = i ? a[i] : i;
31 return k;
34 int
35 main (void)
37 volatile int a[5];
38 a[4] = 1;
39 a[2] = a[3];
40 fn_p (a[4]);
41 fn_a (a);
43 int i = 4;
44 a[i] = 1;
45 a[2] = a[i];
46 fn_p (a[i]);
47 foo_i (i);
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);
56 volatile int c[n][n][n];
57 c[2][2][2] = 2;
58 i = c[4][4][4];
60 volatile struct S s;
61 s.a[9] = 1;
62 i = s.a[9];
64 /* Don't instrument flexible array members. */
65 struct T *t = (struct T *) __builtin_malloc (sizeof (struct T) + 10);
66 t->a[1] = 1;
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;
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;
76 long int *d[10][5];
77 d[9][0] = (long int *) 0;
78 d[8][3] = d[9][0];
80 return 0;