[PATCH] D77374: Fix -fsanitize=array-bounds with comma operator

Vitaly Buka via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Apr 3 02:07:26 PDT 2020


vitalybuka created this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
vitalybuka added a reviewer: rsmith.
vitalybuka updated this revision to Diff 254728.
vitalybuka added a comment.
vitalybuka marked 2 inline comments as done.

remove debug dump



================
Comment at: clang/test/CodeGen/bounds-checking.c:116
+  // CHECK-NOT: @llvm.trap
+  return (s->t, s->a)[i];
+  // CHECK: }
----------------
C version works even without patch


Depends on D77373 <https://reviews.llvm.org/D77373>.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D77374

Files:
  clang/lib/CodeGen/CGExpr.cpp
  clang/test/CodeGen/bounds-checking.c
  clang/test/CodeGen/bounds-checking.cpp


Index: clang/test/CodeGen/bounds-checking.cpp
===================================================================
--- clang/test/CodeGen/bounds-checking.cpp
+++ clang/test/CodeGen/bounds-checking.cpp
@@ -98,3 +98,19 @@
   return s->a[i];
   // CHECK: }
 }
+
+// CHECK-LABEL: define {{.*}} @_Z10SFlexComma
+int SFlexComma(struct SFlex *s, int i) {
+  // a and b are treated as flexible array members.
+  // CHECK-NOT: @llvm.trap
+  return (s->t, s->a)[i];
+  // CHECK: }
+}
+
+// CHECK-LABEL: define {{.*}} @_Z7S1Comma
+int S1Comma(struct S1 *s, int i) {
+  // a and b are treated as flexible array members.
+  // CHECK-NOT: @llvm.trap
+  return ((s->t, (1, s->a)))[i];
+  // CHECK: }
+}
Index: clang/test/CodeGen/bounds-checking.c
===================================================================
--- clang/test/CodeGen/bounds-checking.c
+++ clang/test/CodeGen/bounds-checking.c
@@ -100,3 +100,19 @@
   return s->a[i];
   // CHECK: }
 }
+
+// CHECK-LABEL: define {{.*}} @SFlexComma
+int SFlexComma(struct SFlex *s, int i) {
+  // a and b are treated as flexible array members.
+  // CHECK-NOT: @llvm.trap
+  return (s->t, s->a)[i];
+  // CHECK: }
+}
+
+// CHECK-LABEL: define {{.*}} @S1Comma
+int S1Comma(struct S1 *s, int i) {
+  // a and b are treated as flexible array members.
+  // CHECK-NOT: @llvm.trap
+  return (s->t, s->a)[i];
+  // CHECK: }
+}
Index: clang/lib/CodeGen/CGExpr.cpp
===================================================================
--- clang/lib/CodeGen/CGExpr.cpp
+++ clang/lib/CodeGen/CGExpr.cpp
@@ -879,6 +879,13 @@
 
   E = E->IgnoreParens();
 
+  while (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
+    if (!BO->isCommaOp())
+      break;
+    E = BO->getRHS();
+    E = E->IgnoreParens();
+  }
+
   // A flexible array member must be the last member in the class.
   if (const auto *ME = dyn_cast<MemberExpr>(E)) {
     // FIXME: If the base type of the member expr is not FD->getParent(),


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D77374.254728.patch
Type: text/x-patch
Size: 1938 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200403/fe6b0373/attachment-0001.bin>


More information about the cfe-commits mailing list