diff options
| author | Tim Northover <tnorthover@apple.com> | 2017-05-26 02:16:00 +0000 |
|---|---|---|
| committer | Tim Northover <tnorthover@apple.com> | 2017-05-26 02:16:00 +0000 |
| commit | 0150333a3c565fec97d4d65302abbb15ed21ecc4 (patch) | |
| tree | 88f48feb73042fa360b5e1c95f429cc519afbe6d | |
| parent | eb04c8cae2f0867c3959285a992f5319b8d6cfc7 (diff) | |
| download | bcm5719-llvm-0150333a3c565fec97d4d65302abbb15ed21ecc4.tar.gz bcm5719-llvm-0150333a3c565fec97d4d65302abbb15ed21ecc4.zip | |
Create valid LValue to represent null pointers in constant exprs
We were leaving the SubobjectDesignator in a surprising situation, where
it was allegedly valid but didn't actually refer to a type. This caused
a crash later on.
This patch fills out the SubobjectDesignator with the pointee type (as
happens in other evaluations of constant pointers) so that we don't
crash later.
llvm-svn: 303957
| -rw-r--r-- | clang/lib/AST/ExprConstant.cpp | 20 | ||||
| -rw-r--r-- | clang/test/SemaCXX/null-cast.cpp | 8 |
2 files changed, 22 insertions, 6 deletions
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp index bd8b3abd927..c19812e341c 100644 --- a/clang/lib/AST/ExprConstant.cpp +++ b/clang/lib/AST/ExprConstant.cpp @@ -1230,8 +1230,7 @@ namespace { IsNullPtr = V.isNullPointer(); } - void set(APValue::LValueBase B, unsigned I = 0, bool BInvalid = false, - bool IsNullPtr_ = false, uint64_t Offset_ = 0) { + void set(APValue::LValueBase B, unsigned I = 0, bool BInvalid = false) { #ifndef NDEBUG // We only allow a few types of invalid bases. Enforce that here. if (BInvalid) { @@ -1242,11 +1241,20 @@ namespace { #endif Base = B; - Offset = CharUnits::fromQuantity(Offset_); + Offset = CharUnits::fromQuantity(0); InvalidBase = BInvalid; CallIndex = I; Designator = SubobjectDesignator(getType(B)); - IsNullPtr = IsNullPtr_; + IsNullPtr = false; + } + + void setNull(QualType PointerTy, uint64_t TargetVal) { + Base = (Expr *)nullptr; + Offset = CharUnits::fromQuantity(TargetVal); + InvalidBase = false; + CallIndex = 0; + Designator = SubobjectDesignator(PointerTy->getPointeeType()); + IsNullPtr = true; } void setInvalid(APValue::LValueBase B, unsigned I = 0) { @@ -5494,8 +5502,8 @@ public: return true; } bool ZeroInitialization(const Expr *E) { - auto Offset = Info.Ctx.getTargetNullPointerValue(E->getType()); - Result.set((Expr*)nullptr, 0, false, true, Offset); + auto TargetVal = Info.Ctx.getTargetNullPointerValue(E->getType()); + Result.setNull(E->getType(), TargetVal); return true; } diff --git a/clang/test/SemaCXX/null-cast.cpp b/clang/test/SemaCXX/null-cast.cpp new file mode 100644 index 00000000000..c80ab8fced1 --- /dev/null +++ b/clang/test/SemaCXX/null-cast.cpp @@ -0,0 +1,8 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s + +struct A {}; +struct B : virtual A {}; + +void foo() { + (void)static_cast<A&>(*(B *)0); // expected-warning {{binding dereferenced null pointer to reference has undefined behavior}} +} |

