Index: lib/CodeGen/CGExpr.cpp =================================================================== --- lib/CodeGen/CGExpr.cpp +++ lib/CodeGen/CGExpr.cpp @@ -461,7 +461,7 @@ llvm::Value *Cond = nullptr; llvm::BasicBlock *Done = nullptr; - if (SanOpts->Null) { + if (SanOpts->Null || TCK == TCK_DowncastPointer) { // The glvalue must not be an empty glvalue. Cond = Builder.CreateICmpNE( Address, llvm::Constant::getNullValue(Address->getType())); Index: test/CodeGen/ubsan-vptr-null.cpp =================================================================== --- test/CodeGen/ubsan-vptr-null.cpp +++ test/CodeGen/ubsan-vptr-null.cpp @@ -0,0 +1,18 @@ +// Verify ubsan vptr skip null pointer value cases. +// RUN: %clang_cc1 -triple x86_64-linux-gnu -fsanitize=vptr -emit-llvm %s -o - | FileCheck %s + +class Bar { +public: + virtual ~Bar() {} +}; +class Foo : public Bar {}; + +// CHECK-LABEL: @_Z7checkmev +void checkme() { + // CHECK: [[CMP_RES:%.*]] = icmp ne %class.Foo* %{{[0-9]+}}, null + // CHECK: br {{.*}} [[CMP_RES]], label %[[NOT_NULLBB:.*]], label %[[NULLBB:.*]] + // CHECK: br label %[[NULLBB]] + Bar *bar = 0; + Foo* foo = static_cast(bar); // static_cast on the null pointer value. + return; +}