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