| 1 |
// RUN: %clang_cc1 -std=c++20 -Wno-all -Wunsafe-buffer-usage \ |
| 2 |
// RUN: -fsafe-buffer-usage-suggestions -verify %s |
| 3 |
|
| 4 |
namespace localVar { |
| 5 |
void testRefersPtrLocalVarDecl(int i) { |
| 6 |
int * ptr; // expected-warning{{'ptr' is an unsafe pointer used for buffer access}} |
| 7 |
ptr + i; // expected-note{{used in pointer arithmetic here}} |
| 8 |
ptr[i]; // expected-note{{used in buffer access here}} |
| 9 |
} |
| 10 |
|
| 11 |
void testRefersArrayLocalVarDecl(int i) { |
| 12 |
int array[i]; // expected-warning{{'array' is an unsafe buffer that does not perform bounds}} |
| 13 |
array[i/2]; // expected-note{{used in buffer access here}} |
| 14 |
} |
| 15 |
} |
| 16 |
|
| 17 |
namespace globalVar { |
| 18 |
int * ptr; // expected-warning{{'ptr' is an unsafe pointer used for buffer access}} |
| 19 |
void testRefersPtrGlobalVarDecl(int i) { |
| 20 |
ptr + i; // expected-note{{used in pointer arithmetic here}} |
| 21 |
ptr[i]; // expected-note{{used in buffer access here}} |
| 22 |
} |
| 23 |
|
| 24 |
int array[10]; // expected-warning{{'array' is an unsafe buffer that does not perform bounds}} |
| 25 |
void testRefersArrayGlobalVarDecl(int i) { |
| 26 |
array[i/2]; // expected-note{{used in buffer access here}} |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
namespace functionParm { |
| 31 |
void testRefersPtrParmVarDecl(int * ptr) { |
| 32 |
// expected-warning@-1{{'ptr' is an unsafe pointer used for buffer access}} |
| 33 |
ptr + 5; // expected-note{{used in pointer arithmetic here}} |
| 34 |
ptr[5]; // expected-note{{used in buffer access here}} |
| 35 |
} |
| 36 |
|
| 37 |
// FIXME: shall we explain the array to pointer decay to make the warning more understandable? |
| 38 |
void testRefersArrayParmVarDecl(int array[10]) { |
| 39 |
// expected-warning@-1{{'array' is an unsafe pointer used for buffer access}} |
| 40 |
array[2]; // expected-note{{used in buffer access here}} |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
namespace structField { |
| 45 |
struct Struct1 { |
| 46 |
int * ptr; // FIXME: per-declaration warning aggregated at the struct definition? |
| 47 |
}; |
| 48 |
|
| 49 |
void testRefersPtrStructFieldDecl(int i) { |
| 50 |
Struct1 s1; |
| 51 |
s1.ptr + i; // expected-warning{{unsafe pointer arithmetic}} |
| 52 |
s1.ptr[i]; // expected-warning{{unsafe buffer access}} |
| 53 |
} |
| 54 |
|
| 55 |
struct Struct2 { |
| 56 |
int array[10]; // FIXME: per-declaration warning aggregated at the struct definition? |
| 57 |
}; |
| 58 |
|
| 59 |
void testRefersArrayStructFieldDecl(int i) { |
| 60 |
Struct2 s2; |
| 61 |
s2.array[i/2]; // expected-warning{{unsafe buffer access}} |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
namespace structFieldFromMethod { |
| 66 |
struct Struct1 { |
| 67 |
int * ptr; // FIXME: per-declaration warning aggregated at the struct definition |
| 68 |
|
| 69 |
void testRefersPtrStructFieldDecl(int i) { |
| 70 |
ptr + i; // expected-warning{{unsafe pointer arithmetic}} |
| 71 |
ptr[i]; // expected-warning{{unsafe buffer access}} |
| 72 |
} |
| 73 |
}; |
| 74 |
|
| 75 |
struct Struct2 { |
| 76 |
int array[10]; // FIXME: per-declaration warning aggregated at the struct definition |
| 77 |
|
| 78 |
void testRefersArrayStructFieldDecl(int i) { |
| 79 |
Struct2 s2; |
| 80 |
s2.array[i/2]; // expected-warning{{unsafe buffer access}} |
| 81 |
} |
| 82 |
}; |
| 83 |
} |
| 84 |
|
| 85 |
namespace staticStructField { |
| 86 |
struct Struct1 { |
| 87 |
static int * ptr; // expected-warning{{'ptr' is an unsafe pointer used for buffer access}} |
| 88 |
}; |
| 89 |
|
| 90 |
void testRefersPtrStructFieldDecl(int i) { |
| 91 |
Struct1::ptr + i; // expected-note{{used in pointer arithmetic here}} |
| 92 |
Struct1::ptr[i]; // expected-note{{used in buffer access here}} |
| 93 |
} |
| 94 |
|
| 95 |
struct Struct2 { |
| 96 |
static int array[10]; // expected-warning{{'array' is an unsafe buffer that does not perform bounds}} |
| 97 |
}; |
| 98 |
|
| 99 |
void testRefersArrayStructFieldDecl(int i) { |
| 100 |
Struct2::array[i/2]; // expected-note{{used in buffer access here}} |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
int * return_ptr(); |
| 105 |
|
| 106 |
void testNoDeclRef(int i) { |
| 107 |
return_ptr() + i; // expected-warning{{unsafe pointer arithmetic}} |
| 108 |
return_ptr()[i]; // expected-warning{{unsafe buffer access}} |
| 109 |
} |