1*b7bdf199SArtem Dergachev // RUN: %clang_cc1 -std=c++20 -Wno-all -Wunsafe-buffer-usage \ 2*b7bdf199SArtem Dergachev // RUN: -fsafe-buffer-usage-suggestions -verify %s 3237ca436SJan Korous 4237ca436SJan Korous namespace localVar { testRefersPtrLocalVarDecl(int i)5237ca436SJan Korousvoid testRefersPtrLocalVarDecl(int i) { 6237ca436SJan Korous int * ptr; // expected-warning{{'ptr' is an unsafe pointer used for buffer access}} 7237ca436SJan Korous ptr + i; // expected-note{{used in pointer arithmetic here}} 8237ca436SJan Korous ptr[i]; // expected-note{{used in buffer access here}} 9237ca436SJan Korous } 10237ca436SJan Korous testRefersArrayLocalVarDecl(int i)11237ca436SJan Korousvoid testRefersArrayLocalVarDecl(int i) { 12237ca436SJan Korous int array[i]; // expected-warning{{'array' is an unsafe buffer that does not perform bounds}} 13237ca436SJan Korous array[i/2]; // expected-note{{used in buffer access here}} 14237ca436SJan Korous } 15237ca436SJan Korous } 16237ca436SJan Korous 17237ca436SJan Korous namespace globalVar { 18237ca436SJan Korous int * ptr; // expected-warning{{'ptr' is an unsafe pointer used for buffer access}} testRefersPtrGlobalVarDecl(int i)19237ca436SJan Korousvoid testRefersPtrGlobalVarDecl(int i) { 20237ca436SJan Korous ptr + i; // expected-note{{used in pointer arithmetic here}} 21237ca436SJan Korous ptr[i]; // expected-note{{used in buffer access here}} 22237ca436SJan Korous } 23237ca436SJan Korous 24237ca436SJan Korous int array[10]; // expected-warning{{'array' is an unsafe buffer that does not perform bounds}} testRefersArrayGlobalVarDecl(int i)25237ca436SJan Korousvoid testRefersArrayGlobalVarDecl(int i) { 26237ca436SJan Korous array[i/2]; // expected-note{{used in buffer access here}} 27237ca436SJan Korous } 28237ca436SJan Korous } 29237ca436SJan Korous 30237ca436SJan Korous namespace functionParm { testRefersPtrParmVarDecl(int * ptr)31237ca436SJan Korousvoid testRefersPtrParmVarDecl(int * ptr) { 32237ca436SJan Korous // expected-warning@-1{{'ptr' is an unsafe pointer used for buffer access}} 33237ca436SJan Korous ptr + 5; // expected-note{{used in pointer arithmetic here}} 34237ca436SJan Korous ptr[5]; // expected-note{{used in buffer access here}} 35237ca436SJan Korous } 36237ca436SJan Korous 37237ca436SJan Korous // FIXME: shall we explain the array to pointer decay to make the warning more understandable? testRefersArrayParmVarDecl(int array[10])38237ca436SJan Korousvoid testRefersArrayParmVarDecl(int array[10]) { 39237ca436SJan Korous // expected-warning@-1{{'array' is an unsafe pointer used for buffer access}} 40237ca436SJan Korous array[2]; // expected-note{{used in buffer access here}} 41237ca436SJan Korous } 42237ca436SJan Korous } 43237ca436SJan Korous 44237ca436SJan Korous namespace structField { 45237ca436SJan Korous struct Struct1 { 46237ca436SJan Korous int * ptr; // FIXME: per-declaration warning aggregated at the struct definition? 47237ca436SJan Korous }; 48237ca436SJan Korous testRefersPtrStructFieldDecl(int i)49237ca436SJan Korousvoid testRefersPtrStructFieldDecl(int i) { 50237ca436SJan Korous Struct1 s1; 51237ca436SJan Korous s1.ptr + i; // expected-warning{{unsafe pointer arithmetic}} 52237ca436SJan Korous s1.ptr[i]; // expected-warning{{unsafe buffer access}} 53237ca436SJan Korous } 54237ca436SJan Korous 55237ca436SJan Korous struct Struct2 { 56237ca436SJan Korous int array[10]; // FIXME: per-declaration warning aggregated at the struct definition? 57237ca436SJan Korous }; 58237ca436SJan Korous testRefersArrayStructFieldDecl(int i)59237ca436SJan Korousvoid testRefersArrayStructFieldDecl(int i) { 60237ca436SJan Korous Struct2 s2; 61237ca436SJan Korous s2.array[i/2]; // expected-warning{{unsafe buffer access}} 62237ca436SJan Korous } 63237ca436SJan Korous } 64237ca436SJan Korous 65237ca436SJan Korous namespace structFieldFromMethod { 66237ca436SJan Korous struct Struct1 { 67237ca436SJan Korous int * ptr; // FIXME: per-declaration warning aggregated at the struct definition 68237ca436SJan Korous testRefersPtrStructFieldDeclstructFieldFromMethod::Struct169237ca436SJan Korous void testRefersPtrStructFieldDecl(int i) { 70237ca436SJan Korous ptr + i; // expected-warning{{unsafe pointer arithmetic}} 71237ca436SJan Korous ptr[i]; // expected-warning{{unsafe buffer access}} 72237ca436SJan Korous } 73237ca436SJan Korous }; 74237ca436SJan Korous 75237ca436SJan Korous struct Struct2 { 76237ca436SJan Korous int array[10]; // FIXME: per-declaration warning aggregated at the struct definition 77237ca436SJan Korous testRefersArrayStructFieldDeclstructFieldFromMethod::Struct278237ca436SJan Korous void testRefersArrayStructFieldDecl(int i) { 79237ca436SJan Korous Struct2 s2; 80237ca436SJan Korous s2.array[i/2]; // expected-warning{{unsafe buffer access}} 81237ca436SJan Korous } 82237ca436SJan Korous }; 83237ca436SJan Korous } 84237ca436SJan Korous 85237ca436SJan Korous namespace staticStructField { 86237ca436SJan Korous struct Struct1 { 87237ca436SJan Korous static int * ptr; // expected-warning{{'ptr' is an unsafe pointer used for buffer access}} 88237ca436SJan Korous }; 89237ca436SJan Korous testRefersPtrStructFieldDecl(int i)90237ca436SJan Korousvoid testRefersPtrStructFieldDecl(int i) { 91237ca436SJan Korous Struct1::ptr + i; // expected-note{{used in pointer arithmetic here}} 92237ca436SJan Korous Struct1::ptr[i]; // expected-note{{used in buffer access here}} 93237ca436SJan Korous } 94237ca436SJan Korous 95237ca436SJan Korous struct Struct2 { 96237ca436SJan Korous static int array[10]; // expected-warning{{'array' is an unsafe buffer that does not perform bounds}} 97237ca436SJan Korous }; 98237ca436SJan Korous testRefersArrayStructFieldDecl(int i)99237ca436SJan Korousvoid testRefersArrayStructFieldDecl(int i) { 100237ca436SJan Korous Struct2::array[i/2]; // expected-note{{used in buffer access here}} 101237ca436SJan Korous } 102237ca436SJan Korous } 103237ca436SJan Korous 104237ca436SJan Korous int * return_ptr(); 105237ca436SJan Korous testNoDeclRef(int i)106237ca436SJan Korousvoid testNoDeclRef(int i) { 107237ca436SJan Korous return_ptr() + i; // expected-warning{{unsafe pointer arithmetic}} 108237ca436SJan Korous return_ptr()[i]; // expected-warning{{unsafe buffer access}} 109237ca436SJan Korous } 110