Argyrios Kyrtzidis | 8a285ae | 2011-04-26 17:41:22 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -analyze -analyzer-checker=core,cplusplus.experimental.CString,deadcode.experimental.UnreachableCode -analyzer-store=region -Wno-null-dereference -verify %s |
| 2 | // RUN: %clang_cc1 -analyze -DUSE_BUILTINS -analyzer-checker=core,cplusplus.experimental.CString,deadcode.experimental.UnreachableCode -analyzer-store=region -Wno-null-dereference -verify %s |
| 3 | // RUN: %clang_cc1 -analyze -DVARIANT -analyzer-checker=core,cplusplus.experimental.CString,deadcode.experimental.UnreachableCode -analyzer-store=region -Wno-null-dereference -verify %s |
| 4 | // RUN: %clang_cc1 -analyze -DUSE_BUILTINS -DVARIANT -analyzer-checker=core,cplusplus.experimental.CString,deadcode.experimental.UnreachableCode -analyzer-store=region -Wno-null-dereference -verify %s |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 5 | |
| 6 | //===----------------------------------------------------------------------=== |
| 7 | // Declarations |
| 8 | //===----------------------------------------------------------------------=== |
| 9 | |
| 10 | // Some functions are so similar to each other that they follow the same code |
| 11 | // path, such as memcpy and __memcpy_chk, or memcmp and bcmp. If VARIANT is |
| 12 | // defined, make sure to use the variants instead to make sure they are still |
| 13 | // checked by the analyzer. |
| 14 | |
| 15 | // Some functions are implemented as builtins. These should be #defined as |
| 16 | // BUILTIN(f), which will prepend "__builtin_" if USE_BUILTINS is defined. |
| 17 | |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 18 | // Functions that have variants and are also available as builtins should be |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 19 | // declared carefully! See memcpy() for an example. |
| 20 | |
| 21 | #ifdef USE_BUILTINS |
| 22 | # define BUILTIN(f) __builtin_ ## f |
| 23 | #else /* USE_BUILTINS */ |
| 24 | # define BUILTIN(f) f |
| 25 | #endif /* USE_BUILTINS */ |
| 26 | |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 27 | #define NULL 0 |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 28 | typedef typeof(sizeof(int)) size_t; |
| 29 | |
| 30 | //===----------------------------------------------------------------------=== |
| 31 | // strlen() |
| 32 | //===----------------------------------------------------------------------=== |
| 33 | |
| 34 | #define strlen BUILTIN(strlen) |
| 35 | size_t strlen(const char *s); |
| 36 | |
| 37 | void strlen_constant0() { |
| 38 | if (strlen("123") != 3) |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 39 | (void)*(char*)0; // no-warning |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | void strlen_constant1() { |
| 43 | const char *a = "123"; |
| 44 | if (strlen(a) != 3) |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 45 | (void)*(char*)0; // no-warning |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | void strlen_constant2(char x) { |
| 49 | char a[] = "123"; |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 50 | if (strlen(a) != 3) |
| 51 | (void)*(char*)0; // no-warning |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 52 | a[0] = x; |
| 53 | if (strlen(a) != 3) |
| 54 | (void)*(char*)0; // expected-warning{{null}} |
| 55 | } |
| 56 | |
| 57 | size_t strlen_null() { |
| 58 | return strlen(0); // expected-warning{{Null pointer argument in call to byte string function}} |
| 59 | } |
| 60 | |
| 61 | size_t strlen_fn() { |
| 62 | return strlen((char*)&strlen_fn); // expected-warning{{Argument to byte string function is the address of the function 'strlen_fn', which is not a null-terminated string}} |
| 63 | } |
| 64 | |
| 65 | size_t strlen_nonloc() { |
| 66 | label: |
| 67 | return strlen((char*)&&label); // expected-warning{{Argument to byte string function is the address of the label 'label', which is not a null-terminated string}} |
| 68 | } |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 69 | |
| 70 | void strlen_subregion() { |
Carl Norum | e224ba7 | 2011-03-07 22:57:45 +0000 | [diff] [blame] | 71 | struct two_strings { char a[2], b[2]; }; |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 72 | extern void use_two_strings(struct two_strings *); |
| 73 | |
| 74 | struct two_strings z; |
| 75 | use_two_strings(&z); |
| 76 | |
| 77 | size_t a = strlen(z.a); |
| 78 | z.b[0] = 5; |
| 79 | size_t b = strlen(z.a); |
| 80 | if (a == 0 && b != 0) |
| 81 | (void)*(char*)0; // expected-warning{{never executed}} |
| 82 | |
| 83 | use_two_strings(&z); |
| 84 | |
| 85 | size_t c = strlen(z.a); |
| 86 | if (a == 0 && c != 0) |
| 87 | (void)*(char*)0; // expected-warning{{null}} |
| 88 | } |
| 89 | |
| 90 | extern void use_string(char *); |
| 91 | void strlen_argument(char *x) { |
| 92 | size_t a = strlen(x); |
| 93 | size_t b = strlen(x); |
| 94 | if (a == 0 && b != 0) |
| 95 | (void)*(char*)0; // expected-warning{{never executed}} |
| 96 | |
| 97 | use_string(x); |
| 98 | |
| 99 | size_t c = strlen(x); |
| 100 | if (a == 0 && c != 0) |
| 101 | (void)*(char*)0; // expected-warning{{null}} |
| 102 | } |
| 103 | |
| 104 | extern char global_str[]; |
| 105 | void strlen_global() { |
| 106 | size_t a = strlen(global_str); |
| 107 | size_t b = strlen(global_str); |
| 108 | if (a == 0 && b != 0) |
| 109 | (void)*(char*)0; // expected-warning{{never executed}} |
| 110 | |
| 111 | // Call a function with unknown effects, which should invalidate globals. |
| 112 | use_string(0); |
| 113 | |
| 114 | size_t c = strlen(global_str); |
| 115 | if (a == 0 && c != 0) |
| 116 | (void)*(char*)0; // expected-warning{{null}} |
| 117 | } |
| 118 | |
| 119 | void strlen_indirect(char *x) { |
| 120 | size_t a = strlen(x); |
| 121 | char *p = x; |
| 122 | char **p2 = &p; |
| 123 | size_t b = strlen(x); |
| 124 | if (a == 0 && b != 0) |
| 125 | (void)*(char*)0; // expected-warning{{never executed}} |
| 126 | |
| 127 | extern void use_string_ptr(char*const*); |
| 128 | use_string_ptr(p2); |
| 129 | |
| 130 | size_t c = strlen(x); |
| 131 | if (a == 0 && c != 0) |
| 132 | (void)*(char*)0; // expected-warning{{null}} |
| 133 | } |
| 134 | |
| 135 | void strlen_liveness(const char *x) { |
| 136 | if (strlen(x) < 5) |
| 137 | return; |
| 138 | if (strlen(x) < 5) |
| 139 | (void)*(char*)0; // no-warning |
| 140 | } |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 141 | |
| 142 | //===----------------------------------------------------------------------=== |
Ted Kremenek | be4242c | 2011-02-22 04:55:05 +0000 | [diff] [blame] | 143 | // strnlen() |
| 144 | //===----------------------------------------------------------------------=== |
| 145 | |
| 146 | #define strnlen BUILTIN(strnlen) |
| 147 | size_t strnlen(const char *s, size_t maxlen); |
| 148 | |
| 149 | void strnlen_constant0() { |
| 150 | if (strnlen("123", 10) != 3) |
| 151 | (void)*(char*)0; // no-warning |
| 152 | } |
| 153 | |
| 154 | void strnlen_constant1() { |
| 155 | const char *a = "123"; |
| 156 | if (strnlen(a, 10) != 3) |
| 157 | (void)*(char*)0; // no-warning |
| 158 | } |
| 159 | |
| 160 | void strnlen_constant2(char x) { |
| 161 | char a[] = "123"; |
| 162 | if (strnlen(a, 10) != 3) |
| 163 | (void)*(char*)0; // no-warning |
| 164 | a[0] = x; |
| 165 | if (strnlen(a, 10) != 3) |
| 166 | (void)*(char*)0; // expected-warning{{null}} |
| 167 | } |
| 168 | |
| 169 | void strnlen_constant4() { |
| 170 | if (strnlen("123456", 3) != 3) |
| 171 | (void)*(char*)0; // no-warning |
| 172 | } |
| 173 | |
| 174 | void strnlen_constant5() { |
| 175 | const char *a = "123456"; |
| 176 | if (strnlen(a, 3) != 3) |
| 177 | (void)*(char*)0; // no-warning |
| 178 | } |
| 179 | |
| 180 | void strnlen_constant6(char x) { |
| 181 | char a[] = "123456"; |
| 182 | if (strnlen(a, 3) != 3) |
| 183 | (void)*(char*)0; // no-warning |
| 184 | a[0] = x; |
| 185 | if (strnlen(a, 3) != 3) |
| 186 | (void)*(char*)0; // expected-warning{{null}} |
| 187 | } |
| 188 | |
| 189 | size_t strnlen_null() { |
| 190 | return strnlen(0, 3); // expected-warning{{Null pointer argument in call to byte string function}} |
| 191 | } |
| 192 | |
| 193 | size_t strnlen_fn() { |
| 194 | return strnlen((char*)&strlen_fn, 3); // expected-warning{{Argument to byte string function is the address of the function 'strlen_fn', which is not a null-terminated string}} |
| 195 | } |
| 196 | |
| 197 | size_t strnlen_nonloc() { |
| 198 | label: |
| 199 | return strnlen((char*)&&label, 3); // expected-warning{{Argument to byte string function is the address of the label 'label', which is not a null-terminated string}} |
| 200 | } |
| 201 | |
| 202 | void strnlen_subregion() { |
Carl Norum | e224ba7 | 2011-03-07 22:57:45 +0000 | [diff] [blame] | 203 | struct two_stringsn { char a[2], b[2]; }; |
Ted Kremenek | be4242c | 2011-02-22 04:55:05 +0000 | [diff] [blame] | 204 | extern void use_two_stringsn(struct two_stringsn *); |
| 205 | |
| 206 | struct two_stringsn z; |
| 207 | use_two_stringsn(&z); |
| 208 | |
| 209 | size_t a = strnlen(z.a, 10); |
| 210 | z.b[0] = 5; |
| 211 | size_t b = strnlen(z.a, 10); |
| 212 | if (a == 0 && b != 0) |
| 213 | (void)*(char*)0; // expected-warning{{never executed}} |
| 214 | |
| 215 | use_two_stringsn(&z); |
| 216 | |
| 217 | size_t c = strnlen(z.a, 10); |
| 218 | if (a == 0 && c != 0) |
| 219 | (void)*(char*)0; // expected-warning{{null}} |
| 220 | } |
| 221 | |
| 222 | extern void use_stringn(char *); |
| 223 | void strnlen_argument(char *x) { |
| 224 | size_t a = strnlen(x, 10); |
| 225 | size_t b = strnlen(x, 10); |
| 226 | if (a == 0 && b != 0) |
| 227 | (void)*(char*)0; // expected-warning{{never executed}} |
| 228 | |
| 229 | use_stringn(x); |
| 230 | |
| 231 | size_t c = strnlen(x, 10); |
| 232 | if (a == 0 && c != 0) |
| 233 | (void)*(char*)0; // expected-warning{{null}} |
| 234 | } |
| 235 | |
| 236 | extern char global_strn[]; |
| 237 | void strnlen_global() { |
| 238 | size_t a = strnlen(global_strn, 10); |
| 239 | size_t b = strnlen(global_strn, 10); |
| 240 | if (a == 0 && b != 0) |
| 241 | (void)*(char*)0; // expected-warning{{never executed}} |
| 242 | |
| 243 | // Call a function with unknown effects, which should invalidate globals. |
| 244 | use_stringn(0); |
| 245 | |
| 246 | size_t c = strnlen(global_str, 10); |
| 247 | if (a == 0 && c != 0) |
| 248 | (void)*(char*)0; // expected-warning{{null}} |
| 249 | } |
| 250 | |
| 251 | void strnlen_indirect(char *x) { |
| 252 | size_t a = strnlen(x, 10); |
| 253 | char *p = x; |
| 254 | char **p2 = &p; |
| 255 | size_t b = strnlen(x, 10); |
| 256 | if (a == 0 && b != 0) |
| 257 | (void)*(char*)0; // expected-warning{{never executed}} |
| 258 | |
| 259 | extern void use_stringn_ptr(char*const*); |
| 260 | use_stringn_ptr(p2); |
| 261 | |
| 262 | size_t c = strnlen(x, 10); |
| 263 | if (a == 0 && c != 0) |
| 264 | (void)*(char*)0; // expected-warning{{null}} |
| 265 | } |
| 266 | |
| 267 | void strnlen_liveness(const char *x) { |
| 268 | if (strnlen(x, 10) < 5) |
| 269 | return; |
| 270 | if (strnlen(x, 10) < 5) |
| 271 | (void)*(char*)0; // no-warning |
| 272 | } |
| 273 | |
| 274 | //===----------------------------------------------------------------------=== |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 275 | // strcpy() |
| 276 | //===----------------------------------------------------------------------=== |
| 277 | |
| 278 | #ifdef VARIANT |
| 279 | |
| 280 | #define __strcpy_chk BUILTIN(__strcpy_chk) |
| 281 | char *__strcpy_chk(char *restrict s1, const char *restrict s2, size_t destlen); |
| 282 | |
| 283 | #define strcpy(a,b) __strcpy_chk(a,b,(size_t)-1) |
| 284 | |
| 285 | #else /* VARIANT */ |
| 286 | |
| 287 | #define strcpy BUILTIN(strcpy) |
| 288 | char *strcpy(char *restrict s1, const char *restrict s2); |
| 289 | |
| 290 | #endif /* VARIANT */ |
| 291 | |
| 292 | |
| 293 | void strcpy_null_dst(char *x) { |
| 294 | strcpy(NULL, x); // expected-warning{{Null pointer argument in call to byte string function}} |
| 295 | } |
| 296 | |
| 297 | void strcpy_null_src(char *x) { |
| 298 | strcpy(x, NULL); // expected-warning{{Null pointer argument in call to byte string function}} |
| 299 | } |
| 300 | |
| 301 | void strcpy_fn(char *x) { |
| 302 | strcpy(x, (char*)&strcpy_fn); // expected-warning{{Argument to byte string function is the address of the function 'strcpy_fn', which is not a null-terminated string}} |
| 303 | } |
| 304 | |
| 305 | void strcpy_effects(char *x, char *y) { |
| 306 | char a = x[0]; |
| 307 | |
| 308 | if (strcpy(x, y) != x) |
| 309 | (void)*(char*)0; // no-warning |
| 310 | |
| 311 | if (strlen(x) != strlen(y)) |
| 312 | (void)*(char*)0; // no-warning |
| 313 | |
| 314 | if (a != x[0]) |
| 315 | (void)*(char*)0; // expected-warning{{null}} |
| 316 | } |
| 317 | |
| 318 | void strcpy_overflow(char *y) { |
| 319 | char x[4]; |
| 320 | if (strlen(y) == 4) |
| 321 | strcpy(x, y); // expected-warning{{Byte string function overflows destination buffer}} |
| 322 | } |
| 323 | |
| 324 | void strcpy_no_overflow(char *y) { |
| 325 | char x[4]; |
| 326 | if (strlen(y) == 3) |
| 327 | strcpy(x, y); // no-warning |
| 328 | } |
| 329 | |
| 330 | //===----------------------------------------------------------------------=== |
Ted Kremenek | 0ef473f | 2011-02-22 04:58:34 +0000 | [diff] [blame] | 331 | // strncpy() |
| 332 | //===----------------------------------------------------------------------=== |
| 333 | |
| 334 | #ifdef VARIANT |
| 335 | |
| 336 | #define __strncpy_chk BUILTIN(__strncpy_chk) |
| 337 | char *__strncpy_chk(char *restrict s1, const char *restrict s2, size_t n, size_t destlen); |
| 338 | |
| 339 | #define strncpy(a,b,c) __strncpy_chk(a,b,c, (size_t)-1) |
| 340 | |
| 341 | #else /* VARIANT */ |
| 342 | |
| 343 | #define strncpy BUILTIN(strncpy) |
| 344 | char *strncpy(char *restrict s1, const char *restrict s2, size_t n); |
| 345 | |
| 346 | #endif /* VARIANT */ |
| 347 | |
| 348 | |
| 349 | void strncpy_null_dst(char *x) { |
| 350 | strncpy(NULL, x, 1); // expected-warning{{Null pointer argument in call to byte string function}} |
| 351 | } |
| 352 | |
| 353 | void strncpy_null_src(char *x) { |
| 354 | strncpy(x, NULL, 1); // expected-warning{{Null pointer argument in call to byte string function}} |
| 355 | } |
| 356 | |
| 357 | void strncpy_fn(char *x) { |
| 358 | strncpy(x, (char*)&strncpy_fn, 1); // expected-warning{{Argument to byte string function is the address of the function 'strncpy_fn', which is not a null-terminated string}} |
| 359 | } |
| 360 | |
| 361 | void strncpy_effects(char *x, char *y) { |
| 362 | char a = x[0]; |
| 363 | |
| 364 | if (strncpy(x, y, strlen(y)) != x) |
| 365 | (void)*(char*)0; // no-warning |
| 366 | |
| 367 | if (strlen(x) != strlen(y)) |
| 368 | (void)*(char*)0; // no-warning |
| 369 | |
| 370 | if (a != x[0]) |
| 371 | (void)*(char*)0; // expected-warning{{null}} |
| 372 | } |
| 373 | |
| 374 | void strncpy_overflow(char *y) { |
| 375 | char x[4]; |
| 376 | if (strlen(y) == 4) |
| 377 | strncpy(x, y, strlen(y)); // expected-warning{{Byte string function overflows destination buffer}} |
| 378 | } |
| 379 | |
| 380 | void strncpy_len_overflow(char *y) { |
| 381 | char x[4]; |
| 382 | if (strlen(y) == 3) |
| 383 | strncpy(x, y, sizeof(x)); // no-warning |
| 384 | } |
| 385 | |
| 386 | void strncpy_no_overflow(char *y) { |
| 387 | char x[4]; |
| 388 | if (strlen(y) == 3) |
| 389 | strncpy(x, y, strlen(y)); // no-warning |
| 390 | } |
| 391 | |
| 392 | void strncpy_no_len_overflow(char *y) { |
| 393 | char x[4]; |
| 394 | if (strlen(y) == 4) |
| 395 | strncpy(x, y, sizeof(x)-1); // no-warning |
| 396 | } |
| 397 | |
| 398 | //===----------------------------------------------------------------------=== |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 399 | // stpcpy() |
| 400 | //===----------------------------------------------------------------------=== |
| 401 | |
| 402 | #ifdef VARIANT |
| 403 | |
| 404 | #define __stpcpy_chk BUILTIN(__stpcpy_chk) |
| 405 | char *__stpcpy_chk(char *restrict s1, const char *restrict s2, size_t destlen); |
| 406 | |
| 407 | #define stpcpy(a,b) __stpcpy_chk(a,b,(size_t)-1) |
| 408 | |
| 409 | #else /* VARIANT */ |
| 410 | |
| 411 | #define stpcpy BUILTIN(stpcpy) |
| 412 | char *stpcpy(char *restrict s1, const char *restrict s2); |
| 413 | |
| 414 | #endif /* VARIANT */ |
| 415 | |
| 416 | |
| 417 | void stpcpy_effect(char *x, char *y) { |
| 418 | char a = x[0]; |
| 419 | |
| 420 | if (stpcpy(x, y) != &x[strlen(y)]) |
| 421 | (void)*(char*)0; // no-warning |
| 422 | |
| 423 | if (strlen(x) != strlen(y)) |
| 424 | (void)*(char*)0; // no-warning |
| 425 | |
| 426 | if (a != x[0]) |
| 427 | (void)*(char*)0; // expected-warning{{null}} |
| 428 | } |
| 429 | |
| 430 | void stpcpy_overflow(char *y) { |
| 431 | char x[4]; |
| 432 | if (strlen(y) == 4) |
| 433 | stpcpy(x, y); // expected-warning{{Byte string function overflows destination buffer}} |
| 434 | } |
| 435 | |
| 436 | void stpcpy_no_overflow(char *y) { |
| 437 | char x[4]; |
| 438 | if (strlen(y) == 3) |
| 439 | stpcpy(x, y); // no-warning |
| 440 | } |
Lenny Maiorani | 067bbd0 | 2011-04-09 15:12:58 +0000 | [diff] [blame] | 441 | |
| 442 | //===----------------------------------------------------------------------=== |
| 443 | // strcat() |
| 444 | //===----------------------------------------------------------------------=== |
| 445 | |
| 446 | #ifdef VARIANT |
| 447 | |
| 448 | #define __strcat_chk BUILTIN(__strcat_chk) |
| 449 | char *__strcat_chk(char *restrict s1, const char *restrict s2, size_t destlen); |
| 450 | |
| 451 | #define strcat(a,b) __strcat_chk(a,b,(size_t)-1) |
| 452 | |
| 453 | #else /* VARIANT */ |
| 454 | |
| 455 | #define strcat BUILTIN(strcat) |
| 456 | char *strcat(char *restrict s1, const char *restrict s2); |
| 457 | |
| 458 | #endif /* VARIANT */ |
| 459 | |
| 460 | |
| 461 | void strcat_null_dst(char *x) { |
| 462 | strcat(NULL, x); // expected-warning{{Null pointer argument in call to byte string function}} |
| 463 | } |
| 464 | |
| 465 | void strcat_null_src(char *x) { |
| 466 | strcat(x, NULL); // expected-warning{{Null pointer argument in call to byte string function}} |
| 467 | } |
| 468 | |
| 469 | void strcat_fn(char *x) { |
| 470 | strcat(x, (char*)&strcat_fn); // expected-warning{{Argument to byte string function is the address of the function 'strcat_fn', which is not a null-terminated string}} |
| 471 | } |
| 472 | |
| 473 | void strcat_effects(char *y) { |
| 474 | char x[8] = "123"; |
| 475 | size_t orig_len = strlen(x); |
| 476 | char a = x[0]; |
| 477 | |
| 478 | if (strlen(y) != 4) |
| 479 | return; |
| 480 | |
| 481 | if (strcat(x, y) != x) |
| 482 | (void)*(char*)0; // no-warning |
| 483 | |
| 484 | if ((int)strlen(x) != (orig_len + strlen(y))) |
| 485 | (void)*(char*)0; // no-warning |
| 486 | |
| 487 | if (a != x[0]) |
| 488 | (void)*(char*)0; // expected-warning{{null}} |
| 489 | } |
| 490 | |
| 491 | void strcat_overflow_0(char *y) { |
| 492 | char x[4] = "12"; |
| 493 | if (strlen(y) == 4) |
| 494 | strcat(x, y); // expected-warning{{Byte string function overflows destination buffer}} |
| 495 | } |
| 496 | |
| 497 | void strcat_overflow_1(char *y) { |
| 498 | char x[4] = "12"; |
| 499 | if (strlen(y) == 3) |
| 500 | strcat(x, y); // expected-warning{{Byte string function overflows destination buffer}} |
| 501 | } |
| 502 | |
| 503 | void strcat_overflow_2(char *y) { |
| 504 | char x[4] = "12"; |
| 505 | if (strlen(y) == 2) |
| 506 | strcat(x, y); // expected-warning{{Byte string function overflows destination buffer}} |
| 507 | } |
| 508 | |
| 509 | void strcat_no_overflow(char *y) { |
| 510 | char x[5] = "12"; |
| 511 | if (strlen(y) == 2) |
| 512 | strcat(x, y); // no-warning |
| 513 | } |
| 514 | |
| 515 | |
| 516 | //===----------------------------------------------------------------------=== |
| 517 | // strncat() |
| 518 | //===----------------------------------------------------------------------=== |
| 519 | |
| 520 | #ifdef VARIANT |
| 521 | |
| 522 | #define __strncat_chk BUILTIN(__strncat_chk) |
| 523 | char *__strncat_chk(char *restrict s1, const char *restrict s2, size_t n, size_t destlen); |
| 524 | |
| 525 | #define strncat(a,b,c) __strncat_chk(a,b,c, (size_t)-1) |
| 526 | |
| 527 | #else /* VARIANT */ |
| 528 | |
| 529 | #define strncat BUILTIN(strncat) |
| 530 | char *strncat(char *restrict s1, const char *restrict s2, size_t n); |
| 531 | |
| 532 | #endif /* VARIANT */ |
| 533 | |
| 534 | |
| 535 | void strncat_null_dst(char *x) { |
| 536 | strncat(NULL, x, 4); // expected-warning{{Null pointer argument in call to byte string function}} |
| 537 | } |
| 538 | |
| 539 | void strncat_null_src(char *x) { |
| 540 | strncat(x, NULL, 4); // expected-warning{{Null pointer argument in call to byte string function}} |
| 541 | } |
| 542 | |
| 543 | void strncat_fn(char *x) { |
| 544 | strncat(x, (char*)&strncat_fn, 4); // expected-warning{{Argument to byte string function is the address of the function 'strncat_fn', which is not a null-terminated string}} |
| 545 | } |
| 546 | |
| 547 | void strncat_effects(char *y) { |
| 548 | char x[8] = "123"; |
| 549 | size_t orig_len = strlen(x); |
| 550 | char a = x[0]; |
| 551 | |
| 552 | if (strlen(y) != 4) |
| 553 | return; |
| 554 | |
| 555 | if (strncat(x, y, strlen(y)) != x) |
| 556 | (void)*(char*)0; // no-warning |
| 557 | |
| 558 | if (strlen(x) != orig_len + strlen(y)) |
| 559 | (void)*(char*)0; // no-warning |
| 560 | |
| 561 | if (a != x[0]) |
| 562 | (void)*(char*)0; // expected-warning{{null}} |
| 563 | } |
| 564 | |
| 565 | void strncat_overflow_0(char *y) { |
| 566 | char x[4] = "12"; |
| 567 | if (strlen(y) == 4) |
| 568 | strncat(x, y, strlen(y)); // expected-warning{{Byte string function overflows destination buffer}} |
| 569 | } |
| 570 | |
| 571 | void strncat_overflow_1(char *y) { |
| 572 | char x[4] = "12"; |
| 573 | if (strlen(y) == 3) |
| 574 | strncat(x, y, strlen(y)); // expected-warning{{Byte string function overflows destination buffer}} |
| 575 | } |
| 576 | |
| 577 | void strncat_overflow_2(char *y) { |
| 578 | char x[4] = "12"; |
| 579 | if (strlen(y) == 2) |
| 580 | strncat(x, y, strlen(y)); // expected-warning{{Byte string function overflows destination buffer}} |
| 581 | } |
| 582 | |
| 583 | void strncat_overflow_3(char *y) { |
| 584 | char x[4] = "12"; |
| 585 | if (strlen(y) == 4) |
| 586 | strncat(x, y, 2); // expected-warning{{Byte string function overflows destination buffer}} |
| 587 | } |
| 588 | void strncat_no_overflow_1(char *y) { |
| 589 | char x[5] = "12"; |
| 590 | if (strlen(y) == 2) |
| 591 | strncat(x, y, strlen(y)); // no-warning |
| 592 | } |
| 593 | |
| 594 | void strncat_no_overflow_2(char *y) { |
| 595 | char x[4] = "12"; |
| 596 | if (strlen(y) == 4) |
| 597 | strncat(x, y, 1); // no-warning |
| 598 | } |
Lenny Maiorani | 318dd92 | 2011-04-12 17:08:43 +0000 | [diff] [blame] | 599 | |
| 600 | //===----------------------------------------------------------------------=== |
| 601 | // strcmp() |
| 602 | //===----------------------------------------------------------------------=== |
| 603 | |
| 604 | #define strcmp BUILTIN(strcmp) |
| 605 | int strcmp(const char *restrict s1, const char *restrict s2); |
| 606 | |
| 607 | void strcmp_constant0() { |
| 608 | if (strcmp("123", "123") != 0) |
| 609 | (void)*(char*)0; // no-warning |
| 610 | } |
| 611 | |
| 612 | void strcmp_constant_and_var_0() { |
| 613 | char *x = "123"; |
| 614 | if (strcmp(x, "123") != 0) |
| 615 | (void)*(char*)0; // no-warning |
| 616 | } |
| 617 | |
| 618 | void strcmp_constant_and_var_1() { |
| 619 | char *x = "123"; |
| 620 | if (strcmp("123", x) != 0) |
| 621 | (void)*(char*)0; // no-warning |
| 622 | } |
| 623 | |
| 624 | void strcmp_0() { |
| 625 | char *x = "123"; |
| 626 | char *y = "123"; |
| 627 | if (strcmp(x, y) != 0) |
| 628 | (void)*(char*)0; // no-warning |
| 629 | } |
| 630 | |
| 631 | void strcmp_1() { |
| 632 | char *x = "234"; |
| 633 | char *y = "123"; |
| 634 | if (strcmp(x, y) != 1) |
| 635 | (void)*(char*)0; // no-warning |
| 636 | } |
| 637 | |
| 638 | void strcmp_2() { |
| 639 | char *x = "123"; |
| 640 | char *y = "234"; |
| 641 | if (strcmp(x, y) != -1) |
| 642 | (void)*(char*)0; // no-warning |
| 643 | } |
| 644 | |
| 645 | void strcmp_null_0() { |
| 646 | char *x = NULL; |
| 647 | char *y = "123"; |
| 648 | strcmp(x, y); // expected-warning{{Null pointer argument in call to byte string function}} |
| 649 | } |
| 650 | |
| 651 | void strcmp_null_1() { |
| 652 | char *x = "123"; |
| 653 | char *y = NULL; |
| 654 | strcmp(x, y); // expected-warning{{Null pointer argument in call to byte string function}} |
| 655 | } |
| 656 | |
| 657 | void strcmp_diff_length_0() { |
| 658 | char *x = "12345"; |
| 659 | char *y = "234"; |
| 660 | if (strcmp(x, y) != -1) |
| 661 | (void)*(char*)0; // no-warning |
| 662 | } |
| 663 | |
| 664 | void strcmp_diff_length_1() { |
| 665 | char *x = "123"; |
| 666 | char *y = "23456"; |
| 667 | if (strcmp(x, y) != -1) |
| 668 | (void)*(char*)0; // no-warning |
| 669 | } |
| 670 | |
| 671 | void strcmp_diff_length_2() { |
| 672 | char *x = "12345"; |
| 673 | char *y = "123"; |
| 674 | if (strcmp(x, y) != 1) |
| 675 | (void)*(char*)0; // no-warning |
| 676 | } |
| 677 | |
| 678 | void strcmp_diff_length_3() { |
| 679 | char *x = "123"; |
| 680 | char *y = "12345"; |
| 681 | if (strcmp(x, y) != -1) |
| 682 | (void)*(char*)0; // no-warning |
| 683 | } |
| 684 | |
Lenny Maiorani | 357f6ee | 2011-04-25 22:21:00 +0000 | [diff] [blame] | 685 | //===----------------------------------------------------------------------=== |
| 686 | // strncmp() |
| 687 | //===----------------------------------------------------------------------=== |
| 688 | |
| 689 | #define strncmp BUILTIN(strncmp) |
| 690 | int strncmp(const char *restrict s1, const char *restrict s2, size_t n); |
| 691 | |
| 692 | void strncmp_constant0() { |
| 693 | if (strncmp("123", "123", 3) != 0) |
| 694 | (void)*(char*)0; // no-warning |
| 695 | } |
| 696 | |
| 697 | void strncmp_constant_and_var_0() { |
| 698 | char *x = "123"; |
| 699 | if (strncmp(x, "123", 3) != 0) |
| 700 | (void)*(char*)0; // no-warning |
| 701 | } |
| 702 | |
| 703 | void strncmp_constant_and_var_1() { |
| 704 | char *x = "123"; |
| 705 | if (strncmp("123", x, 3) != 0) |
| 706 | (void)*(char*)0; // no-warning |
| 707 | } |
| 708 | |
| 709 | void strncmp_0() { |
| 710 | char *x = "123"; |
| 711 | char *y = "123"; |
| 712 | if (strncmp(x, y, 3) != 0) |
| 713 | (void)*(char*)0; // no-warning |
| 714 | } |
| 715 | |
| 716 | void strncmp_1() { |
| 717 | char *x = "234"; |
| 718 | char *y = "123"; |
| 719 | if (strncmp(x, y, 3) != 1) |
| 720 | (void)*(char*)0; // no-warning |
| 721 | } |
| 722 | |
| 723 | void strncmp_2() { |
| 724 | char *x = "123"; |
| 725 | char *y = "234"; |
| 726 | if (strncmp(x, y, 3) != -1) |
| 727 | (void)*(char*)0; // no-warning |
| 728 | } |
| 729 | |
| 730 | void strncmp_null_0() { |
| 731 | char *x = NULL; |
| 732 | char *y = "123"; |
| 733 | strncmp(x, y, 3); // expected-warning{{Null pointer argument in call to byte string function}} |
| 734 | } |
| 735 | |
| 736 | void strncmp_null_1() { |
| 737 | char *x = "123"; |
| 738 | char *y = NULL; |
| 739 | strncmp(x, y, 3); // expected-warning{{Null pointer argument in call to byte string function}} |
| 740 | } |
| 741 | |
| 742 | void strncmp_diff_length_0() { |
| 743 | char *x = "12345"; |
| 744 | char *y = "234"; |
| 745 | if (strncmp(x, y, 5) != -1) |
| 746 | (void)*(char*)0; // no-warning |
| 747 | } |
| 748 | |
| 749 | void strncmp_diff_length_1() { |
| 750 | char *x = "123"; |
| 751 | char *y = "23456"; |
| 752 | if (strncmp(x, y, 5) != -1) |
| 753 | (void)*(char*)0; // no-warning |
| 754 | } |
| 755 | |
| 756 | void strncmp_diff_length_2() { |
| 757 | char *x = "12345"; |
| 758 | char *y = "123"; |
| 759 | if (strncmp(x, y, 5) != 1) |
| 760 | (void)*(char*)0; // no-warning |
| 761 | } |
| 762 | |
| 763 | void strncmp_diff_length_3() { |
| 764 | char *x = "123"; |
| 765 | char *y = "12345"; |
| 766 | if (strncmp(x, y, 5) != -1) |
| 767 | (void)*(char*)0; // no-warning |
| 768 | } |
| 769 | |
| 770 | void strncmp_diff_length_4() { |
| 771 | char *x = "123"; |
| 772 | char *y = "12345"; |
| 773 | if (strncmp(x, y, 3) != 0) |
| 774 | (void)*(char*)0; // no-warning |
| 775 | } |
| 776 | |
| 777 | void strncmp_diff_length_5() { |
| 778 | char *x = "012"; |
| 779 | char *y = "12345"; |
| 780 | if (strncmp(x, y, 3) != -1) |
| 781 | (void)*(char*)0; // no-warning |
| 782 | } |
| 783 | |
| 784 | void strncmp_diff_length_6() { |
| 785 | char *x = "234"; |
| 786 | char *y = "12345"; |
| 787 | if (strncmp(x, y, 3) != 1) |
| 788 | (void)*(char*)0; // no-warning |
| 789 | } |
| 790 | |
Lenny Maiorani | bd1d16a | 2011-04-28 15:09:11 +0000 | [diff] [blame] | 791 | //===----------------------------------------------------------------------=== |
| 792 | // strcasecmp() |
| 793 | //===----------------------------------------------------------------------=== |
| 794 | |
| 795 | #define strcasecmp BUILTIN(strcasecmp) |
| 796 | int strcasecmp(const char *restrict s1, const char *restrict s2); |
| 797 | |
| 798 | void strcasecmp_constant0() { |
| 799 | if (strcasecmp("abc", "Abc") != 0) |
| 800 | (void)*(char*)0; // no-warning |
| 801 | } |
| 802 | |
| 803 | void strcasecmp_constant_and_var_0() { |
| 804 | char *x = "abc"; |
| 805 | if (strcasecmp(x, "Abc") != 0) |
| 806 | (void)*(char*)0; // no-warning |
| 807 | } |
| 808 | |
| 809 | void strcasecmp_constant_and_var_1() { |
| 810 | char *x = "abc"; |
| 811 | if (strcasecmp("Abc", x) != 0) |
| 812 | (void)*(char*)0; // no-warning |
| 813 | } |
| 814 | |
| 815 | void strcasecmp_0() { |
| 816 | char *x = "abc"; |
| 817 | char *y = "Abc"; |
| 818 | if (strcasecmp(x, y) != 0) |
| 819 | (void)*(char*)0; // no-warning |
| 820 | } |
| 821 | |
| 822 | void strcasecmp_1() { |
| 823 | char *x = "Bcd"; |
| 824 | char *y = "abc"; |
| 825 | if (strcasecmp(x, y) != 1) |
| 826 | (void)*(char*)0; // no-warning |
| 827 | } |
| 828 | |
| 829 | void strcasecmp_2() { |
| 830 | char *x = "abc"; |
| 831 | char *y = "Bcd"; |
| 832 | if (strcasecmp(x, y) != -1) |
| 833 | (void)*(char*)0; // no-warning |
| 834 | } |
| 835 | |
| 836 | void strcasecmp_null_0() { |
| 837 | char *x = NULL; |
| 838 | char *y = "123"; |
| 839 | strcasecmp(x, y); // expected-warning{{Null pointer argument in call to byte string function}} |
| 840 | } |
| 841 | |
| 842 | void strcasecmp_null_1() { |
| 843 | char *x = "123"; |
| 844 | char *y = NULL; |
| 845 | strcasecmp(x, y); // expected-warning{{Null pointer argument in call to byte string function}} |
| 846 | } |
| 847 | |
| 848 | void strcasecmp_diff_length_0() { |
| 849 | char *x = "abcde"; |
| 850 | char *y = "aBd"; |
| 851 | if (strcasecmp(x, y) != -1) |
| 852 | (void)*(char*)0; // no-warning |
| 853 | } |
| 854 | |
| 855 | void strcasecmp_diff_length_1() { |
| 856 | char *x = "abc"; |
| 857 | char *y = "aBdef"; |
| 858 | if (strcasecmp(x, y) != -1) |
| 859 | (void)*(char*)0; // no-warning |
| 860 | } |
| 861 | |
| 862 | void strcasecmp_diff_length_2() { |
| 863 | char *x = "aBcDe"; |
| 864 | char *y = "abc"; |
| 865 | if (strcasecmp(x, y) != 1) |
| 866 | (void)*(char*)0; // no-warning |
| 867 | } |
| 868 | |
| 869 | void strcasecmp_diff_length_3() { |
| 870 | char *x = "aBc"; |
| 871 | char *y = "abcde"; |
| 872 | if (strcasecmp(x, y) != -1) |
| 873 | (void)*(char*)0; // no-warning |
| 874 | } |
Lenny Maiorani | 454fd2d | 2011-05-02 19:05:49 +0000 | [diff] [blame^] | 875 | |
| 876 | //===----------------------------------------------------------------------=== |
| 877 | // strncasecmp() |
| 878 | //===----------------------------------------------------------------------=== |
| 879 | |
| 880 | #define strncasecmp BUILTIN(strncasecmp) |
| 881 | int strncasecmp(const char *restrict s1, const char *restrict s2, size_t n); |
| 882 | |
| 883 | void strncasecmp_constant0() { |
| 884 | if (strncasecmp("abc", "Abc", 3) != 0) |
| 885 | (void)*(char*)0; // no-warning |
| 886 | } |
| 887 | |
| 888 | void strncasecmp_constant_and_var_0() { |
| 889 | char *x = "abc"; |
| 890 | if (strncasecmp(x, "Abc", 3) != 0) |
| 891 | (void)*(char*)0; // no-warning |
| 892 | } |
| 893 | |
| 894 | void strncasecmp_constant_and_var_1() { |
| 895 | char *x = "abc"; |
| 896 | if (strncasecmp("Abc", x, 3) != 0) |
| 897 | (void)*(char*)0; // no-warning |
| 898 | } |
| 899 | |
| 900 | void strncasecmp_0() { |
| 901 | char *x = "abc"; |
| 902 | char *y = "Abc"; |
| 903 | if (strncasecmp(x, y, 3) != 0) |
| 904 | (void)*(char*)0; // no-warning |
| 905 | } |
| 906 | |
| 907 | void strncasecmp_1() { |
| 908 | char *x = "Bcd"; |
| 909 | char *y = "abc"; |
| 910 | if (strncasecmp(x, y, 3) != 1) |
| 911 | (void)*(char*)0; // no-warning |
| 912 | } |
| 913 | |
| 914 | void strncasecmp_2() { |
| 915 | char *x = "abc"; |
| 916 | char *y = "Bcd"; |
| 917 | if (strncasecmp(x, y, 3) != -1) |
| 918 | (void)*(char*)0; // no-warning |
| 919 | } |
| 920 | |
| 921 | void strncasecmp_null_0() { |
| 922 | char *x = NULL; |
| 923 | char *y = "123"; |
| 924 | strncasecmp(x, y, 3); // expected-warning{{Null pointer argument in call to byte string function}} |
| 925 | } |
| 926 | |
| 927 | void strncasecmp_null_1() { |
| 928 | char *x = "123"; |
| 929 | char *y = NULL; |
| 930 | strncasecmp(x, y, 3); // expected-warning{{Null pointer argument in call to byte string function}} |
| 931 | } |
| 932 | |
| 933 | void strncasecmp_diff_length_0() { |
| 934 | char *x = "abcde"; |
| 935 | char *y = "aBd"; |
| 936 | if (strncasecmp(x, y, 5) != -1) |
| 937 | (void)*(char*)0; // no-warning |
| 938 | } |
| 939 | |
| 940 | void strncasecmp_diff_length_1() { |
| 941 | char *x = "abc"; |
| 942 | char *y = "aBdef"; |
| 943 | if (strncasecmp(x, y, 5) != -1) |
| 944 | (void)*(char*)0; // no-warning |
| 945 | } |
| 946 | |
| 947 | void strncasecmp_diff_length_2() { |
| 948 | char *x = "aBcDe"; |
| 949 | char *y = "abc"; |
| 950 | if (strncasecmp(x, y, 5) != 1) |
| 951 | (void)*(char*)0; // no-warning |
| 952 | } |
| 953 | |
| 954 | void strncasecmp_diff_length_3() { |
| 955 | char *x = "aBc"; |
| 956 | char *y = "abcde"; |
| 957 | if (strncasecmp(x, y, 5) != -1) |
| 958 | (void)*(char*)0; // no-warning |
| 959 | } |
| 960 | |
| 961 | void strncasecmp_diff_length_4() { |
| 962 | char *x = "abcde"; |
| 963 | char *y = "aBc"; |
| 964 | if (strncasecmp(x, y, 3) != 0) |
| 965 | (void)*(char*)0; // no-warning |
| 966 | } |
| 967 | |
| 968 | void strncasecmp_diff_length_5() { |
| 969 | char *x = "abcde"; |
| 970 | char *y = "aBd"; |
| 971 | if (strncasecmp(x, y, 3) != -1) |
| 972 | (void)*(char*)0; // no-warning |
| 973 | } |
| 974 | |
| 975 | void strncasecmp_diff_length_6() { |
| 976 | char *x = "aBDe"; |
| 977 | char *y = "abc"; |
| 978 | if (strncasecmp(x, y, 3) != 1) |
| 979 | (void)*(char*)0; // no-warning |
| 980 | } |