PR15338: Don't assert if -fsanitize=bounds sees array indexing on an incomplete
authorRichard Smith <richard-llvm@metafoo.co.uk>
Sun, 24 Feb 2013 01:56:24 +0000 (01:56 +0000)
committerRichard Smith <richard-llvm@metafoo.co.uk>
Sun, 24 Feb 2013 01:56:24 +0000 (01:56 +0000)
array type.

llvm-svn: 175982

clang/lib/CodeGen/CGExpr.cpp
clang/test/CodeGenCXX/catch-undef-behavior.cpp

index a688dbf..8566206 100644 (file)
@@ -677,7 +677,7 @@ llvm::Value *getArrayIndexingBound(CodeGenFunction &CGF, const Expr *Base,
       const ArrayType *AT = IndexedType->castAsArrayTypeUnsafe();
       if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
         return CGF.Builder.getInt(CAT->getSize());
-      else if (const VariableArrayType *VAT = cast<VariableArrayType>(AT))
+      else if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(AT))
         return CGF.getVLASize(VAT).first;
     }
   }
@@ -688,6 +688,8 @@ llvm::Value *getArrayIndexingBound(CodeGenFunction &CGF, const Expr *Base,
 void CodeGenFunction::EmitBoundsCheck(const Expr *E, const Expr *Base,
                                       llvm::Value *Index, QualType IndexType,
                                       bool Accessed) {
+  assert(SanOpts->Bounds && "should not be called unless adding bounds checks");
+
   QualType IndexedType;
   llvm::Value *Bound = getArrayIndexingBound(*this, Base, IndexedType);
   if (!Bound)
index 044b92b..31958a6 100644 (file)
@@ -292,6 +292,13 @@ int flex_array_index(ArrayMembers *p, int n) {
   return p->a2[n];
 }
 
+extern int incomplete[];
+// CHECK: @_Z22incomplete_array_index
+int incomplete_array_index(int n) {
+  // CHECK-NOT: call void @__ubsan_handle_out_of_bounds(
+  return incomplete[n];
+}
+
 typedef __attribute__((ext_vector_type(4))) int V4I;
 // CHECK: @_Z12vector_index
 int vector_index(V4I v, int n) {