string.c revision 9e49d9fbdc861c25c2480233147dee07f5fa9660
1// RUN: %clang_cc1 -analyze -analyzer-checker=core,unix.experimental.CString,deadcode.experimental.UnreachableCode -analyzer-store=region -Wno-null-dereference -verify %s 2// RUN: %clang_cc1 -analyze -DUSE_BUILTINS -analyzer-checker=core,unix.experimental.CString,deadcode.experimental.UnreachableCode -analyzer-store=region -Wno-null-dereference -verify %s 3// RUN: %clang_cc1 -analyze -DVARIANT -analyzer-checker=core,unix.experimental.CString,deadcode.experimental.UnreachableCode -analyzer-store=region -Wno-null-dereference -verify %s 4// RUN: %clang_cc1 -analyze -DUSE_BUILTINS -DVARIANT -analyzer-checker=core,unix.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 string length function}} 59} 60 61size_t strlen_fn() { 62 return strlen((char*)&strlen_fn); // expected-warning{{Argument to string length 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 string length 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 string length function}} 191} 192 193size_t strnlen_fn() { 194 return strnlen((char*)&strlen_fn, 3); // expected-warning{{Argument to string length 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 string length function is the address of the label 'label', which is not a null-terminated string}} 200} 201 202void strnlen_zero() { 203 if (strnlen("abc", 0) != 0) 204 (void)*(char*)0; // no-warning 205 if (strnlen(NULL, 0) != 0) // no-warning 206 (void)*(char*)0; // no-warning 207} 208 209size_t strnlen_compound_literal() { 210 // This used to crash because we don't model the string lengths of 211 // compound literals. 212 return strnlen((char[]) { 'a', 'b', 0 }, 1); 213} 214 215size_t strnlen_unknown_limit(float f) { 216 // This used to crash because we don't model the integer values of floats. 217 return strnlen("abc", (int)f); 218} 219 220void strnlen_is_not_strlen(char *x) { 221 if (strnlen(x, 10) != strlen(x)) 222 (void)*(char*)0; // expected-warning{{null}} 223} 224 225void strnlen_at_limit(char *x) { 226 size_t len = strnlen(x, 10); 227 if (len > 10) 228 (void)*(char*)0; // expected-warning{{never executed}} 229 if (len == 10) 230 (void)*(char*)0; // expected-warning{{null}} 231} 232 233void strnlen_less_than_limit(char *x) { 234 size_t len = strnlen(x, 10); 235 if (len > 10) 236 (void)*(char*)0; // expected-warning{{never executed}} 237 if (len < 10) 238 (void)*(char*)0; // expected-warning{{null}} 239} 240 241void strnlen_at_actual(size_t limit) { 242 size_t len = strnlen("abc", limit); 243 if (len > 3) 244 (void)*(char*)0; // expected-warning{{never executed}} 245 if (len == 3) 246 (void)*(char*)0; // expected-warning{{null}} 247} 248 249void strnlen_less_than_actual(size_t limit) { 250 size_t len = strnlen("abc", limit); 251 if (len > 3) 252 (void)*(char*)0; // expected-warning{{never executed}} 253 if (len < 3) 254 (void)*(char*)0; // expected-warning{{null}} 255} 256 257//===----------------------------------------------------------------------=== 258// strcpy() 259//===----------------------------------------------------------------------=== 260 261#ifdef VARIANT 262 263#define __strcpy_chk BUILTIN(__strcpy_chk) 264char *__strcpy_chk(char *restrict s1, const char *restrict s2, size_t destlen); 265 266#define strcpy(a,b) __strcpy_chk(a,b,(size_t)-1) 267 268#else /* VARIANT */ 269 270#define strcpy BUILTIN(strcpy) 271char *strcpy(char *restrict s1, const char *restrict s2); 272 273#endif /* VARIANT */ 274 275 276void strcpy_null_dst(char *x) { 277 strcpy(NULL, x); // expected-warning{{Null pointer argument in call to string copy function}} 278} 279 280void strcpy_null_src(char *x) { 281 strcpy(x, NULL); // expected-warning{{Null pointer argument in call to string copy function}} 282} 283 284void strcpy_fn(char *x) { 285 strcpy(x, (char*)&strcpy_fn); // expected-warning{{Argument to string copy function is the address of the function 'strcpy_fn', which is not a null-terminated string}} 286} 287 288void strcpy_effects(char *x, char *y) { 289 char a = x[0]; 290 291 if (strcpy(x, y) != x) 292 (void)*(char*)0; // no-warning 293 294 if (strlen(x) != strlen(y)) 295 (void)*(char*)0; // no-warning 296 297 if (a != x[0]) 298 (void)*(char*)0; // expected-warning{{null}} 299} 300 301void strcpy_overflow(char *y) { 302 char x[4]; 303 if (strlen(y) == 4) 304 strcpy(x, y); // expected-warning{{String copy function overflows destination buffer}} 305} 306 307void strcpy_no_overflow(char *y) { 308 char x[4]; 309 if (strlen(y) == 3) 310 strcpy(x, y); // no-warning 311} 312 313//===----------------------------------------------------------------------=== 314// stpcpy() 315//===----------------------------------------------------------------------=== 316 317#ifdef VARIANT 318 319#define __stpcpy_chk BUILTIN(__stpcpy_chk) 320char *__stpcpy_chk(char *restrict s1, const char *restrict s2, size_t destlen); 321 322#define stpcpy(a,b) __stpcpy_chk(a,b,(size_t)-1) 323 324#else /* VARIANT */ 325 326#define stpcpy BUILTIN(stpcpy) 327char *stpcpy(char *restrict s1, const char *restrict s2); 328 329#endif /* VARIANT */ 330 331 332void stpcpy_effect(char *x, char *y) { 333 char a = x[0]; 334 335 if (stpcpy(x, y) != &x[strlen(y)]) 336 (void)*(char*)0; // no-warning 337 338 if (strlen(x) != strlen(y)) 339 (void)*(char*)0; // no-warning 340 341 if (a != x[0]) 342 (void)*(char*)0; // expected-warning{{null}} 343} 344 345void stpcpy_overflow(char *y) { 346 char x[4]; 347 if (strlen(y) == 4) 348 stpcpy(x, y); // expected-warning{{String copy function overflows destination buffer}} 349} 350 351void stpcpy_no_overflow(char *y) { 352 char x[4]; 353 if (strlen(y) == 3) 354 stpcpy(x, y); // no-warning 355} 356 357//===----------------------------------------------------------------------=== 358// strcat() 359//===----------------------------------------------------------------------=== 360 361#ifdef VARIANT 362 363#define __strcat_chk BUILTIN(__strcat_chk) 364char *__strcat_chk(char *restrict s1, const char *restrict s2, size_t destlen); 365 366#define strcat(a,b) __strcat_chk(a,b,(size_t)-1) 367 368#else /* VARIANT */ 369 370#define strcat BUILTIN(strcat) 371char *strcat(char *restrict s1, const char *restrict s2); 372 373#endif /* VARIANT */ 374 375 376void strcat_null_dst(char *x) { 377 strcat(NULL, x); // expected-warning{{Null pointer argument in call to string copy function}} 378} 379 380void strcat_null_src(char *x) { 381 strcat(x, NULL); // expected-warning{{Null pointer argument in call to string copy function}} 382} 383 384void strcat_fn(char *x) { 385 strcat(x, (char*)&strcat_fn); // expected-warning{{Argument to string copy function is the address of the function 'strcat_fn', which is not a null-terminated string}} 386} 387 388void strcat_effects(char *y) { 389 char x[8] = "123"; 390 size_t orig_len = strlen(x); 391 char a = x[0]; 392 393 if (strlen(y) != 4) 394 return; 395 396 if (strcat(x, y) != x) 397 (void)*(char*)0; // no-warning 398 399 if ((int)strlen(x) != (orig_len + strlen(y))) 400 (void)*(char*)0; // no-warning 401} 402 403void strcat_overflow_0(char *y) { 404 char x[4] = "12"; 405 if (strlen(y) == 4) 406 strcat(x, y); // expected-warning{{String copy function overflows destination buffer}} 407} 408 409void strcat_overflow_1(char *y) { 410 char x[4] = "12"; 411 if (strlen(y) == 3) 412 strcat(x, y); // expected-warning{{String copy function overflows destination buffer}} 413} 414 415void strcat_overflow_2(char *y) { 416 char x[4] = "12"; 417 if (strlen(y) == 2) 418 strcat(x, y); // expected-warning{{String copy function overflows destination buffer}} 419} 420 421void strcat_no_overflow(char *y) { 422 char x[5] = "12"; 423 if (strlen(y) == 2) 424 strcat(x, y); // no-warning 425} 426 427void strcat_symbolic_dst_length(char *dst) { 428 strcat(dst, "1234"); 429 if (strlen(dst) < 4) 430 (void)*(char*)0; // no-warning 431} 432 433void strcat_symbolic_src_length(char *src) { 434 char dst[8] = "1234"; 435 strcat(dst, src); 436 if (strlen(dst) < 4) 437 (void)*(char*)0; // no-warning 438} 439 440void strcat_unknown_src_length(char *src, int offset) { 441 char dst[8] = "1234"; 442 strcat(dst, &src[offset]); 443 if (strlen(dst) < 4) 444 (void)*(char*)0; // no-warning 445} 446 447// There is no strcat_unknown_dst_length because if we can't get a symbolic 448// length for the "before" strlen, we won't be able to set one for "after". 449 450void strcat_too_big(char *dst, char *src) { 451 if (strlen(dst) != (((size_t)0) - 2)) 452 return; 453 if (strlen(src) != 2) 454 return; 455 strcat(dst, src); // expected-warning{{This expression will create a string whose length is too big to be represented as a size_t}} 456} 457 458 459//===----------------------------------------------------------------------=== 460// strncat() 461//===----------------------------------------------------------------------=== 462 463#ifdef VARIANT 464 465#define __strncat_chk BUILTIN(__strncat_chk) 466char *__strncat_chk(char *restrict s1, const char *restrict s2, size_t n, size_t destlen); 467 468#define strncat(a,b,c) __strncat_chk(a,b,c, (size_t)-1) 469 470#else /* VARIANT */ 471 472#define strncat BUILTIN(strncat) 473char *strncat(char *restrict s1, const char *restrict s2, size_t n); 474 475#endif /* VARIANT */ 476 477 478void strncat_null_dst(char *x) { 479 strncat(NULL, x, 4); // expected-warning{{Null pointer argument in call to string copy function}} 480} 481 482void strncat_null_src(char *x) { 483 strncat(x, NULL, 4); // expected-warning{{Null pointer argument in call to string copy function}} 484} 485 486void strncat_fn(char *x) { 487 strncat(x, (char*)&strncat_fn, 4); // expected-warning{{Argument to string copy function is the address of the function 'strncat_fn', which is not a null-terminated string}} 488} 489 490void strncat_effects(char *y) { 491 char x[8] = "123"; 492 size_t orig_len = strlen(x); 493 char a = x[0]; 494 495 if (strlen(y) != 4) 496 return; 497 498 if (strncat(x, y, strlen(y)) != x) 499 (void)*(char*)0; // no-warning 500 501 if (strlen(x) != orig_len + strlen(y)) 502 (void)*(char*)0; // no-warning 503} 504 505void strncat_overflow_0(char *y) { 506 char x[4] = "12"; 507 if (strlen(y) == 4) 508 strncat(x, y, strlen(y)); // expected-warning{{String copy function overflows destination buffer}} 509} 510 511void strncat_overflow_1(char *y) { 512 char x[4] = "12"; 513 if (strlen(y) == 3) 514 strncat(x, y, strlen(y)); // expected-warning{{String copy function overflows destination buffer}} 515} 516 517void strncat_overflow_2(char *y) { 518 char x[4] = "12"; 519 if (strlen(y) == 2) 520 strncat(x, y, strlen(y)); // expected-warning{{String copy function overflows destination buffer}} 521} 522 523void strncat_overflow_3(char *y) { 524 char x[4] = "12"; 525 if (strlen(y) == 4) 526 strncat(x, y, 2); // expected-warning{{String copy function overflows destination buffer}} 527} 528void strncat_no_overflow_1(char *y) { 529 char x[5] = "12"; 530 if (strlen(y) == 2) 531 strncat(x, y, strlen(y)); // no-warning 532} 533 534void strncat_no_overflow_2(char *y) { 535 char x[4] = "12"; 536 if (strlen(y) == 4) 537 strncat(x, y, 1); // no-warning 538} 539 540//===----------------------------------------------------------------------=== 541// strcmp() 542//===----------------------------------------------------------------------=== 543 544#define strcmp BUILTIN(strcmp) 545int strcmp(const char * s1, const char * s2); 546 547void strcmp_constant0() { 548 if (strcmp("123", "123") != 0) 549 (void)*(char*)0; // no-warning 550} 551 552void strcmp_constant_and_var_0() { 553 char *x = "123"; 554 if (strcmp(x, "123") != 0) 555 (void)*(char*)0; // no-warning 556} 557 558void strcmp_constant_and_var_1() { 559 char *x = "123"; 560 if (strcmp("123", x) != 0) 561 (void)*(char*)0; // no-warning 562} 563 564void strcmp_0() { 565 char *x = "123"; 566 char *y = "123"; 567 if (strcmp(x, y) != 0) 568 (void)*(char*)0; // no-warning 569} 570 571void strcmp_1() { 572 char *x = "234"; 573 char *y = "123"; 574 if (strcmp(x, y) != 1) 575 (void)*(char*)0; // no-warning 576} 577 578void strcmp_2() { 579 char *x = "123"; 580 char *y = "234"; 581 if (strcmp(x, y) != -1) 582 (void)*(char*)0; // no-warning 583} 584 585void strcmp_null_0() { 586 char *x = NULL; 587 char *y = "123"; 588 strcmp(x, y); // expected-warning{{Null pointer argument in call to string comparison function}} 589} 590 591void strcmp_null_1() { 592 char *x = "123"; 593 char *y = NULL; 594 strcmp(x, y); // expected-warning{{Null pointer argument in call to string comparison function}} 595} 596 597void strcmp_diff_length_0() { 598 char *x = "12345"; 599 char *y = "234"; 600 if (strcmp(x, y) != -1) 601 (void)*(char*)0; // no-warning 602} 603 604void strcmp_diff_length_1() { 605 char *x = "123"; 606 char *y = "23456"; 607 if (strcmp(x, y) != -1) 608 (void)*(char*)0; // no-warning 609} 610 611void strcmp_diff_length_2() { 612 char *x = "12345"; 613 char *y = "123"; 614 if (strcmp(x, y) != 1) 615 (void)*(char*)0; // no-warning 616} 617 618void strcmp_diff_length_3() { 619 char *x = "123"; 620 char *y = "12345"; 621 if (strcmp(x, y) != -1) 622 (void)*(char*)0; // no-warning 623} 624 625void strcmp_embedded_null () { 626 if (strcmp("\0z", "\0y") != 0) 627 (void)*(char*)0; // no-warning 628} 629 630void strcmp_unknown_arg (char *unknown) { 631 if (strcmp(unknown, unknown) != 0) 632 (void)*(char*)0; // no-warning 633} 634 635//===----------------------------------------------------------------------=== 636// strncmp() 637//===----------------------------------------------------------------------=== 638 639#define strncmp BUILTIN(strncmp) 640int strncmp(const char *s1, const char *s2, size_t n); 641 642void strncmp_constant0() { 643 if (strncmp("123", "123", 3) != 0) 644 (void)*(char*)0; // no-warning 645} 646 647void strncmp_constant_and_var_0() { 648 char *x = "123"; 649 if (strncmp(x, "123", 3) != 0) 650 (void)*(char*)0; // no-warning 651} 652 653void strncmp_constant_and_var_1() { 654 char *x = "123"; 655 if (strncmp("123", x, 3) != 0) 656 (void)*(char*)0; // no-warning 657} 658 659void strncmp_0() { 660 char *x = "123"; 661 char *y = "123"; 662 if (strncmp(x, y, 3) != 0) 663 (void)*(char*)0; // no-warning 664} 665 666void strncmp_1() { 667 char *x = "234"; 668 char *y = "123"; 669 if (strncmp(x, y, 3) != 1) 670 (void)*(char*)0; // no-warning 671} 672 673void strncmp_2() { 674 char *x = "123"; 675 char *y = "234"; 676 if (strncmp(x, y, 3) != -1) 677 (void)*(char*)0; // no-warning 678} 679 680void strncmp_null_0() { 681 char *x = NULL; 682 char *y = "123"; 683 strncmp(x, y, 3); // expected-warning{{Null pointer argument in call to string comparison function}} 684} 685 686void strncmp_null_1() { 687 char *x = "123"; 688 char *y = NULL; 689 strncmp(x, y, 3); // expected-warning{{Null pointer argument in call to string comparison function}} 690} 691 692void strncmp_diff_length_0() { 693 char *x = "12345"; 694 char *y = "234"; 695 if (strncmp(x, y, 5) != -1) 696 (void)*(char*)0; // no-warning 697} 698 699void strncmp_diff_length_1() { 700 char *x = "123"; 701 char *y = "23456"; 702 if (strncmp(x, y, 5) != -1) 703 (void)*(char*)0; // no-warning 704} 705 706void strncmp_diff_length_2() { 707 char *x = "12345"; 708 char *y = "123"; 709 if (strncmp(x, y, 5) != 1) 710 (void)*(char*)0; // no-warning 711} 712 713void strncmp_diff_length_3() { 714 char *x = "123"; 715 char *y = "12345"; 716 if (strncmp(x, y, 5) != -1) 717 (void)*(char*)0; // no-warning 718} 719 720void strncmp_diff_length_4() { 721 char *x = "123"; 722 char *y = "12345"; 723 if (strncmp(x, y, 3) != 0) 724 (void)*(char*)0; // no-warning 725} 726 727void strncmp_diff_length_5() { 728 char *x = "012"; 729 char *y = "12345"; 730 if (strncmp(x, y, 3) != -1) 731 (void)*(char*)0; // no-warning 732} 733 734void strncmp_diff_length_6() { 735 char *x = "234"; 736 char *y = "12345"; 737 if (strncmp(x, y, 3) != 1) 738 (void)*(char*)0; // no-warning 739} 740 741void strncmp_embedded_null () { 742 if (strncmp("ab\0zz", "ab\0yy", 4) != 0) 743 (void)*(char*)0; // no-warning 744} 745 746//===----------------------------------------------------------------------=== 747// strcasecmp() 748//===----------------------------------------------------------------------=== 749 750#define strcasecmp BUILTIN(strcasecmp) 751int strcasecmp(const char *s1, const char *s2); 752 753void strcasecmp_constant0() { 754 if (strcasecmp("abc", "Abc") != 0) 755 (void)*(char*)0; // no-warning 756} 757 758void strcasecmp_constant_and_var_0() { 759 char *x = "abc"; 760 if (strcasecmp(x, "Abc") != 0) 761 (void)*(char*)0; // no-warning 762} 763 764void strcasecmp_constant_and_var_1() { 765 char *x = "abc"; 766 if (strcasecmp("Abc", x) != 0) 767 (void)*(char*)0; // no-warning 768} 769 770void strcasecmp_0() { 771 char *x = "abc"; 772 char *y = "Abc"; 773 if (strcasecmp(x, y) != 0) 774 (void)*(char*)0; // no-warning 775} 776 777void strcasecmp_1() { 778 char *x = "Bcd"; 779 char *y = "abc"; 780 if (strcasecmp(x, y) != 1) 781 (void)*(char*)0; // no-warning 782} 783 784void strcasecmp_2() { 785 char *x = "abc"; 786 char *y = "Bcd"; 787 if (strcasecmp(x, y) != -1) 788 (void)*(char*)0; // no-warning 789} 790 791void strcasecmp_null_0() { 792 char *x = NULL; 793 char *y = "123"; 794 strcasecmp(x, y); // expected-warning{{Null pointer argument in call to string comparison function}} 795} 796 797void strcasecmp_null_1() { 798 char *x = "123"; 799 char *y = NULL; 800 strcasecmp(x, y); // expected-warning{{Null pointer argument in call to string comparison function}} 801} 802 803void strcasecmp_diff_length_0() { 804 char *x = "abcde"; 805 char *y = "aBd"; 806 if (strcasecmp(x, y) != -1) 807 (void)*(char*)0; // no-warning 808} 809 810void strcasecmp_diff_length_1() { 811 char *x = "abc"; 812 char *y = "aBdef"; 813 if (strcasecmp(x, y) != -1) 814 (void)*(char*)0; // no-warning 815} 816 817void strcasecmp_diff_length_2() { 818 char *x = "aBcDe"; 819 char *y = "abc"; 820 if (strcasecmp(x, y) != 1) 821 (void)*(char*)0; // no-warning 822} 823 824void strcasecmp_diff_length_3() { 825 char *x = "aBc"; 826 char *y = "abcde"; 827 if (strcasecmp(x, y) != -1) 828 (void)*(char*)0; // no-warning 829} 830 831void strcasecmp_embedded_null () { 832 if (strcasecmp("ab\0zz", "ab\0yy") != 0) 833 (void)*(char*)0; // no-warning 834} 835 836//===----------------------------------------------------------------------=== 837// strncasecmp() 838//===----------------------------------------------------------------------=== 839 840#define strncasecmp BUILTIN(strncasecmp) 841int strncasecmp(const char *s1, const char *s2, size_t n); 842 843void strncasecmp_constant0() { 844 if (strncasecmp("abc", "Abc", 3) != 0) 845 (void)*(char*)0; // no-warning 846} 847 848void strncasecmp_constant_and_var_0() { 849 char *x = "abc"; 850 if (strncasecmp(x, "Abc", 3) != 0) 851 (void)*(char*)0; // no-warning 852} 853 854void strncasecmp_constant_and_var_1() { 855 char *x = "abc"; 856 if (strncasecmp("Abc", x, 3) != 0) 857 (void)*(char*)0; // no-warning 858} 859 860void strncasecmp_0() { 861 char *x = "abc"; 862 char *y = "Abc"; 863 if (strncasecmp(x, y, 3) != 0) 864 (void)*(char*)0; // no-warning 865} 866 867void strncasecmp_1() { 868 char *x = "Bcd"; 869 char *y = "abc"; 870 if (strncasecmp(x, y, 3) != 1) 871 (void)*(char*)0; // no-warning 872} 873 874void strncasecmp_2() { 875 char *x = "abc"; 876 char *y = "Bcd"; 877 if (strncasecmp(x, y, 3) != -1) 878 (void)*(char*)0; // no-warning 879} 880 881void strncasecmp_null_0() { 882 char *x = NULL; 883 char *y = "123"; 884 strncasecmp(x, y, 3); // expected-warning{{Null pointer argument in call to string comparison function}} 885} 886 887void strncasecmp_null_1() { 888 char *x = "123"; 889 char *y = NULL; 890 strncasecmp(x, y, 3); // expected-warning{{Null pointer argument in call to string comparison function}} 891} 892 893void strncasecmp_diff_length_0() { 894 char *x = "abcde"; 895 char *y = "aBd"; 896 if (strncasecmp(x, y, 5) != -1) 897 (void)*(char*)0; // no-warning 898} 899 900void strncasecmp_diff_length_1() { 901 char *x = "abc"; 902 char *y = "aBdef"; 903 if (strncasecmp(x, y, 5) != -1) 904 (void)*(char*)0; // no-warning 905} 906 907void strncasecmp_diff_length_2() { 908 char *x = "aBcDe"; 909 char *y = "abc"; 910 if (strncasecmp(x, y, 5) != 1) 911 (void)*(char*)0; // no-warning 912} 913 914void strncasecmp_diff_length_3() { 915 char *x = "aBc"; 916 char *y = "abcde"; 917 if (strncasecmp(x, y, 5) != -1) 918 (void)*(char*)0; // no-warning 919} 920 921void strncasecmp_diff_length_4() { 922 char *x = "abcde"; 923 char *y = "aBc"; 924 if (strncasecmp(x, y, 3) != 0) 925 (void)*(char*)0; // no-warning 926} 927 928void strncasecmp_diff_length_5() { 929 char *x = "abcde"; 930 char *y = "aBd"; 931 if (strncasecmp(x, y, 3) != -1) 932 (void)*(char*)0; // no-warning 933} 934 935void strncasecmp_diff_length_6() { 936 char *x = "aBDe"; 937 char *y = "abc"; 938 if (strncasecmp(x, y, 3) != 1) 939 (void)*(char*)0; // no-warning 940} 941 942void strncasecmp_embedded_null () { 943 if (strncasecmp("ab\0zz", "ab\0yy", 4) != 0) 944 (void)*(char*)0; // no-warning 945} 946