aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/c-c++-common/ubsan/bounds-7.c
blob: e08a3fd80ec2e9baf964a74d545944132e9862b2 (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
52
53
54
/* { dg-do run } */
/* { dg-options "-fsanitize=bounds" } */

/* Test negative bounds.  */

struct S { int a[10]; };

__attribute__ ((noinline, noclone))
void
fn1 (void)
{
  volatile int i;
  int m = -1;
  volatile int a[7];
  asm ("" : : "r" (&a) : "memory");
  i = a[-1];
  asm ("" : : "r" (&a) : "memory");
  i = a[m];
}

__attribute__ ((noinline, noclone))
void
fn2 (void)
{
  volatile int i;
  int m = 7;
  volatile int a[m];
  asm ("" : : "r" (&a) : "memory");
  i = a[-1];
}

__attribute__ ((noinline, noclone))
void
fn3 (void)
{
  volatile int i;
  volatile struct S s;
  asm ("" : : "r" (&s.a) : "memory");
  i = s.a[-1];
}

int
main (void)
{
  fn1 ();
  fn2 ();
  fn3 ();
  return 0;
}

/* { dg-output "index -1 out of bounds for type 'int \\\[7\\\]'\[^\n\r]*(\n|\r\n|\r)" } */
/* { dg-output "\[^\n\r]*index -1 out of bounds for type 'int \\\[7\\\]'\[^\n\r]*(\n|\r\n|\r)" } */
/* { dg-output "\[^\n\r]*index -1 out of bounds for type 'int \\\[\\\*\\\]'\[^\n\r]*(\n|\r\n|\r)" } */
/* { dg-output "\[^\n\r]*index -1 out of bounds for type 'int \\\[10\\\]'" } */