string.c revision 793bff3fb7ca2a31e81aa7f4f3f21f921459010b
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_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 byte string function}} 278} 279 280void strcpy_null_src(char *x) { 281 strcpy(x, NULL); // expected-warning{{Null pointer argument in call to byte string function}} 282} 283 284void strcpy_fn(char *x) { 285 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}} 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{{Byte string 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{{Byte string 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 byte string function}} 378} 379 380void strcat_null_src(char *x) { 381 strcat(x, NULL); // expected-warning{{Null pointer argument in call to byte string function}} 382} 383 384void strcat_fn(char *x) { 385 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}} 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 if (a != x[0]) 403 (void)*(char*)0; // expected-warning{{null}} 404} 405 406void strcat_overflow_0(char *y) { 407 char x[4] = "12"; 408 if (strlen(y) == 4) 409 strcat(x, y); // expected-warning{{Byte string function overflows destination buffer}} 410} 411 412void strcat_overflow_1(char *y) { 413 char x[4] = "12"; 414 if (strlen(y) == 3) 415 strcat(x, y); // expected-warning{{Byte string function overflows destination buffer}} 416} 417 418void strcat_overflow_2(char *y) { 419 char x[4] = "12"; 420 if (strlen(y) == 2) 421 strcat(x, y); // expected-warning{{Byte string function overflows destination buffer}} 422} 423 424void strcat_no_overflow(char *y) { 425 char x[5] = "12"; 426 if (strlen(y) == 2) 427 strcat(x, y); // no-warning 428} 429 430 431//===----------------------------------------------------------------------=== 432// strncat() 433//===----------------------------------------------------------------------=== 434 435#ifdef VARIANT 436 437#define __strncat_chk BUILTIN(__strncat_chk) 438char *__strncat_chk(char *restrict s1, const char *restrict s2, size_t n, size_t destlen); 439 440#define strncat(a,b,c) __strncat_chk(a,b,c, (size_t)-1) 441 442#else /* VARIANT */ 443 444#define strncat BUILTIN(strncat) 445char *strncat(char *restrict s1, const char *restrict s2, size_t n); 446 447#endif /* VARIANT */ 448 449 450void strncat_null_dst(char *x) { 451 strncat(NULL, x, 4); // expected-warning{{Null pointer argument in call to byte string function}} 452} 453 454void strncat_null_src(char *x) { 455 strncat(x, NULL, 4); // expected-warning{{Null pointer argument in call to byte string function}} 456} 457 458void strncat_fn(char *x) { 459 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}} 460} 461 462void strncat_effects(char *y) { 463 char x[8] = "123"; 464 size_t orig_len = strlen(x); 465 char a = x[0]; 466 467 if (strlen(y) != 4) 468 return; 469 470 if (strncat(x, y, strlen(y)) != x) 471 (void)*(char*)0; // no-warning 472 473 if (strlen(x) != orig_len + strlen(y)) 474 (void)*(char*)0; // no-warning 475 476 if (a != x[0]) 477 (void)*(char*)0; // expected-warning{{null}} 478} 479 480void strncat_overflow_0(char *y) { 481 char x[4] = "12"; 482 if (strlen(y) == 4) 483 strncat(x, y, strlen(y)); // expected-warning{{Byte string function overflows destination buffer}} 484} 485 486void strncat_overflow_1(char *y) { 487 char x[4] = "12"; 488 if (strlen(y) == 3) 489 strncat(x, y, strlen(y)); // expected-warning{{Byte string function overflows destination buffer}} 490} 491 492void strncat_overflow_2(char *y) { 493 char x[4] = "12"; 494 if (strlen(y) == 2) 495 strncat(x, y, strlen(y)); // expected-warning{{Byte string function overflows destination buffer}} 496} 497 498void strncat_overflow_3(char *y) { 499 char x[4] = "12"; 500 if (strlen(y) == 4) 501 strncat(x, y, 2); // expected-warning{{Byte string function overflows destination buffer}} 502} 503void strncat_no_overflow_1(char *y) { 504 char x[5] = "12"; 505 if (strlen(y) == 2) 506 strncat(x, y, strlen(y)); // no-warning 507} 508 509void strncat_no_overflow_2(char *y) { 510 char x[4] = "12"; 511 if (strlen(y) == 4) 512 strncat(x, y, 1); // no-warning 513} 514 515//===----------------------------------------------------------------------=== 516// strcmp() 517//===----------------------------------------------------------------------=== 518 519#define strcmp BUILTIN(strcmp) 520int strcmp(const char *restrict s1, const char *restrict s2); 521 522void strcmp_constant0() { 523 if (strcmp("123", "123") != 0) 524 (void)*(char*)0; // no-warning 525} 526 527void strcmp_constant_and_var_0() { 528 char *x = "123"; 529 if (strcmp(x, "123") != 0) 530 (void)*(char*)0; // no-warning 531} 532 533void strcmp_constant_and_var_1() { 534 char *x = "123"; 535 if (strcmp("123", x) != 0) 536 (void)*(char*)0; // no-warning 537} 538 539void strcmp_0() { 540 char *x = "123"; 541 char *y = "123"; 542 if (strcmp(x, y) != 0) 543 (void)*(char*)0; // no-warning 544} 545 546void strcmp_1() { 547 char *x = "234"; 548 char *y = "123"; 549 if (strcmp(x, y) != 1) 550 (void)*(char*)0; // no-warning 551} 552 553void strcmp_2() { 554 char *x = "123"; 555 char *y = "234"; 556 if (strcmp(x, y) != -1) 557 (void)*(char*)0; // no-warning 558} 559 560void strcmp_null_0() { 561 char *x = NULL; 562 char *y = "123"; 563 strcmp(x, y); // expected-warning{{Null pointer argument in call to byte string function}} 564} 565 566void strcmp_null_1() { 567 char *x = "123"; 568 char *y = NULL; 569 strcmp(x, y); // expected-warning{{Null pointer argument in call to byte string function}} 570} 571 572void strcmp_diff_length_0() { 573 char *x = "12345"; 574 char *y = "234"; 575 if (strcmp(x, y) != -1) 576 (void)*(char*)0; // no-warning 577} 578 579void strcmp_diff_length_1() { 580 char *x = "123"; 581 char *y = "23456"; 582 if (strcmp(x, y) != -1) 583 (void)*(char*)0; // no-warning 584} 585 586void strcmp_diff_length_2() { 587 char *x = "12345"; 588 char *y = "123"; 589 if (strcmp(x, y) != 1) 590 (void)*(char*)0; // no-warning 591} 592 593void strcmp_diff_length_3() { 594 char *x = "123"; 595 char *y = "12345"; 596 if (strcmp(x, y) != -1) 597 (void)*(char*)0; // no-warning 598} 599 600//===----------------------------------------------------------------------=== 601// strncmp() 602//===----------------------------------------------------------------------=== 603 604#define strncmp BUILTIN(strncmp) 605int strncmp(const char *restrict s1, const char *restrict s2, size_t n); 606 607void strncmp_constant0() { 608 if (strncmp("123", "123", 3) != 0) 609 (void)*(char*)0; // no-warning 610} 611 612void strncmp_constant_and_var_0() { 613 char *x = "123"; 614 if (strncmp(x, "123", 3) != 0) 615 (void)*(char*)0; // no-warning 616} 617 618void strncmp_constant_and_var_1() { 619 char *x = "123"; 620 if (strncmp("123", x, 3) != 0) 621 (void)*(char*)0; // no-warning 622} 623 624void strncmp_0() { 625 char *x = "123"; 626 char *y = "123"; 627 if (strncmp(x, y, 3) != 0) 628 (void)*(char*)0; // no-warning 629} 630 631void strncmp_1() { 632 char *x = "234"; 633 char *y = "123"; 634 if (strncmp(x, y, 3) != 1) 635 (void)*(char*)0; // no-warning 636} 637 638void strncmp_2() { 639 char *x = "123"; 640 char *y = "234"; 641 if (strncmp(x, y, 3) != -1) 642 (void)*(char*)0; // no-warning 643} 644 645void strncmp_null_0() { 646 char *x = NULL; 647 char *y = "123"; 648 strncmp(x, y, 3); // expected-warning{{Null pointer argument in call to byte string function}} 649} 650 651void strncmp_null_1() { 652 char *x = "123"; 653 char *y = NULL; 654 strncmp(x, y, 3); // expected-warning{{Null pointer argument in call to byte string function}} 655} 656 657void strncmp_diff_length_0() { 658 char *x = "12345"; 659 char *y = "234"; 660 if (strncmp(x, y, 5) != -1) 661 (void)*(char*)0; // no-warning 662} 663 664void strncmp_diff_length_1() { 665 char *x = "123"; 666 char *y = "23456"; 667 if (strncmp(x, y, 5) != -1) 668 (void)*(char*)0; // no-warning 669} 670 671void strncmp_diff_length_2() { 672 char *x = "12345"; 673 char *y = "123"; 674 if (strncmp(x, y, 5) != 1) 675 (void)*(char*)0; // no-warning 676} 677 678void strncmp_diff_length_3() { 679 char *x = "123"; 680 char *y = "12345"; 681 if (strncmp(x, y, 5) != -1) 682 (void)*(char*)0; // no-warning 683} 684 685void strncmp_diff_length_4() { 686 char *x = "123"; 687 char *y = "12345"; 688 if (strncmp(x, y, 3) != 0) 689 (void)*(char*)0; // no-warning 690} 691 692void strncmp_diff_length_5() { 693 char *x = "012"; 694 char *y = "12345"; 695 if (strncmp(x, y, 3) != -1) 696 (void)*(char*)0; // no-warning 697} 698 699void strncmp_diff_length_6() { 700 char *x = "234"; 701 char *y = "12345"; 702 if (strncmp(x, y, 3) != 1) 703 (void)*(char*)0; // no-warning 704} 705 706//===----------------------------------------------------------------------=== 707// strcasecmp() 708//===----------------------------------------------------------------------=== 709 710#define strcasecmp BUILTIN(strcasecmp) 711int strcasecmp(const char *restrict s1, const char *restrict s2); 712 713void strcasecmp_constant0() { 714 if (strcasecmp("abc", "Abc") != 0) 715 (void)*(char*)0; // no-warning 716} 717 718void strcasecmp_constant_and_var_0() { 719 char *x = "abc"; 720 if (strcasecmp(x, "Abc") != 0) 721 (void)*(char*)0; // no-warning 722} 723 724void strcasecmp_constant_and_var_1() { 725 char *x = "abc"; 726 if (strcasecmp("Abc", x) != 0) 727 (void)*(char*)0; // no-warning 728} 729 730void strcasecmp_0() { 731 char *x = "abc"; 732 char *y = "Abc"; 733 if (strcasecmp(x, y) != 0) 734 (void)*(char*)0; // no-warning 735} 736 737void strcasecmp_1() { 738 char *x = "Bcd"; 739 char *y = "abc"; 740 if (strcasecmp(x, y) != 1) 741 (void)*(char*)0; // no-warning 742} 743 744void strcasecmp_2() { 745 char *x = "abc"; 746 char *y = "Bcd"; 747 if (strcasecmp(x, y) != -1) 748 (void)*(char*)0; // no-warning 749} 750 751void strcasecmp_null_0() { 752 char *x = NULL; 753 char *y = "123"; 754 strcasecmp(x, y); // expected-warning{{Null pointer argument in call to byte string function}} 755} 756 757void strcasecmp_null_1() { 758 char *x = "123"; 759 char *y = NULL; 760 strcasecmp(x, y); // expected-warning{{Null pointer argument in call to byte string function}} 761} 762 763void strcasecmp_diff_length_0() { 764 char *x = "abcde"; 765 char *y = "aBd"; 766 if (strcasecmp(x, y) != -1) 767 (void)*(char*)0; // no-warning 768} 769 770void strcasecmp_diff_length_1() { 771 char *x = "abc"; 772 char *y = "aBdef"; 773 if (strcasecmp(x, y) != -1) 774 (void)*(char*)0; // no-warning 775} 776 777void strcasecmp_diff_length_2() { 778 char *x = "aBcDe"; 779 char *y = "abc"; 780 if (strcasecmp(x, y) != 1) 781 (void)*(char*)0; // no-warning 782} 783 784void strcasecmp_diff_length_3() { 785 char *x = "aBc"; 786 char *y = "abcde"; 787 if (strcasecmp(x, y) != -1) 788 (void)*(char*)0; // no-warning 789} 790 791//===----------------------------------------------------------------------=== 792// strncasecmp() 793//===----------------------------------------------------------------------=== 794 795#define strncasecmp BUILTIN(strncasecmp) 796int strncasecmp(const char *restrict s1, const char *restrict s2, size_t n); 797 798void strncasecmp_constant0() { 799 if (strncasecmp("abc", "Abc", 3) != 0) 800 (void)*(char*)0; // no-warning 801} 802 803void strncasecmp_constant_and_var_0() { 804 char *x = "abc"; 805 if (strncasecmp(x, "Abc", 3) != 0) 806 (void)*(char*)0; // no-warning 807} 808 809void strncasecmp_constant_and_var_1() { 810 char *x = "abc"; 811 if (strncasecmp("Abc", x, 3) != 0) 812 (void)*(char*)0; // no-warning 813} 814 815void strncasecmp_0() { 816 char *x = "abc"; 817 char *y = "Abc"; 818 if (strncasecmp(x, y, 3) != 0) 819 (void)*(char*)0; // no-warning 820} 821 822void strncasecmp_1() { 823 char *x = "Bcd"; 824 char *y = "abc"; 825 if (strncasecmp(x, y, 3) != 1) 826 (void)*(char*)0; // no-warning 827} 828 829void strncasecmp_2() { 830 char *x = "abc"; 831 char *y = "Bcd"; 832 if (strncasecmp(x, y, 3) != -1) 833 (void)*(char*)0; // no-warning 834} 835 836void strncasecmp_null_0() { 837 char *x = NULL; 838 char *y = "123"; 839 strncasecmp(x, y, 3); // expected-warning{{Null pointer argument in call to byte string function}} 840} 841 842void strncasecmp_null_1() { 843 char *x = "123"; 844 char *y = NULL; 845 strncasecmp(x, y, 3); // expected-warning{{Null pointer argument in call to byte string function}} 846} 847 848void strncasecmp_diff_length_0() { 849 char *x = "abcde"; 850 char *y = "aBd"; 851 if (strncasecmp(x, y, 5) != -1) 852 (void)*(char*)0; // no-warning 853} 854 855void strncasecmp_diff_length_1() { 856 char *x = "abc"; 857 char *y = "aBdef"; 858 if (strncasecmp(x, y, 5) != -1) 859 (void)*(char*)0; // no-warning 860} 861 862void strncasecmp_diff_length_2() { 863 char *x = "aBcDe"; 864 char *y = "abc"; 865 if (strncasecmp(x, y, 5) != 1) 866 (void)*(char*)0; // no-warning 867} 868 869void strncasecmp_diff_length_3() { 870 char *x = "aBc"; 871 char *y = "abcde"; 872 if (strncasecmp(x, y, 5) != -1) 873 (void)*(char*)0; // no-warning 874} 875 876void strncasecmp_diff_length_4() { 877 char *x = "abcde"; 878 char *y = "aBc"; 879 if (strncasecmp(x, y, 3) != 0) 880 (void)*(char*)0; // no-warning 881} 882 883void strncasecmp_diff_length_5() { 884 char *x = "abcde"; 885 char *y = "aBd"; 886 if (strncasecmp(x, y, 3) != -1) 887 (void)*(char*)0; // no-warning 888} 889 890void strncasecmp_diff_length_6() { 891 char *x = "aBDe"; 892 char *y = "abc"; 893 if (strncasecmp(x, y, 3) != 1) 894 (void)*(char*)0; // no-warning 895} 896