aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/gcc.dg/ubsan/pointer-counted-by-bounds-2.c
blob: 0653eccff3e1ca2e0350cfdddd0d84c13e99b156 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/* Test the attribute counted_by for pointer fields and its usage in
   bounds sanitizer combined with VLA.  */
/* { dg-do run } */
/* { dg-options "-fsanitize=bounds" } */
/* { dg-output "index 11 out of bounds for type 'int \\\[\\\*\\\]\\\[\\\*\\\]'\[^\n\r]*(\n|\r\n|\r)" } */
/* { dg-output "\[^\n\r]*index 20 out of bounds for type 'int \\\[\\\*\\\]\\\[\\\*\\\]\\\[\\\*\\\]'\[^\n\r]*(\n|\r\n|\r)" } */
/* { dg-output "\[^\n\r]*index 11 out of bounds for type 'int \\\[\\\*\\\]\\\[\\\*\\\]'\[^\n\r]*(\n|\r\n|\r)" } */
/* { dg-output "\[^\n\r]*index 10 out of bounds for type 'int \\\[\\\*\\\]'\[^\n\r]*(\n|\r\n|\r)" } */


#include <stdlib.h>

void __attribute__((__noinline__)) setup_and_test_vla (int n, int m)
{
  struct foo {
    int n;
    int (*p)[n] __attribute__((counted_by(n)));
  } *f;

  f = (struct foo *) malloc (sizeof (struct foo));
  f->p = (int (*)[n]) malloc (m * sizeof (int[n]));
  f->n = m;
  f->p[m][n-1] = 1;
  free (f->p);
  free (f);
  return;
}

void __attribute__((__noinline__)) setup_and_test_vla_1 (int n1, int n2, int m)
{
  struct foo {
    int n;
    int (*p)[n2][n1] __attribute__((counted_by(n)));
  } *f;

  f = (struct foo *) malloc (sizeof(struct foo));
  f->p = (int (*)[n2][n1]) malloc (m * sizeof (int[n2][n1]));
  f->n = m;
  f->p[m][n2][n1] = 1;
  free (f->p);
  free (f);
  return;
}

int main(int argc, char *argv[])
{
  setup_and_test_vla (10, 11);
  setup_and_test_vla_1 (10, 11, 20);
  return 0;
}