Skip to content

Commit

Permalink
[test] Add -fsanitize=array-bounds test for pseudo flexible array member
Browse files Browse the repository at this point in the history
This behavior (from commit 539e4a7 in 2013) was untested.
The test can help detect regression introduced by 886715a
  • Loading branch information
MaskRay committed Jun 27, 2022
1 parent e06a88c commit efd90ff
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions clang/test/CodeGen/bounds-checking-fam.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// REQUIRES: x86-registered-target
// RUN: %clang_cc1 -emit-llvm -triple x86_64 -fsanitize=array-bounds %s -o - | FileCheck %s --check-prefixes=CHECK,CHECK-STRICT-0

/// Before flexible array member was added to C99, many projects use a
/// one-element array as the last emember of a structure as an alternative.
/// E.g. https://github.com/python/cpython/issues/84301
/// Suppress such errors by default.
struct One {
int a[1];
};
struct Two {
int a[2];
};
struct Three {
int a[3];
};

// CHECK-LABEL: define {{.*}} @test_one(
int test_one(struct One *p, int i) {
// CHECK-STRICT-0-NOT: @__ubsan
return p->a[i] + (p->a)[i];
}

// CHECK-LABEL: define {{.*}} @test_two(
int test_two(struct Two *p, int i) {
// CHECK-STRICT-0: call void @__ubsan_handle_out_of_bounds_abort(
return p->a[i] + (p->a)[i];
}

// CHECK-LABEL: define {{.*}} @test_three(
int test_three(struct Three *p, int i) {
// CHECK-STRICT-0: call void @__ubsan_handle_out_of_bounds_abort(
return p->a[i] + (p->a)[i];
}

0 comments on commit efd90ff

Please sign in to comment.