string.c revision 094ea0afcfa79eb0c4a2c35a059491be3ab954a9
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 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 18// Functions that have variants and are also available as builtins should be 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 27#define NULL 0 28typedef typeof(sizeof(int)) size_t; 29 30//===----------------------------------------------------------------------=== 31// strlen() 32//===----------------------------------------------------------------------=== 33 34#define strlen BUILTIN(strlen) 35size_t strlen(const char *s); 36 37void strlen_constant0() { 38 if (strlen("123") != 3) 39 (void)*(char*)0; // no-warning 40} 41 42void strlen_constant1() { 43 const char *a = "123"; 44 if (strlen(a) != 3) 45 (void)*(char*)0; // no-warning 46} 47 48void strlen_constant2(char x) { 49 char a[] = "123"; 50 if (strlen(a) != 3) 51 (void)*(char*)0; // no-warning 52 a[0] = x; 53 if (strlen(a) != 3) 54 (void)*(char*)0; // expected-warning{{null}} 55} 56 57size_t strlen_null() { 58 return strlen(0); // expected-warning{{Null pointer argument in call to byte string function}} 59} 60 61size_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 65size_t strlen_nonloc() { 66label: 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} 69 70void strlen_subregion() { 71 struct two_strings { char a[2], b[2]; }; 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 90extern void use_string(char *); 91void 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 104extern char global_str[]; 105void 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 119void 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 135void strlen_liveness(const char *x) { 136 if (strlen(x) < 5) 137 return; 138 if (strlen(x) < 5) 139 (void)*(char*)0; // no-warning 140} 141 142//===----------------------------------------------------------------------=== 143// strnlen() 144//===----------------------------------------------------------------------=== 145 146#define strnlen BUILTIN(strnlen) 147size_t strnlen(const char *s, size_t maxlen); 148 149void strnlen_constant0() { 150 if (strnlen("123", 10) != 3) 151 (void)*(char*)0; // no-warning 152} 153 154void strnlen_constant1() { 155 const char *a = "123"; 156 if (strnlen(a, 10) != 3) 157 (void)*(char*)0; // no-warning 158} 159 160void 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 169void strnlen_constant4() { 170 if (strnlen("123456", 3) != 3) 171 (void)*(char*)0; // no-warning 172} 173 174void strnlen_constant5() { 175 const char *a = "123456"; 176 if (strnlen(a, 3) != 3) 177 (void)*(char*)0; // no-warning 178} 179 180void 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 189size_t strnlen_null() { 190 return strnlen(0, 3); // expected-warning{{Null pointer argument in call to byte string function}} 191} 192 193size_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 197size_t strnlen_nonloc() { 198label: 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 202void strnlen_subregion() { 203 struct two_stringsn { char a[2], b[2]; }; 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 222extern void use_stringn(char *); 223void 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 236extern char global_strn[]; 237void 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 251void 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 267void 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//===----------------------------------------------------------------------=== 275// strcpy() 276//===----------------------------------------------------------------------=== 277 278#ifdef VARIANT 279 280#define __strcpy_chk BUILTIN(__strcpy_chk) 281char *__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) 288char *strcpy(char *restrict s1, const char *restrict s2); 289 290#endif /* VARIANT */ 291 292 293void strcpy_null_dst(char *x) { 294 strcpy(NULL, x); // expected-warning{{Null pointer argument in call to byte string function}} 295} 296 297void strcpy_null_src(char *x) { 298 strcpy(x, NULL); // expected-warning{{Null pointer argument in call to byte string function}} 299} 300 301void 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 305void 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 318void 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 324void strcpy_no_overflow(char *y) { 325 char x[4]; 326 if (strlen(y) == 3) 327 strcpy(x, y); // no-warning 328} 329 330//===----------------------------------------------------------------------=== 331// stpcpy() 332//===----------------------------------------------------------------------=== 333 334#ifdef VARIANT 335 336#define __stpcpy_chk BUILTIN(__stpcpy_chk) 337char *__stpcpy_chk(char *restrict s1, const char *restrict s2, size_t destlen); 338 339#define stpcpy(a,b) __stpcpy_chk(a,b,(size_t)-1) 340 341#else /* VARIANT */ 342 343#define stpcpy BUILTIN(stpcpy) 344char *stpcpy(char *restrict s1, const char *restrict s2); 345 346#endif /* VARIANT */ 347 348 349void stpcpy_effect(char *x, char *y) { 350 char a = x[0]; 351 352 if (stpcpy(x, y) != &x[strlen(y)]) 353 (void)*(char*)0; // no-warning 354 355 if (strlen(x) != strlen(y)) 356 (void)*(char*)0; // no-warning 357 358 if (a != x[0]) 359 (void)*(char*)0; // expected-warning{{null}} 360} 361 362void stpcpy_overflow(char *y) { 363 char x[4]; 364 if (strlen(y) == 4) 365 stpcpy(x, y); // expected-warning{{Byte string function overflows destination buffer}} 366} 367 368void stpcpy_no_overflow(char *y) { 369 char x[4]; 370 if (strlen(y) == 3) 371 stpcpy(x, y); // no-warning 372} 373 374//===----------------------------------------------------------------------=== 375// strcat() 376//===----------------------------------------------------------------------=== 377 378#ifdef VARIANT 379 380#define __strcat_chk BUILTIN(__strcat_chk) 381char *__strcat_chk(char *restrict s1, const char *restrict s2, size_t destlen); 382 383#define strcat(a,b) __strcat_chk(a,b,(size_t)-1) 384 385#else /* VARIANT */ 386 387#define strcat BUILTIN(strcat) 388char *strcat(char *restrict s1, const char *restrict s2); 389 390#endif /* VARIANT */ 391 392 393void strcat_null_dst(char *x) { 394 strcat(NULL, x); // expected-warning{{Null pointer argument in call to byte string function}} 395} 396 397void strcat_null_src(char *x) { 398 strcat(x, NULL); // expected-warning{{Null pointer argument in call to byte string function}} 399} 400 401void strcat_fn(char *x) { 402 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}} 403} 404 405void strcat_effects(char *y) { 406 char x[8] = "123"; 407 size_t orig_len = strlen(x); 408 char a = x[0]; 409 410 if (strlen(y) != 4) 411 return; 412 413 if (strcat(x, y) != x) 414 (void)*(char*)0; // no-warning 415 416 if ((int)strlen(x) != (orig_len + strlen(y))) 417 (void)*(char*)0; // no-warning 418 419 if (a != x[0]) 420 (void)*(char*)0; // expected-warning{{null}} 421} 422 423void strcat_overflow_0(char *y) { 424 char x[4] = "12"; 425 if (strlen(y) == 4) 426 strcat(x, y); // expected-warning{{Byte string function overflows destination buffer}} 427} 428 429void strcat_overflow_1(char *y) { 430 char x[4] = "12"; 431 if (strlen(y) == 3) 432 strcat(x, y); // expected-warning{{Byte string function overflows destination buffer}} 433} 434 435void strcat_overflow_2(char *y) { 436 char x[4] = "12"; 437 if (strlen(y) == 2) 438 strcat(x, y); // expected-warning{{Byte string function overflows destination buffer}} 439} 440 441void strcat_no_overflow(char *y) { 442 char x[5] = "12"; 443 if (strlen(y) == 2) 444 strcat(x, y); // no-warning 445} 446 447 448//===----------------------------------------------------------------------=== 449// strncat() 450//===----------------------------------------------------------------------=== 451 452#ifdef VARIANT 453 454#define __strncat_chk BUILTIN(__strncat_chk) 455char *__strncat_chk(char *restrict s1, const char *restrict s2, size_t n, size_t destlen); 456 457#define strncat(a,b,c) __strncat_chk(a,b,c, (size_t)-1) 458 459#else /* VARIANT */ 460 461#define strncat BUILTIN(strncat) 462char *strncat(char *restrict s1, const char *restrict s2, size_t n); 463 464#endif /* VARIANT */ 465 466 467void strncat_null_dst(char *x) { 468 strncat(NULL, x, 4); // expected-warning{{Null pointer argument in call to byte string function}} 469} 470 471void strncat_null_src(char *x) { 472 strncat(x, NULL, 4); // expected-warning{{Null pointer argument in call to byte string function}} 473} 474 475void strncat_fn(char *x) { 476 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}} 477} 478 479void strncat_effects(char *y) { 480 char x[8] = "123"; 481 size_t orig_len = strlen(x); 482 char a = x[0]; 483 484 if (strlen(y) != 4) 485 return; 486 487 if (strncat(x, y, strlen(y)) != x) 488 (void)*(char*)0; // no-warning 489 490 if (strlen(x) != orig_len + strlen(y)) 491 (void)*(char*)0; // no-warning 492 493 if (a != x[0]) 494 (void)*(char*)0; // expected-warning{{null}} 495} 496 497void strncat_overflow_0(char *y) { 498 char x[4] = "12"; 499 if (strlen(y) == 4) 500 strncat(x, y, strlen(y)); // expected-warning{{Byte string function overflows destination buffer}} 501} 502 503void strncat_overflow_1(char *y) { 504 char x[4] = "12"; 505 if (strlen(y) == 3) 506 strncat(x, y, strlen(y)); // expected-warning{{Byte string function overflows destination buffer}} 507} 508 509void strncat_overflow_2(char *y) { 510 char x[4] = "12"; 511 if (strlen(y) == 2) 512 strncat(x, y, strlen(y)); // expected-warning{{Byte string function overflows destination buffer}} 513} 514 515void strncat_overflow_3(char *y) { 516 char x[4] = "12"; 517 if (strlen(y) == 4) 518 strncat(x, y, 2); // expected-warning{{Byte string function overflows destination buffer}} 519} 520void strncat_no_overflow_1(char *y) { 521 char x[5] = "12"; 522 if (strlen(y) == 2) 523 strncat(x, y, strlen(y)); // no-warning 524} 525 526void strncat_no_overflow_2(char *y) { 527 char x[4] = "12"; 528 if (strlen(y) == 4) 529 strncat(x, y, 1); // no-warning 530} 531 532//===----------------------------------------------------------------------=== 533// strcmp() 534//===----------------------------------------------------------------------=== 535 536#define strcmp BUILTIN(strcmp) 537int strcmp(const char *restrict s1, const char *restrict s2); 538 539void strcmp_constant0() { 540 if (strcmp("123", "123") != 0) 541 (void)*(char*)0; // no-warning 542} 543 544void strcmp_constant_and_var_0() { 545 char *x = "123"; 546 if (strcmp(x, "123") != 0) 547 (void)*(char*)0; // no-warning 548} 549 550void strcmp_constant_and_var_1() { 551 char *x = "123"; 552 if (strcmp("123", x) != 0) 553 (void)*(char*)0; // no-warning 554} 555 556void strcmp_0() { 557 char *x = "123"; 558 char *y = "123"; 559 if (strcmp(x, y) != 0) 560 (void)*(char*)0; // no-warning 561} 562 563void strcmp_1() { 564 char *x = "234"; 565 char *y = "123"; 566 if (strcmp(x, y) != 1) 567 (void)*(char*)0; // no-warning 568} 569 570void strcmp_2() { 571 char *x = "123"; 572 char *y = "234"; 573 if (strcmp(x, y) != -1) 574 (void)*(char*)0; // no-warning 575} 576 577void strcmp_null_0() { 578 char *x = NULL; 579 char *y = "123"; 580 strcmp(x, y); // expected-warning{{Null pointer argument in call to byte string function}} 581} 582 583void strcmp_null_1() { 584 char *x = "123"; 585 char *y = NULL; 586 strcmp(x, y); // expected-warning{{Null pointer argument in call to byte string function}} 587} 588 589void strcmp_diff_length_0() { 590 char *x = "12345"; 591 char *y = "234"; 592 if (strcmp(x, y) != -1) 593 (void)*(char*)0; // no-warning 594} 595 596void strcmp_diff_length_1() { 597 char *x = "123"; 598 char *y = "23456"; 599 if (strcmp(x, y) != -1) 600 (void)*(char*)0; // no-warning 601} 602 603void strcmp_diff_length_2() { 604 char *x = "12345"; 605 char *y = "123"; 606 if (strcmp(x, y) != 1) 607 (void)*(char*)0; // no-warning 608} 609 610void strcmp_diff_length_3() { 611 char *x = "123"; 612 char *y = "12345"; 613 if (strcmp(x, y) != -1) 614 (void)*(char*)0; // no-warning 615} 616 617//===----------------------------------------------------------------------=== 618// strncmp() 619//===----------------------------------------------------------------------=== 620 621#define strncmp BUILTIN(strncmp) 622int strncmp(const char *restrict s1, const char *restrict s2, size_t n); 623 624void strncmp_constant0() { 625 if (strncmp("123", "123", 3) != 0) 626 (void)*(char*)0; // no-warning 627} 628 629void strncmp_constant_and_var_0() { 630 char *x = "123"; 631 if (strncmp(x, "123", 3) != 0) 632 (void)*(char*)0; // no-warning 633} 634 635void strncmp_constant_and_var_1() { 636 char *x = "123"; 637 if (strncmp("123", x, 3) != 0) 638 (void)*(char*)0; // no-warning 639} 640 641void strncmp_0() { 642 char *x = "123"; 643 char *y = "123"; 644 if (strncmp(x, y, 3) != 0) 645 (void)*(char*)0; // no-warning 646} 647 648void strncmp_1() { 649 char *x = "234"; 650 char *y = "123"; 651 if (strncmp(x, y, 3) != 1) 652 (void)*(char*)0; // no-warning 653} 654 655void strncmp_2() { 656 char *x = "123"; 657 char *y = "234"; 658 if (strncmp(x, y, 3) != -1) 659 (void)*(char*)0; // no-warning 660} 661 662void strncmp_null_0() { 663 char *x = NULL; 664 char *y = "123"; 665 strncmp(x, y, 3); // expected-warning{{Null pointer argument in call to byte string function}} 666} 667 668void strncmp_null_1() { 669 char *x = "123"; 670 char *y = NULL; 671 strncmp(x, y, 3); // expected-warning{{Null pointer argument in call to byte string function}} 672} 673 674void strncmp_diff_length_0() { 675 char *x = "12345"; 676 char *y = "234"; 677 if (strncmp(x, y, 5) != -1) 678 (void)*(char*)0; // no-warning 679} 680 681void strncmp_diff_length_1() { 682 char *x = "123"; 683 char *y = "23456"; 684 if (strncmp(x, y, 5) != -1) 685 (void)*(char*)0; // no-warning 686} 687 688void strncmp_diff_length_2() { 689 char *x = "12345"; 690 char *y = "123"; 691 if (strncmp(x, y, 5) != 1) 692 (void)*(char*)0; // no-warning 693} 694 695void strncmp_diff_length_3() { 696 char *x = "123"; 697 char *y = "12345"; 698 if (strncmp(x, y, 5) != -1) 699 (void)*(char*)0; // no-warning 700} 701 702void strncmp_diff_length_4() { 703 char *x = "123"; 704 char *y = "12345"; 705 if (strncmp(x, y, 3) != 0) 706 (void)*(char*)0; // no-warning 707} 708 709void strncmp_diff_length_5() { 710 char *x = "012"; 711 char *y = "12345"; 712 if (strncmp(x, y, 3) != -1) 713 (void)*(char*)0; // no-warning 714} 715 716void strncmp_diff_length_6() { 717 char *x = "234"; 718 char *y = "12345"; 719 if (strncmp(x, y, 3) != 1) 720 (void)*(char*)0; // no-warning 721} 722 723//===----------------------------------------------------------------------=== 724// strcasecmp() 725//===----------------------------------------------------------------------=== 726 727#define strcasecmp BUILTIN(strcasecmp) 728int strcasecmp(const char *restrict s1, const char *restrict s2); 729 730void strcasecmp_constant0() { 731 if (strcasecmp("abc", "Abc") != 0) 732 (void)*(char*)0; // no-warning 733} 734 735void strcasecmp_constant_and_var_0() { 736 char *x = "abc"; 737 if (strcasecmp(x, "Abc") != 0) 738 (void)*(char*)0; // no-warning 739} 740 741void strcasecmp_constant_and_var_1() { 742 char *x = "abc"; 743 if (strcasecmp("Abc", x) != 0) 744 (void)*(char*)0; // no-warning 745} 746 747void strcasecmp_0() { 748 char *x = "abc"; 749 char *y = "Abc"; 750 if (strcasecmp(x, y) != 0) 751 (void)*(char*)0; // no-warning 752} 753 754void strcasecmp_1() { 755 char *x = "Bcd"; 756 char *y = "abc"; 757 if (strcasecmp(x, y) != 1) 758 (void)*(char*)0; // no-warning 759} 760 761void strcasecmp_2() { 762 char *x = "abc"; 763 char *y = "Bcd"; 764 if (strcasecmp(x, y) != -1) 765 (void)*(char*)0; // no-warning 766} 767 768void strcasecmp_null_0() { 769 char *x = NULL; 770 char *y = "123"; 771 strcasecmp(x, y); // expected-warning{{Null pointer argument in call to byte string function}} 772} 773 774void strcasecmp_null_1() { 775 char *x = "123"; 776 char *y = NULL; 777 strcasecmp(x, y); // expected-warning{{Null pointer argument in call to byte string function}} 778} 779 780void strcasecmp_diff_length_0() { 781 char *x = "abcde"; 782 char *y = "aBd"; 783 if (strcasecmp(x, y) != -1) 784 (void)*(char*)0; // no-warning 785} 786 787void strcasecmp_diff_length_1() { 788 char *x = "abc"; 789 char *y = "aBdef"; 790 if (strcasecmp(x, y) != -1) 791 (void)*(char*)0; // no-warning 792} 793 794void strcasecmp_diff_length_2() { 795 char *x = "aBcDe"; 796 char *y = "abc"; 797 if (strcasecmp(x, y) != 1) 798 (void)*(char*)0; // no-warning 799} 800 801void strcasecmp_diff_length_3() { 802 char *x = "aBc"; 803 char *y = "abcde"; 804 if (strcasecmp(x, y) != -1) 805 (void)*(char*)0; // no-warning 806} 807 808//===----------------------------------------------------------------------=== 809// strncasecmp() 810//===----------------------------------------------------------------------=== 811 812#define strncasecmp BUILTIN(strncasecmp) 813int strncasecmp(const char *restrict s1, const char *restrict s2, size_t n); 814 815void strncasecmp_constant0() { 816 if (strncasecmp("abc", "Abc", 3) != 0) 817 (void)*(char*)0; // no-warning 818} 819 820void strncasecmp_constant_and_var_0() { 821 char *x = "abc"; 822 if (strncasecmp(x, "Abc", 3) != 0) 823 (void)*(char*)0; // no-warning 824} 825 826void strncasecmp_constant_and_var_1() { 827 char *x = "abc"; 828 if (strncasecmp("Abc", x, 3) != 0) 829 (void)*(char*)0; // no-warning 830} 831 832void strncasecmp_0() { 833 char *x = "abc"; 834 char *y = "Abc"; 835 if (strncasecmp(x, y, 3) != 0) 836 (void)*(char*)0; // no-warning 837} 838 839void strncasecmp_1() { 840 char *x = "Bcd"; 841 char *y = "abc"; 842 if (strncasecmp(x, y, 3) != 1) 843 (void)*(char*)0; // no-warning 844} 845 846void strncasecmp_2() { 847 char *x = "abc"; 848 char *y = "Bcd"; 849 if (strncasecmp(x, y, 3) != -1) 850 (void)*(char*)0; // no-warning 851} 852 853void strncasecmp_null_0() { 854 char *x = NULL; 855 char *y = "123"; 856 strncasecmp(x, y, 3); // expected-warning{{Null pointer argument in call to byte string function}} 857} 858 859void strncasecmp_null_1() { 860 char *x = "123"; 861 char *y = NULL; 862 strncasecmp(x, y, 3); // expected-warning{{Null pointer argument in call to byte string function}} 863} 864 865void strncasecmp_diff_length_0() { 866 char *x = "abcde"; 867 char *y = "aBd"; 868 if (strncasecmp(x, y, 5) != -1) 869 (void)*(char*)0; // no-warning 870} 871 872void strncasecmp_diff_length_1() { 873 char *x = "abc"; 874 char *y = "aBdef"; 875 if (strncasecmp(x, y, 5) != -1) 876 (void)*(char*)0; // no-warning 877} 878 879void strncasecmp_diff_length_2() { 880 char *x = "aBcDe"; 881 char *y = "abc"; 882 if (strncasecmp(x, y, 5) != 1) 883 (void)*(char*)0; // no-warning 884} 885 886void strncasecmp_diff_length_3() { 887 char *x = "aBc"; 888 char *y = "abcde"; 889 if (strncasecmp(x, y, 5) != -1) 890 (void)*(char*)0; // no-warning 891} 892 893void strncasecmp_diff_length_4() { 894 char *x = "abcde"; 895 char *y = "aBc"; 896 if (strncasecmp(x, y, 3) != 0) 897 (void)*(char*)0; // no-warning 898} 899 900void strncasecmp_diff_length_5() { 901 char *x = "abcde"; 902 char *y = "aBd"; 903 if (strncasecmp(x, y, 3) != -1) 904 (void)*(char*)0; // no-warning 905} 906 907void strncasecmp_diff_length_6() { 908 char *x = "aBDe"; 909 char *y = "abc"; 910 if (strncasecmp(x, y, 3) != 1) 911 (void)*(char*)0; // no-warning 912} 913