|
| 1 | +// RUN: %clang_analyze_cc1 -std=c++11 -Wno-array-bounds -analyzer-checker=unix,core,security.ArrayBound -verify %s |
| 2 | + |
| 3 | +// Test the interactions of `security.ArrayBound` with C++ features. |
| 4 | + |
| 5 | +void test_tainted_index_local() { |
| 6 | + int arr[10]; |
| 7 | + unsigned index = 10; |
| 8 | + arr[index] = 7; |
| 9 | + // expected-warning@-1{{Out of bound access to memory after the end of 'arr'}} |
| 10 | +} |
| 11 | + |
| 12 | +void test_tainted_index_local_range() { |
| 13 | + int arr[10]; |
| 14 | + for (unsigned index = 0; index < 11; index++) |
| 15 | + arr[index] = index; |
| 16 | + // expected-warning@-1{{Out of bound access to memory after the end of 'arr'}} |
| 17 | +} |
| 18 | + |
| 19 | +void test_tainted_index1(unsigned index) { |
| 20 | + int arr[10]; |
| 21 | + if (index < 12) |
| 22 | + arr[index] = index; |
| 23 | + // expected-warning@-1{{Out of bound access to memory after the end of 'arr'}} |
| 24 | + if (index == 12) |
| 25 | + arr[index] = index; |
| 26 | + // expected-warning@-1{{Out of bound access to memory after the end of 'arr'}} |
| 27 | +} |
| 28 | + |
| 29 | +void test_tainted_index2(unsigned index) { |
| 30 | + int arr[10]; |
| 31 | + if (index < 12) |
| 32 | + arr[index] = index; |
| 33 | + // expected-warning@-1{{Out of bound access to memory after the end of 'arr'}} |
| 34 | +} |
| 35 | + |
| 36 | +unsigned strlen(const char *s); |
| 37 | +void strcpy(char *dst, char *src); |
| 38 | +void test_ex(int argc, char *argv[]) { |
| 39 | + char proc_name[16]; |
| 40 | + unsigned offset = strlen(argv[0]); |
| 41 | + if (offset == 16) { |
| 42 | + strcpy(proc_name, argv[0]); |
| 43 | + proc_name[offset] = 'x'; |
| 44 | + // expected-warning@-1{{Out of bound access to memory after the end of 'proc_name'}} |
| 45 | + } |
| 46 | + if (offset <= 16) { |
| 47 | + strcpy(proc_name, argv[0]); |
| 48 | + proc_name[offset] = argv[0][16]; |
| 49 | + // expected-warning@-1{{Out of bound access to memory after the end of the region}} |
| 50 | + // expected-warning@-2{{Out of bound access to memory after the end of 'proc_name'}} |
| 51 | + } |
| 52 | +} |
0 commit comments