aboutsummaryrefslogtreecommitdiffstats
path: root/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/overflow-add-1.c
diff options
context:
space:
mode:
Diffstat (limited to 'gcc-4.9/gcc/testsuite/c-c++-common/ubsan/overflow-add-1.c')
-rw-r--r--gcc-4.9/gcc/testsuite/c-c++-common/ubsan/overflow-add-1.c67
1 files changed, 67 insertions, 0 deletions
diff --git a/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/overflow-add-1.c b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/overflow-add-1.c
new file mode 100644
index 000000000..fd6c6d325
--- /dev/null
+++ b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/overflow-add-1.c
@@ -0,0 +1,67 @@
+/* { dg-do run } */
+/* { dg-options "-fsanitize=signed-integer-overflow -Wno-unused-variable" } */
+
+#include <stdio.h>
+
+#define SCHAR_MAX __SCHAR_MAX__
+#define SHRT_MAX __SHRT_MAX__
+#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 j = INT_MAX;
+ volatile int i = -1;
+ volatile int k = j + i;
+ check (k, 2147483646);
+ k = i + j;
+ check (k, 2147483646);
+ j--;
+ check (j, 2147483646);
+
+ i = 1;
+ j = INT_MIN;
+ k = i + j;
+ check (k, -2147483647);
+ k = j + i;
+ check (k, -2147483647);
+ j++;
+ check (j, -2147483647);
+#endif
+
+ /* Test integer promotion. */
+#if __SCHAR_MAX__ == 127
+ volatile signed char a = SCHAR_MAX;
+ volatile signed char b = 1;
+ volatile signed char c = a + b;
+ check (c, -128);
+ a++;
+ check (a, -128);
+#endif
+
+#if __SHRT_MAX__ == 32767
+ volatile short d = SHRT_MAX;
+ volatile short e = 1;
+ volatile short f = d + e;
+ check (f, -32768);
+ d++;
+ check (d, -32768);
+#endif
+
+ fputs ("UBSAN TEST END\n", stderr);
+ return 0;
+}
+
+/* { dg-output "UBSAN TEST START(\n|\r\n|\r)UBSAN TEST END" } */