[clang] efd90ff - [test] Add -fsanitize=array-bounds test for pseudo flexible array member

Fangrui Song via cfe-commits cfe-commits at lists.llvm.org
Mon Jun 27 16:40:57 PDT 2022


Author: Fangrui Song
Date: 2022-06-27T16:40:52-07:00
New Revision: efd90ffbfc427ad4c4675ac1fcae9d53cc7f1322

URL: https://github.com/llvm/llvm-project/commit/efd90ffbfc427ad4c4675ac1fcae9d53cc7f1322
DIFF: https://github.com/llvm/llvm-project/commit/efd90ffbfc427ad4c4675ac1fcae9d53cc7f1322.diff

LOG: [test] Add -fsanitize=array-bounds test for pseudo flexible array member

This behavior (from commit 539e4a77bbabbc19f22b2bd24e04af2e432e599d in 2013) was untested.
The test can help detect regression introduced by 886715af962de2c92fac4bd37104450345711e4a

Added: 
    clang/test/CodeGen/bounds-checking-fam.c

Modified: 
    

Removed: 
    


################################################################################
diff  --git a/clang/test/CodeGen/bounds-checking-fam.c b/clang/test/CodeGen/bounds-checking-fam.c
new file mode 100644
index 000000000000..8da580fe0646
--- /dev/null
+++ b/clang/test/CodeGen/bounds-checking-fam.c
@@ -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];
+}


        


More information about the cfe-commits mailing list