summaryrefslogtreecommitdiff
path: root/gcc/testsuite/c-c++-common/ubsan/ptr-overflow-sanitization-1.c
blob: 14569d5a5c6106101a45fddb362b64da1b167b21 (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/* { dg-options "-O -fsanitize=pointer-overflow -fdump-tree-optimized" } */
/* { dg-skip-if "" { *-*-* } "-flto" } */

#define SMAX   __PTRDIFF_MAX__

void foo(void)
{
  char *p;
  char *p2;
  char b[1];
  char c[1];

  p = b + SMAX; /* pointer overflow check is needed */
  p = b;
  p++;
  p2 = p + 1000;
  p2 = p + 999;

  p = b + SMAX;
  p2 = p + 1; /* pointer overflow check is needed */

  p = b;
  p--; /* pointer overflow check is needed */
  p2 = p + 1;
  p2 = p + 2;

  p = b - SMAX; /* pointer overflow check is needed */
  p2 = p + (SMAX - 2); /* b - 2: no need to check this  */
  p2 = p + (SMAX - 1); /* b - 1: no need to check this */
  p2 = p + SMAX; /* b: no need to check this */
  p2++; /* b + 1 */

  p = c;
  p++; /* c + 1 */
  p = c - SMAX; /* pointer overflow check is needed */
  p2 = p + SMAX; /* c: pointer overflow check is needed */
  p2++; /* c + 1 */
}

void bar(char *ptr)
{
  char *p = ptr - 1000; /* pointer overflow check is needed */
  p = ptr + 1000; /* pointer overflow check is needed */
  p -= 2000; /* pointer overflow check is needed */
}

void baz(char *ptr)
{
  char **p = &ptr;
  char **p2 = p + 20; /* pointer overflow check is needed */
  p2--;
}

void positive_and_positive (char *ptr)
{
  char **p = &ptr;
  char **p2 = p + 100; /* pointer overflow check is needed */
  p2 = p + 10;
  p += 50; 
}

void negative_to_positive (char *ptr)
{
  char **p = &ptr;
  char **p2 = p + 20; /* pointer overflow check is needed */
  p2 = p - 10; /* pointer overflow check is needed */
  p2 += 15;
}

void negative_to_negative (char *ptr)
{
  char **p = &ptr;
  char **p2 = p - 20; /* pointer overflow check is needed */
  p2 = p - 20;
  p2 += 5;
}

/* { dg-final { scan-tree-dump-times "__ubsan_handle_pointer_overflow" 14 "optimized" } } */