File tree 3 files changed +40
-0
lines changed
3 files changed +40
-0
lines changed Original file line number Diff line number Diff line change
1
+ // RUN: %clang -fsanitize=bounds %s -O3 -o %T/bounds.exe
2
+ // RUN: %T/bounds.exe 0 0 0
3
+ // RUN: %T/bounds.exe 1 2 3
4
+ // RUN: %T/bounds.exe 2 0 0 2>&1 | FileCheck %s --check-prefix=CHECK-A-2
5
+ // RUN: %T/bounds.exe 0 3 0 2>&1 | FileCheck %s --check-prefix=CHECK-B-3
6
+ // RUN: %T/bounds.exe 0 0 4 2>&1 | FileCheck %s --check-prefix=CHECK-C-4
7
+
8
+ int main (int argc, char **argv) {
9
+ int arr[2 ][3 ][4 ] = {};
10
+
11
+ return arr[argv[1 ][0 ] - ' 0' ][argv[2 ][0 ] - ' 0' ][argv[3 ][0 ] - ' 0' ];
12
+ // CHECK-A-2: bounds.cpp:11:10: runtime error: index 2 out of bounds for type 'int [2][3][4]'
13
+ // CHECK-B-3: bounds.cpp:11:10: runtime error: index 3 out of bounds for type 'int [3][4]'
14
+ // CHECK-C-4: bounds.cpp:11:10: runtime error: index 4 out of bounds for type 'int [4]'
15
+ }
Original file line number Diff line number Diff line change @@ -183,6 +183,22 @@ void __ubsan::__ubsan_handle_shift_out_of_bounds_abort(
183
183
Die ();
184
184
}
185
185
186
+ void __ubsan::__ubsan_handle_out_of_bounds (OutOfBoundsData *Data,
187
+ ValueHandle Index) {
188
+ SourceLocation Loc = Data->Loc .acquire ();
189
+ if (Loc.isDisabled ())
190
+ return ;
191
+
192
+ Value IndexVal (Data->IndexType , Index);
193
+ Diag (Loc, DL_Error, " index %0 out of bounds for type %1" )
194
+ << IndexVal << Data->ArrayType ;
195
+ }
196
+ void __ubsan::__ubsan_handle_out_of_bounds_abort (OutOfBoundsData *Data,
197
+ ValueHandle Index) {
198
+ __ubsan_handle_out_of_bounds (Data, Index);
199
+ Die ();
200
+ }
201
+
186
202
void __ubsan::__ubsan_handle_builtin_unreachable (UnreachableData *Data) {
187
203
Diag (Data->Loc , DL_Error, " execution reached a __builtin_unreachable() call" );
188
204
Die ();
Original file line number Diff line number Diff line change @@ -67,6 +67,15 @@ struct ShiftOutOfBoundsData {
67
67
RECOVERABLE (shift_out_of_bounds, ShiftOutOfBoundsData *Data,
68
68
ValueHandle LHS, ValueHandle RHS)
69
69
70
+ struct OutOfBoundsData {
71
+ SourceLocation Loc;
72
+ const TypeDescriptor &ArrayType;
73
+ const TypeDescriptor &IndexType;
74
+ };
75
+
76
+ // / \brief Handle an array index out of bounds error.
77
+ RECOVERABLE (out_of_bounds, OutOfBoundsData *Data, ValueHandle Index)
78
+
70
79
struct UnreachableData {
71
80
SourceLocation Loc;
72
81
};
You can’t perform that action at this time.
0 commit comments