229 lines · cpp
1// RUN: %clang_cc1 -std=c++20 -Wunsafe-buffer-usage \2// RUN: -fsafe-buffer-usage-suggestions \3// RUN: -fdiagnostics-parseable-fixits %s 2>&1 | FileCheck %s4typedef int * Int_ptr_t;5typedef int Int_t;6 7void simple(unsigned idx) {8 int buffer[10];9// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:3-[[@LINE-1]]:17}:"std::array<int, 10> buffer"10 buffer[idx] = 0;11}12 13void array2d(unsigned idx) {14 int buffer[10][10];15// CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]16 buffer[idx][idx] = 0;17}18 19void array2d_vla(unsigned sz, unsigned idx) {20 int buffer1[10][sz];21// CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]22 int buffer2[sz][10];23// CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]24 buffer1[idx][idx] = 0;25 buffer2[idx][idx] = 0;26}27 28void array2d_assign_from_elem(unsigned idx) {29 int buffer[10][10];30// CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]31 int a = buffer[idx][idx];32}33 34void array2d_use(int *);35void array2d_call(unsigned idx) {36 int buffer[10][10];37// CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]38 array2d_use(buffer[idx]);39}40void array2d_call_vla(unsigned sz, unsigned idx) {41 int buffer[10][sz];42// CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]43 array2d_use(buffer[idx]);44}45 46void array2d_typedef(unsigned idx) {47 typedef int ten_ints_t[10];48// CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]49 ten_ints_t buffer[10];50// CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]51 buffer[idx][idx] = 0;52}53 54void whitespace_in_declaration(unsigned idx) {55 int buffer_w [ 10 ];56// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:3-[[@LINE-1]]:35}:"std::array<int, 10> buffer_w"57 buffer_w[idx] = 0;58}59 60void comments_in_declaration(unsigned idx) {61 int /* [A] */ buffer_w /* [B] */ [ /* [C] */ 10 /* [D] */ ] ;62// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:3-[[@LINE-1]]:69}:"std::array<int /* [A] */, /* [C] */ 10 /* [D] */> buffer_w"63 buffer_w[idx] = 0;64}65 66void initializer(unsigned idx) {67 int buffer[3] = {0};68// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:3-[[@LINE-1]]:16}:"std::array<int, 3> buffer"69 70 int buffer2[3] = {0, 1, 2};71// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:3-[[@LINE-1]]:17}:"std::array<int, 3> buffer2"72 73 buffer[idx] = 0;74 buffer2[idx] = 0;75}76 77void auto_size(unsigned idx) {78 int buffer[] = {0, 1, 2};79// CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]80// FIXME: implement support81 82 buffer[idx] = 0;83}84 85void universal_initialization(unsigned idx) {86 int buffer[] {0, 1, 2};87// CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]88// FIXME: implement support89 90 buffer[idx] = 0;91}92 93void multi_decl1(unsigned idx) {94 int a, buffer[10];95// CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]96// FIXME: implement support97 98 buffer[idx] = 0;99}100 101void multi_decl2(unsigned idx) {102 int buffer[10], b;103// CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]104// FIXME: implement support105 106 buffer[idx] = 0;107}108 109void local_array_ptr_to_const(unsigned idx, const int*& a) {110 const int * buffer[10] = {a};111// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:3-[[@LINE-1]]:25}:"std::array<const int *, 10> buffer"112 a = buffer[idx];113}114 115void local_array_const_ptr(unsigned idx, int*& a) {116 int * const buffer[10] = {a};117// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:3-[[@LINE-1]]:25}:"std::array<int * const, 10> buffer"118 119 a = buffer[idx];120}121 122void local_array_const_ptr_via_typedef(unsigned idx, int*& a) {123 typedef int * const my_const_ptr;124 my_const_ptr buffer[10] = {a};125// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:3-[[@LINE-1]]:26}:"std::array<my_const_ptr, 10> buffer"126 127 a = buffer[idx];128}129 130void local_array_const_ptr_to_const(unsigned idx, const int*& a) {131 const int * const buffer[10] = {a};132// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:3-[[@LINE-1]]:31}:"std::array<const int * const, 10> buffer"133 134 a = buffer[idx];135 136}137 138template<typename T>139void unsupported_local_array_in_template(unsigned idx) {140 T buffer[10];141// CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]:.*-[[@LINE-1]]:.*}142 buffer[idx] = 0;143}144// Instantiate the template function to force its analysis.145template void unsupported_local_array_in_template<int>(unsigned);146 147typedef unsigned int my_uint;148void typedef_as_elem_type(unsigned idx) {149 my_uint buffer[10];150// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:3-[[@LINE-1]]:21}:"std::array<my_uint, 10> buffer"151 buffer[idx] = 0;152}153 154void decltype_as_elem_type(unsigned idx) {155 int a;156 decltype(a) buffer[10];157// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:3-[[@LINE-1]]:25}:"std::array<decltype(a), 10> buffer"158 buffer[idx] = 0;159}160 161void macro_as_elem_type(unsigned idx) {162#define MY_INT int163 MY_INT buffer[10];164// CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]:.*-[[@LINE-1]]:.*}165// FIXME: implement support166 167 buffer[idx] = 0;168#undef MY_INT169}170 171void macro_as_identifier(unsigned idx) {172#define MY_BUFFER buffer173 int MY_BUFFER[10];174// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:3-[[@LINE-1]]:20}:"std::array<int, 10> MY_BUFFER"175 MY_BUFFER[idx] = 0;176#undef MY_BUFFER177}178 179void macro_as_size(unsigned idx) {180#define MY_TEN 10181 int buffer[MY_TEN];182// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:3-[[@LINE-1]]:21}:"std::array<int, MY_TEN> buffer"183 buffer[idx] = 0;184#undef MY_TEN185}186 187typedef unsigned int my_array[42];188// CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]:.*-[[@LINE-1]]:.*}189void typedef_as_array_type(unsigned idx) {190 my_array buffer;191// CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]:.*-[[@LINE-1]]:.*}192 buffer[idx] = 0;193}194 195void decltype_as_array_type(unsigned idx) {196 int buffer[42];197// CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]:.*-[[@LINE-1]]:.*}198 decltype(buffer) buffer2;199// CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]:.*-[[@LINE-1]]:.*}200 buffer2[idx] = 0;201}202 203void constant_as_size(unsigned idx) {204 const unsigned my_const = 10;205 int buffer[my_const];206// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:3-[[@LINE-1]]:23}:"std::array<int, my_const> buffer"207 buffer[idx] = 0;208}209 210void subscript_negative() {211 int buffer[10];212// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:3-[[@LINE-1]]:17}:"std::array<int, 10> buffer"213 214 // For constant-size arrays any negative index will lead to buffer underflow.215 // std::array::operator[] has unsigned parameter so the value will be casted to unsigned.216 // This will very likely be buffer overflow but hardened std::array catch these at runtime.217 buffer[-5] = 0;218}219 220void subscript_signed(int signed_idx) {221 int buffer[10];222// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:3-[[@LINE-1]]:17}:"std::array<int, 10> buffer"223 224 // For constant-size arrays any negative index will lead to buffer underflow.225 // std::array::operator[] has unsigned parameter so the value will be casted to unsigned.226 // This will very likely be buffer overflow but hardened std::array catches these at runtime.227 buffer[signed_idx] = 0;228}229