modernc.org/cc@v1.0.1/v2/testdata/tcc-0.9.26/tests/boundtest.c (about) 1 #include <stdlib.h> 2 #include <stdio.h> 3 4 #define NB_ITS 1000000 5 //#define NB_ITS 1 6 #define TAB_SIZE 100 7 8 int tab[TAB_SIZE]; 9 int ret_sum; 10 char tab3[256]; 11 12 int test1(void) 13 { 14 int i, sum = 0; 15 for(i=0;i<TAB_SIZE;i++) { 16 sum += tab[i]; 17 } 18 return sum; 19 } 20 21 /* error */ 22 int test2(void) 23 { 24 int i, sum = 0; 25 for(i=0;i<TAB_SIZE + 1;i++) { 26 sum += tab[i]; 27 } 28 return sum; 29 } 30 31 /* actually, profiling test */ 32 int test3(void) 33 { 34 int sum; 35 int i, it; 36 37 sum = 0; 38 for(it=0;it<NB_ITS;it++) { 39 for(i=0;i<TAB_SIZE;i++) { 40 sum += tab[i]; 41 } 42 } 43 return sum; 44 } 45 46 /* ok */ 47 int test4(void) 48 { 49 int i, sum = 0; 50 int *tab4; 51 52 tab4 = malloc(20 * sizeof(int)); 53 for(i=0;i<20;i++) { 54 sum += tab4[i]; 55 } 56 free(tab4); 57 58 return sum; 59 } 60 61 /* error */ 62 int test5(void) 63 { 64 int i, sum = 0; 65 int *tab4; 66 67 tab4 = malloc(20 * sizeof(int)); 68 for(i=0;i<21;i++) { 69 sum += tab4[i]; 70 } 71 free(tab4); 72 73 return sum; 74 } 75 76 /* error */ 77 /* XXX: currently: bug */ 78 int test6(void) 79 { 80 int i, sum = 0; 81 int *tab4; 82 83 tab4 = malloc(20 * sizeof(int)); 84 free(tab4); 85 for(i=0;i<21;i++) { 86 sum += tab4[i]; 87 } 88 89 return sum; 90 } 91 92 /* error */ 93 int test7(void) 94 { 95 int i, sum = 0; 96 int *p; 97 98 for(i=0;i<TAB_SIZE + 1;i++) { 99 p = &tab[i]; 100 if (i == TAB_SIZE) 101 printf("i=%d %x\n", i, p); 102 sum += *p; 103 } 104 return sum; 105 } 106 107 /* ok */ 108 int test8(void) 109 { 110 int i, sum = 0; 111 int tab[10]; 112 113 for(i=0;i<10;i++) { 114 sum += tab[i]; 115 } 116 return sum; 117 } 118 119 /* error */ 120 int test9(void) 121 { 122 int i, sum = 0; 123 char tab[10]; 124 125 for(i=0;i<11;i++) { 126 sum += tab[i]; 127 } 128 return sum; 129 } 130 131 /* ok */ 132 int test10(void) 133 { 134 char tab[10]; 135 char tab1[10]; 136 137 memset(tab, 0, 10); 138 memcpy(tab, tab1, 10); 139 memmove(tab, tab1, 10); 140 return 0; 141 } 142 143 /* error */ 144 int test11(void) 145 { 146 char tab[10]; 147 148 memset(tab, 0, 11); 149 return 0; 150 } 151 152 /* error */ 153 int test12(void) 154 { 155 void *ptr; 156 ptr = malloc(10); 157 free(ptr); 158 free(ptr); 159 return 0; 160 } 161 162 /* error */ 163 int test13(void) 164 { 165 char pad1 = 0; 166 char tab[10]; 167 char pad2 = 0; 168 memset(tab, 'a', sizeof(tab)); 169 return strlen(tab); 170 } 171 172 int test14(void) 173 { 174 char *p = alloca(TAB_SIZE); 175 memset(p, 'a', TAB_SIZE); 176 p[TAB_SIZE-1] = 0; 177 return strlen(p); 178 } 179 180 /* error */ 181 int test15(void) 182 { 183 char *p = alloca(TAB_SIZE-1); 184 memset(p, 'a', TAB_SIZE); 185 p[TAB_SIZE-1] = 0; 186 return strlen(p); 187 } 188 189 int (*table_test[])(void) = { 190 test1, 191 test1, 192 test2, 193 test3, 194 test4, 195 test5, 196 test6, 197 test7, 198 test8, 199 test9, 200 test10, 201 test11, 202 test12, 203 test13, 204 test14, 205 test15, 206 }; 207 208 int main(int argc, char **argv) 209 { 210 int index; 211 int (*ftest)(void); 212 213 if (argc < 2) { 214 printf("usage: boundtest n\n" 215 "test TCC bound checking system\n" 216 ); 217 exit(1); 218 } 219 220 index = 0; 221 if (argc >= 2) 222 index = atoi(argv[1]); 223 /* well, we also use bounds on this ! */ 224 ftest = table_test[index]; 225 ftest(); 226 227 return 0; 228 } 229 230 /* 231 * without bound 0.77 s 232 * with bounds 4.73 233 */