travis: add -fsanitize=undefined
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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
diff --git a/.travis.yml b/.travis.yml
index bde3d08..d1ad089 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -86,7 +86,7 @@ matrix:
# GCC for the x64_32 architecture (32-bit longs and 32-bit pointers)
# TODO: Probably not possible to run anything in x32 in Travis
# but needs to be checked to be sure.
- - env: BUILDOPTIONS='--with-cc=gcc-5 --with-mx32'
+ - env: SANITIZER=1 BUILDOPTIONS='--with-cc=gcc-5 --with-mx32'
addons:
apt:
packages:
@@ -94,7 +94,7 @@ matrix:
- gcc-multilib
# GCC for the x86-64 architecture (64-bit longs and 64-bit pointers)
- - env: BUILDOPTIONS='--with-cc=gcc-5 --with-m64 --with-valgrind'
+ - env: SANITIZER=1 BUILDOPTIONS='--with-cc=gcc-5 --with-m64 --with-valgrind'
- env: BUILDOPTIONS='--with-cc=gcc-4.7 --with-m64 --with-valgrind'
addons:
apt:
@@ -112,13 +112,13 @@ matrix:
- gcc-4.9
# clang for x86-64 architecture (64-bit longs and 64-bit pointers)
- - env: CONV_WARNINGS=1 BUILDOPTIONS='--with-cc=clang-7 --with-m64 --with-valgrind'
- - env: BUILDOPTIONS='--with-cc=clang-6.0 --with-m64 --with-valgrind'
+ - env: SANITIZER=1 CONV_WARNINGS=1 BUILDOPTIONS='--with-cc=clang-7 --with-m64 --with-valgrind'
+ - env: SANITIZER=1 BUILDOPTIONS='--with-cc=clang-6.0 --with-m64 --with-valgrind'
addons:
apt:
packages:
- clang-6.0
- - env: BUILDOPTIONS='--with-cc=clang-5.0 --with-m64 --with-valgrind'
+ - env: SANITIZER=1 BUILDOPTIONS='--with-cc=clang-5.0 --with-m64 --with-valgrind'
addons:
apt:
packages:
@@ -132,34 +132,34 @@ matrix:
# GCC for the x86-64 architecture with restricted limb sizes
# formerly started with the option "--with-low-mp" to testme.sh
# but testing all three in one run took to long and timed out.
- - env: BUILDOPTIONS='--with-cc=gcc-5 --cflags=-DMP_8BIT --with-valgrind'
- - env: BUILDOPTIONS='--with-cc=gcc-5 --cflags=-DMP_16BIT --with-valgrind'
- - env: BUILDOPTIONS='--with-cc=gcc-5 --cflags=-DMP_32BIT --with-valgrind'
+ - env: SANITIZER=1 BUILDOPTIONS='--with-cc=gcc-5 --cflags=-DMP_8BIT --with-valgrind'
+ - env: SANITIZER=1 BUILDOPTIONS='--with-cc=gcc-5 --cflags=-DMP_16BIT --with-valgrind'
+ - env: SANITIZER=1 BUILDOPTIONS='--with-cc=gcc-5 --cflags=-DMP_32BIT --with-valgrind'
# clang for the x86-64 architecture with restricted limb sizes
- - env: BUILDOPTIONS='--with-cc=clang-7 --cflags=-DMP_8BIT --with-valgrind'
- - env: BUILDOPTIONS='--with-cc=clang-7 --cflags=-DMP_16BIT --with-valgrind'
- - env: BUILDOPTIONS='--with-cc=clang-7 --cflags=-DMP_32BIT --with-valgrind'
+ - env: SANITIZER=1 BUILDOPTIONS='--with-cc=clang-7 --cflags=-DMP_8BIT --with-valgrind'
+ - env: SANITIZER=1 BUILDOPTIONS='--with-cc=clang-7 --cflags=-DMP_16BIT --with-valgrind'
+ - env: SANITIZER=1 BUILDOPTIONS='--with-cc=clang-7 --cflags=-DMP_32BIT --with-valgrind'
# Test "autotuning", the automatic evaluation and setting of the Toom-Cook cut-offs.
- - env: BUILDOPTIONS='--with-cc=gcc-5 --cflags=-DMP_8BIT --with-valgrind --make-option=tune'
- - env: BUILDOPTIONS='--with-cc=gcc-5 --cflags=-DMP_16BIT --with-valgrind --make-option=tune'
- - env: BUILDOPTIONS='--with-cc=gcc-5 --cflags=-DMP_32BIT --with-valgrind --make-option=tune'
- - env: BUILDOPTIONS='--with-cc=gcc-5 --with-valgrind --make-option=tune'
- - env: BUILDOPTIONS='--with-cc=clang-7 --cflags=-DMP_8BIT --with-valgrind --make-option=tune'
- - env: BUILDOPTIONS='--with-cc=clang-7 --cflags=-DMP_16BIT --with-valgrind --make-option=tune'
- - env: BUILDOPTIONS='--with-cc=clang-7 --cflags=-DMP_32BIT --with-valgrind --make-option=tune'
- - env: BUILDOPTIONS='--with-cc=clang-7 --with-valgrind --make-option=tune'
+ - env: SANITIZER=1 BUILDOPTIONS='--with-cc=gcc-5 --cflags=-DMP_8BIT --with-valgrind --make-option=tune'
+ - env: SANITIZER=1 BUILDOPTIONS='--with-cc=gcc-5 --cflags=-DMP_16BIT --with-valgrind --make-option=tune'
+ - env: SANITIZER=1 BUILDOPTIONS='--with-cc=gcc-5 --cflags=-DMP_32BIT --with-valgrind --make-option=tune'
+ - env: SANITIZER=1 BUILDOPTIONS='--with-cc=gcc-5 --with-valgrind --make-option=tune'
+ - env: SANITIZER=1 BUILDOPTIONS='--with-cc=clang-7 --cflags=-DMP_8BIT --with-valgrind --make-option=tune'
+ - env: SANITIZER=1 BUILDOPTIONS='--with-cc=clang-7 --cflags=-DMP_16BIT --with-valgrind --make-option=tune'
+ - env: SANITIZER=1 BUILDOPTIONS='--with-cc=clang-7 --cflags=-DMP_32BIT --with-valgrind --make-option=tune'
+ - env: SANITIZER=1 BUILDOPTIONS='--with-cc=clang-7 --with-valgrind --make-option=tune'
# GCC for the x86-64 architecture testing against a different Bigint-implementation
# with 333333 different inputs.
- - env: BUILDOPTIONS='--with-cc=gcc-5 --test-vs-mtest=333333 --with-valgrind'
- - env: BUILDOPTIONS='--with-cc=clang-7 --test-vs-mtest=333333 --with-valgrind'
+ - env: SANITIZER=1 BUILDOPTIONS='--with-cc=gcc-5 --test-vs-mtest=333333 --with-valgrind'
+ - env: SANITIZER=1 BUILDOPTIONS='--with-cc=clang-7 --test-vs-mtest=333333 --with-valgrind'
# clang for the x86-64 architecture testing against a different Bigint-implementation
# with a better random source.
- - env: BUILDOPTIONS='--with-cc=gcc-5 --test-vs-mtest=333333 --mtest-real-rand --with-valgrind'
- - env: BUILDOPTIONS='--with-cc=clang-7 --test-vs-mtest=333333 --mtest-real-rand --with-valgrind'
+ - env: SANITIZER=1 BUILDOPTIONS='--with-cc=gcc-5 --test-vs-mtest=333333 --mtest-real-rand --with-valgrind'
+ - env: SANITIZER=1 BUILDOPTIONS='--with-cc=clang-7 --test-vs-mtest=333333 --mtest-real-rand --with-valgrind'
# Notifications go to
diff --git a/demo/test.c b/demo/test.c
index 2d0348f..00716e0 100644
--- a/demo/test.c
+++ b/demo/test.c
@@ -260,7 +260,7 @@ static int test_mp_complement(void)
}
for (i = 0; i < 1000; ++i) {
- long l = (rand() * rand() + 1) * (rand() % 1 ? -1 : 1);
+ long l = ((long)rand() * rand() + 1) * (rand() % 1 ? -1 : 1);
mp_set_long(&a, (unsigned long)labs(l));
if (l < 0)
mp_neg(&a, &a);
@@ -297,7 +297,7 @@ static int test_mp_tc_div_2d(void)
long l;
int em;
- l = (rand() * rand() + 1) * (rand() % 1 ? -1 : 1);
+ l = ((long)rand() * rand() + 1) * (rand() % 1 ? -1 : 1);
mp_set_long(&a, (unsigned long)labs(l));
if (l < 0)
mp_neg(&a, &a);
@@ -335,12 +335,12 @@ static int test_mp_tc_xor(void)
for (i = 0; i < 1000; ++i) {
int l, em;
- l = (rand() * rand() + 1) * (rand() % 1 ? -1 : 1);
+ l = ((long)rand() * rand() + 1) * (rand() % 1 ? -1 : 1);
mp_set_int(&a, (unsigned long)labs(l));
if (l < 0)
mp_neg(&a, &a);
- em = (rand() * rand() + 1) * (rand() % 1 ? -1 : 1);
+ em = ((long)rand() * rand() + 1) * (rand() % 1 ? -1 : 1);
mp_set_int(&b, (unsigned long)labs(em));
if (em < 0)
mp_neg(&b, &b);
@@ -376,12 +376,12 @@ static int test_mp_tc_or(void)
for (i = 0; i < 1000; ++i) {
long l, em;
- l = (rand() * rand() + 1) * (rand() % 1 ? -1 : 1);
+ l = ((long)rand() * rand() + 1) * (rand() % 1 ? -1 : 1);
mp_set_long(&a, (unsigned long)labs(l));
if (l < 0)
mp_neg(&a, &a);
- em = (rand() * rand() + 1) * (rand() % 1 ? -1 : 1);
+ em = ((long)rand() * rand() + 1) * (rand() % 1 ? -1 : 1);
mp_set_long(&b, (unsigned long)labs(em));
if (em < 0)
mp_neg(&b, &b);
@@ -416,12 +416,12 @@ static int test_mp_tc_and(void)
for (i = 0; i < 1000; ++i) {
long l, em;
- l = (rand() * rand() + 1) * (rand() % 1 ? -1 : 1);
+ l = ((long)rand() * rand() + 1) * (rand() % 1 ? -1 : 1);
mp_set_long(&a, (unsigned long)labs(l));
if (l < 0)
mp_neg(&a, &a);
- em = (rand() * rand() + 1) * (rand() % 1 ? -1 : 1);
+ em = ((long)rand() * rand() + 1) * (rand() % 1 ? -1 : 1);
mp_set_long(&b, (unsigned long)labs(em));
if (em < 0)
mp_neg(&b, &b);
@@ -558,7 +558,7 @@ static int test_mp_get_int(void)
}
for (i = 0; i < 1000; ++i) {
- t = (unsigned long)(rand() * rand() + 1) & 0xFFFFFFFFuL;
+ t = ((unsigned long)rand() * (unsigned long)rand() + 1uL) & 0xFFFFFFFFuL;
mp_set_int(&a, t);
if (t != mp_get_int(&a)) {
printf("\nmp_get_int() bad result!");
diff --git a/makefile_include.mk b/makefile_include.mk
index 161321a..d7d4cba 100644
--- a/makefile_include.mk
+++ b/makefile_include.mk
@@ -49,6 +49,10 @@ endif
CFLAGS += -I./ -Wall -Wsign-compare -Wextra -Wshadow
+ifdef SANITIZER
+CFLAGS += -fsanitize=undefined -fno-sanitize-recover=all -fno-sanitize=float-divide-by-zero
+endif
+
ifndef NO_ADDTL_WARNINGS
# additional warnings
CFLAGS += -Wdeclaration-after-statement -Wbad-function-cast -Wcast-align