|
| 1 | +// REQUIRES: x86-registered-target |
| 2 | +// RUN: %clang_cc1 -emit-llvm -triple x86_64 -fsanitize=array-bounds %s -o - | FileCheck %s --check-prefixes=CHECK,CHECK-STRICT-0 |
| 3 | +// RUN: %clang_cc1 -emit-llvm -triple x86_64 -fsanitize=array-bounds -fstrict-flex-arrays=1 %s -o - | FileCheck %s --check-prefixes=CHECK,CHECK-STRICT-1 |
| 4 | +// RUN: %clang_cc1 -emit-llvm -triple x86_64 -fsanitize=array-bounds -fstrict-flex-arrays=2 %s -o - | FileCheck %s --check-prefixes=CHECK,CHECK-STRICT-2 |
| 5 | + |
| 6 | +// Before flexible array member was added to C99, many projects use a |
| 7 | +// one-element array as the last emember of a structure as an alternative. |
| 8 | +// E.g. https://github.com/python/cpython/issues/94250 |
| 9 | +// Suppress such errors with -fstrict-flex-arrays=0. |
| 10 | +struct One { |
| 11 | + int a[1]; |
| 12 | +}; |
| 13 | +struct Two { |
| 14 | + int a[2]; |
| 15 | +}; |
| 16 | +struct Three { |
| 17 | + int a[3]; |
| 18 | +}; |
| 19 | + |
| 20 | +// CHECK-LABEL: define {{.*}} @test_one( |
| 21 | +int test_one(struct One *p, int i) { |
| 22 | + // CHECK-STRICT-0-NOT: @__ubsan |
| 23 | + // CHECK-STRICT-1-NOT: @__ubsan |
| 24 | + // CHECK-STRICT-2: call void @__ubsan_handle_out_of_bounds_abort( |
| 25 | + return p->a[i] + (p->a)[i]; |
| 26 | +} |
| 27 | + |
| 28 | +// CHECK-LABEL: define {{.*}} @test_two( |
| 29 | +int test_two(struct Two *p, int i) { |
| 30 | + // CHECK-STRICT-0: call void @__ubsan_handle_out_of_bounds_abort( |
| 31 | + // CHECK-STRICT-1: call void @__ubsan_handle_out_of_bounds_abort( |
| 32 | + // CHECK-STRICT-2: call void @__ubsan_handle_out_of_bounds_abort( |
| 33 | + return p->a[i] + (p->a)[i]; |
| 34 | +} |
| 35 | + |
| 36 | +// CHECK-LABEL: define {{.*}} @test_three( |
| 37 | +int test_three(struct Three *p, int i) { |
| 38 | + // CHECK-STRICT-0: call void @__ubsan_handle_out_of_bounds_abort( |
| 39 | + // CHECK-STRICT-1: call void @__ubsan_handle_out_of_bounds_abort( |
| 40 | + // CHECK-STRICT-2: call void @__ubsan_handle_out_of_bounds_abort( |
| 41 | + return p->a[i] + (p->a)[i]; |
| 42 | +} |
0 commit comments