aboutsummaryrefslogtreecommitdiffstats
path: root/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/overflow-sub-1.c
blob: 15f04455e6eda8caa71d72064f5413b808622809 (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
/* { dg-do run } */
/* { dg-options "-fsanitize=signed-integer-overflow -Wno-unused-variable" } */

#include <stdio.h>

#define SCHAR_MAX __SCHAR_MAX__
#define SCHAR_MIN (-__SCHAR_MAX__ - 1)
#define SHRT_MAX __SHRT_MAX__
#define SHRT_MIN (-__SHRT_MAX__ - 1)
#define INT_MAX __INT_MAX__
#define INT_MIN (-__INT_MAX__ - 1)

void __attribute__((noinline,noclone))
check (int i, int j)
{
  if (i != j)
    __builtin_abort ();
}

int
main (void)
{
  fputs ("UBSAN TEST START\n", stderr);

#if __INT_MAX__ == 2147483647
  /* Here, nothing should fail.  */
  volatile int i = -1;
  volatile int j = INT_MIN;
  volatile int k = j - i;
  check (k, -2147483647);
  k = i - j;
  check (k, 2147483647);
  j++;
  check (j, -2147483647);

  i = 1;
  j = INT_MAX;
  k = i - j;
  check (k, -2147483646);
  k = j - i;
  check (k, 2147483646);
  j--;
  check (k, 2147483646);
#endif

  /* Test integer promotion.  */
#if __SCHAR_MAX__ == 127
  volatile signed char a = SCHAR_MIN;
  volatile signed char b = 1;
  volatile signed char c = a - b;
  check (c, 127);
  a--;
  check (a, 127);
#endif

#if __SHRT_MAX__ == 32767
  volatile short d = SHRT_MIN;
  volatile short e = 1;
  volatile short f = d - e;
  check (f, 32767);
  d--;
  check (d, 32767);
#endif

  fputs ("UBSAN TEST END\n", stderr);
  return 0;
}

/* { dg-output "UBSAN TEST START(\n|\r\n|\r)UBSAN TEST END" } */