outofbound.c revision 540dda6f2e4982b3eab0300c804345f5b6104c11
18a68774cdbe5609c50d7ee9dad3b6040e4f5fbe1Ulrich Drepper// RUN: %clang_cc1 -Wno-array-bounds -analyze -analyzer-checker=core,experimental.unix,experimental.security.ArrayBound -analyzer-store=region -verify %s 244173edad1dfd24c4d98fcf032308f7df28db652Ulrich Drepper 3361df7da6dfecd817b27e62b91752ac316d7cdd4Ulrich Dreppertypedef __typeof(sizeof(int)) size_t; 48a68774cdbe5609c50d7ee9dad3b6040e4f5fbe1Ulrich Dreppervoid *malloc(size_t); 58a68774cdbe5609c50d7ee9dad3b6040e4f5fbe1Ulrich Dreppervoid *calloc(size_t, size_t); 6361df7da6dfecd817b27e62b91752ac316d7cdd4Ulrich Drepper 7361df7da6dfecd817b27e62b91752ac316d7cdd4Ulrich Drepperchar f1() { 8361df7da6dfecd817b27e62b91752ac316d7cdd4Ulrich Drepper char* s = "abcd"; 98a68774cdbe5609c50d7ee9dad3b6040e4f5fbe1Ulrich Drepper char c = s[4]; // no-warning 10361df7da6dfecd817b27e62b91752ac316d7cdd4Ulrich Drepper return s[5] + c; // expected-warning{{Access out-of-bound array element (buffer overflow)}} 11361df7da6dfecd817b27e62b91752ac316d7cdd4Ulrich Drepper} 12361df7da6dfecd817b27e62b91752ac316d7cdd4Ulrich Drepper 13361df7da6dfecd817b27e62b91752ac316d7cdd4Ulrich Dreppervoid f2() { 14361df7da6dfecd817b27e62b91752ac316d7cdd4Ulrich Drepper int *p = malloc(12); 15361df7da6dfecd817b27e62b91752ac316d7cdd4Ulrich Drepper p[3] = 4; // expected-warning{{Access out-of-bound array element (buffer overflow)}} 16361df7da6dfecd817b27e62b91752ac316d7cdd4Ulrich Drepper} 171e9ef50681e20ef14c2ba38aef37a71ff148be08Ulrich Drepper 18361df7da6dfecd817b27e62b91752ac316d7cdd4Ulrich Drepperstruct three_words { 19361df7da6dfecd817b27e62b91752ac316d7cdd4Ulrich Drepper int c[3]; 20361df7da6dfecd817b27e62b91752ac316d7cdd4Ulrich Drepper}; 21361df7da6dfecd817b27e62b91752ac316d7cdd4Ulrich Drepper 22361df7da6dfecd817b27e62b91752ac316d7cdd4Ulrich Drepperstruct seven_words { 23361df7da6dfecd817b27e62b91752ac316d7cdd4Ulrich Drepper int c[7]; 24361df7da6dfecd817b27e62b91752ac316d7cdd4Ulrich Drepper}; 25361df7da6dfecd817b27e62b91752ac316d7cdd4Ulrich Drepper 268a68774cdbe5609c50d7ee9dad3b6040e4f5fbe1Ulrich Dreppervoid f3() { 278a68774cdbe5609c50d7ee9dad3b6040e4f5fbe1Ulrich Drepper struct three_words a, *p; 288a68774cdbe5609c50d7ee9dad3b6040e4f5fbe1Ulrich Drepper p = &a; 298a68774cdbe5609c50d7ee9dad3b6040e4f5fbe1Ulrich Drepper p[0] = a; // no-warning 308a68774cdbe5609c50d7ee9dad3b6040e4f5fbe1Ulrich Drepper p[1] = a; // expected-warning{{Access out-of-bound array element (buffer overflow)}} 318a68774cdbe5609c50d7ee9dad3b6040e4f5fbe1Ulrich Drepper} 328a68774cdbe5609c50d7ee9dad3b6040e4f5fbe1Ulrich Drepper 338a68774cdbe5609c50d7ee9dad3b6040e4f5fbe1Ulrich Dreppervoid f4() { 348a68774cdbe5609c50d7ee9dad3b6040e4f5fbe1Ulrich Drepper struct seven_words c; 358a68774cdbe5609c50d7ee9dad3b6040e4f5fbe1Ulrich Drepper struct three_words a, *p = (struct three_words *)&c; 368a68774cdbe5609c50d7ee9dad3b6040e4f5fbe1Ulrich Drepper p[0] = a; // no-warning 378a68774cdbe5609c50d7ee9dad3b6040e4f5fbe1Ulrich Drepper p[1] = a; // no-warning 388a68774cdbe5609c50d7ee9dad3b6040e4f5fbe1Ulrich Drepper p[2] = a; // expected-warning{{Access out-of-bound array element (buffer overflow)}} 398a68774cdbe5609c50d7ee9dad3b6040e4f5fbe1Ulrich Drepper} 408a68774cdbe5609c50d7ee9dad3b6040e4f5fbe1Ulrich Drepper 418a68774cdbe5609c50d7ee9dad3b6040e4f5fbe1Ulrich Dreppervoid f5() { 428a68774cdbe5609c50d7ee9dad3b6040e4f5fbe1Ulrich Drepper char *p = calloc(2,2); 438a68774cdbe5609c50d7ee9dad3b6040e4f5fbe1Ulrich Drepper p[3] = '.'; // no-warning 448a68774cdbe5609c50d7ee9dad3b6040e4f5fbe1Ulrich Drepper p[4] = '!'; // expected-warning{{out-of-bound}} 458a68774cdbe5609c50d7ee9dad3b6040e4f5fbe1Ulrich Drepper} 468a68774cdbe5609c50d7ee9dad3b6040e4f5fbe1Ulrich Drepper 478a68774cdbe5609c50d7ee9dad3b6040e4f5fbe1Ulrich Dreppervoid f6() { 488a68774cdbe5609c50d7ee9dad3b6040e4f5fbe1Ulrich Drepper char a[2]; 498a68774cdbe5609c50d7ee9dad3b6040e4f5fbe1Ulrich Drepper int *b = (int*)a; 508a68774cdbe5609c50d7ee9dad3b6040e4f5fbe1Ulrich Drepper b[1] = 3; // expected-warning{{out-of-bound}} 518a68774cdbe5609c50d7ee9dad3b6040e4f5fbe1Ulrich Drepper} 52fdc93e12a77866cafd1aae4463d89cef2c01d9b1Ulrich Drepper 538a68774cdbe5609c50d7ee9dad3b6040e4f5fbe1Ulrich Dreppervoid f7() { 548a68774cdbe5609c50d7ee9dad3b6040e4f5fbe1Ulrich Drepper struct three_words a; 55fdc93e12a77866cafd1aae4463d89cef2c01d9b1Ulrich Drepper a.c[3] = 1; // expected-warning{{out-of-bound}} 568a68774cdbe5609c50d7ee9dad3b6040e4f5fbe1Ulrich Drepper} 578a68774cdbe5609c50d7ee9dad3b6040e4f5fbe1Ulrich Drepper 588a68774cdbe5609c50d7ee9dad3b6040e4f5fbe1Ulrich Dreppervoid vla(int a) { 598a68774cdbe5609c50d7ee9dad3b6040e4f5fbe1Ulrich Drepper if (a == 5) { 608a68774cdbe5609c50d7ee9dad3b6040e4f5fbe1Ulrich Drepper int x[a]; 618a68774cdbe5609c50d7ee9dad3b6040e4f5fbe1Ulrich Drepper x[4] = 4; // no-warning 628a68774cdbe5609c50d7ee9dad3b6040e4f5fbe1Ulrich Drepper x[5] = 5; // expected-warning{{out-of-bound}} 638a68774cdbe5609c50d7ee9dad3b6040e4f5fbe1Ulrich Drepper } 648a68774cdbe5609c50d7ee9dad3b6040e4f5fbe1Ulrich Drepper} 658a68774cdbe5609c50d7ee9dad3b6040e4f5fbe1Ulrich Drepper 668a68774cdbe5609c50d7ee9dad3b6040e4f5fbe1Ulrich Dreppervoid alloca_region(int a) { 678a68774cdbe5609c50d7ee9dad3b6040e4f5fbe1Ulrich Drepper if (a == 5) { 688a68774cdbe5609c50d7ee9dad3b6040e4f5fbe1Ulrich Drepper char *x = __builtin_alloca(a); 698a68774cdbe5609c50d7ee9dad3b6040e4f5fbe1Ulrich Drepper x[4] = 4; // no-warning 708a68774cdbe5609c50d7ee9dad3b6040e4f5fbe1Ulrich Drepper x[5] = 5; // expected-warning{{out-of-bound}} 718a68774cdbe5609c50d7ee9dad3b6040e4f5fbe1Ulrich Drepper } 728a68774cdbe5609c50d7ee9dad3b6040e4f5fbe1Ulrich Drepper} 738a68774cdbe5609c50d7ee9dad3b6040e4f5fbe1Ulrich Drepper 748a68774cdbe5609c50d7ee9dad3b6040e4f5fbe1Ulrich Drepperint symbolic_index(int a) { 758a68774cdbe5609c50d7ee9dad3b6040e4f5fbe1Ulrich Drepper int x[2] = {1, 2}; 768a68774cdbe5609c50d7ee9dad3b6040e4f5fbe1Ulrich Drepper if (a == 2) { 778a68774cdbe5609c50d7ee9dad3b6040e4f5fbe1Ulrich Drepper return x[a]; // expected-warning{{out-of-bound}} 788a68774cdbe5609c50d7ee9dad3b6040e4f5fbe1Ulrich Drepper } 798a68774cdbe5609c50d7ee9dad3b6040e4f5fbe1Ulrich Drepper return 0; 808a68774cdbe5609c50d7ee9dad3b6040e4f5fbe1Ulrich Drepper} 818a68774cdbe5609c50d7ee9dad3b6040e4f5fbe1Ulrich Drepper 828a68774cdbe5609c50d7ee9dad3b6040e4f5fbe1Ulrich Drepperint symbolic_index2(int a) { 838a68774cdbe5609c50d7ee9dad3b6040e4f5fbe1Ulrich Drepper int x[2] = {1, 2}; 848a68774cdbe5609c50d7ee9dad3b6040e4f5fbe1Ulrich Drepper if (a < 0) { 858a68774cdbe5609c50d7ee9dad3b6040e4f5fbe1Ulrich Drepper return x[a]; // expected-warning{{out-of-bound}} 868a68774cdbe5609c50d7ee9dad3b6040e4f5fbe1Ulrich Drepper } 878a68774cdbe5609c50d7ee9dad3b6040e4f5fbe1Ulrich Drepper return 0; 888a68774cdbe5609c50d7ee9dad3b6040e4f5fbe1Ulrich Drepper} 898a68774cdbe5609c50d7ee9dad3b6040e4f5fbe1Ulrich Drepper