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