Debian Codegraph @

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

raw
1 /* Test the attribute counted_by and its usage in
2 bounds sanitizer combined with VLA. */
3 /* { dg-do run } */
4 /* { dg-options "-fsanitize=bounds" } */
5 /* { dg-output "index 11 out of bounds for type 'int \\\[\\\*\\\]\\\[\\\*\\\]'\[^\n\r]*(\n|\r\n|\r)" } */
6 /* { dg-output "\[^\n\r]*index 20 out of bounds for type 'int \\\[\\\*\\\]\\\[\\\*\\\]\\\[\\\*\\\]'\[^\n\r]*(\n|\r\n|\r)" } */
7 /* { dg-output "\[^\n\r]*index 11 out of bounds for type 'int \\\[\\\*\\\]\\\[\\\*\\\]'\[^\n\r]*(\n|\r\n|\r)" } */
8 /* { dg-output "\[^\n\r]*index 10 out of bounds for type 'int \\\[\\\*\\\]'\[^\n\r]*(\n|\r\n|\r)" } */
9
10
11 #include <stdlib.h>
12
13 void __attribute__((__noinline__)) setup_and_test_vla (int n, int m)
14 {
15 struct foo {
16 int n;
17 int p[][n] __attribute__((counted_by(n)));
18 } *f;
19
20 f = (struct foo *) malloc (sizeof(struct foo) + m*sizeof(int[n]));
21 f->n = m;
22 f->p[m][n-1]=1;
23 return;
24 }
25
26 void __attribute__((__noinline__)) setup_and_test_vla_1 (int n1, int n2, int m)
27 {
28 struct foo {
29 int n;
30 int p[][n2][n1] __attribute__((counted_by(n)));
31 } *f;
32
33 f = (struct foo *) malloc (sizeof(struct foo) + m*sizeof(int[n2][n1]));
34 f->n = m;
35 f->p[m][n2][n1]=1;
36 return;
37 }
38
39 int main(int argc, char *argv[])
40 {
41 setup_and_test_vla (10, 11);
42 setup_and_test_vla_1 (10, 11, 20);
43 return 0;
44 }
45