Debian Codegraph @

gcc/testsuite/gcc.dg/ubsan/flex-array-counted-by-bounds.c

raw
1 /* Test the attribute counted_by and its usage in
2 bounds sanitizer. */
3 /* { dg-do run } */
4 /* { dg-options "-fsanitize=bounds" } */
5
6 #include <stdlib.h>
7
8 struct flex {
9 int b;
10 int c[];
11 } *array_flex;
12
13 struct annotated {
14 int b;
15 int c[] __attribute__ ((counted_by (b)));
16 } *array_annotated;
17
18 void __attribute__((__noinline__)) setup (int normal_count, int annotated_count)
19 {
20 array_flex
21 = (struct flex *)malloc (sizeof (struct flex)
22 + normal_count * sizeof (int));
23 array_flex->b = normal_count;
24
25 array_annotated
26 = (struct annotated *)malloc (sizeof (struct annotated)
27 + annotated_count * sizeof (int));
28 array_annotated->b = annotated_count;
29
30 return;
31 }
32
33 void __attribute__((__noinline__)) test (int normal_index, int annotated_index)
34 {
35 array_flex->c[normal_index] = 1;
36 array_annotated->c[annotated_index] = 2;
37 }
38
39 int main(int argc, char *argv[])
40 {
41 setup (10, 10);
42 test (10, 10);
43 return 0;
44 }
45
46 /* { dg-output "36:21: runtime error: index 10 out of bounds for type" } */