From 2847b2202978cbe5b4e1fb7799e71176410ccb40 Mon Sep 17 00:00:00 2001 From: Richard Smith Date: Sun, 24 Feb 2013 01:56:24 +0000 Subject: [PATCH] PR15338: Don't assert if -fsanitize=bounds sees array indexing on an incomplete array type. llvm-svn: 175982 --- clang/lib/CodeGen/CGExpr.cpp | 4 +++- clang/test/CodeGenCXX/catch-undef-behavior.cpp | 7 +++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp index a688dbf..8566206 100644 --- a/clang/lib/CodeGen/CGExpr.cpp +++ b/clang/lib/CodeGen/CGExpr.cpp @@ -677,7 +677,7 @@ llvm::Value *getArrayIndexingBound(CodeGenFunction &CGF, const Expr *Base, const ArrayType *AT = IndexedType->castAsArrayTypeUnsafe(); if (const ConstantArrayType *CAT = dyn_cast(AT)) return CGF.Builder.getInt(CAT->getSize()); - else if (const VariableArrayType *VAT = cast(AT)) + else if (const VariableArrayType *VAT = dyn_cast(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) diff --git a/clang/test/CodeGenCXX/catch-undef-behavior.cpp b/clang/test/CodeGenCXX/catch-undef-behavior.cpp index 044b92b..31958a6 100644 --- a/clang/test/CodeGenCXX/catch-undef-behavior.cpp +++ b/clang/test/CodeGenCXX/catch-undef-behavior.cpp @@ -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) { -- 2.7.4