diff options
| author | Richard Smith <richard-llvm@metafoo.co.uk> | 2018-09-13 22:47:33 +0000 |
|---|---|---|
| committer | Richard Smith <richard-llvm@metafoo.co.uk> | 2018-09-13 22:47:33 +0000 |
| commit | 128719c4fe7c3bc1f4beccb82a8636c375033f5a (patch) | |
| tree | 96f1e1cb7261af36a66b840f613882def8912dfe /clang/lib/AST | |
| parent | 11ca38f4212646e0998ecc10db227df8ce1e6348 (diff) | |
| download | bcm5719-llvm-128719c4fe7c3bc1f4beccb82a8636c375033f5a.tar.gz bcm5719-llvm-128719c4fe7c3bc1f4beccb82a8636c375033f5a.zip | |
Fix crash on call to __builtin_memcpy with a null pointer to an
incomplete type.
Also improve the diagnostics for similar situations.
llvm-svn: 342192
Diffstat (limited to 'clang/lib/AST')
| -rw-r--r-- | clang/lib/AST/APValue.cpp | 20 | ||||
| -rw-r--r-- | clang/lib/AST/ExprConstant.cpp | 18 |
2 files changed, 30 insertions, 8 deletions
diff --git a/clang/lib/AST/APValue.cpp b/clang/lib/AST/APValue.cpp index c45b52a65a4..c05b160b8e3 100644 --- a/clang/lib/AST/APValue.cpp +++ b/clang/lib/AST/APValue.cpp @@ -416,18 +416,26 @@ void APValue::printPretty(raw_ostream &Out, ASTContext &Ctx, QualType Ty) const{ << GetApproxValue(getComplexFloatImag()) << "i"; return; case APValue::LValue: { - LValueBase Base = getLValueBase(); - if (!Base) { - Out << "0"; - return; - } - bool IsReference = Ty->isReferenceType(); QualType InnerTy = IsReference ? Ty.getNonReferenceType() : Ty->getPointeeType(); if (InnerTy.isNull()) InnerTy = Ty; + LValueBase Base = getLValueBase(); + if (!Base) { + if (isNullPointer()) { + Out << (Ctx.getLangOpts().CPlusPlus11 ? "nullptr" : "0"); + } else if (IsReference) { + Out << "*(" << InnerTy.stream(Ctx.getPrintingPolicy()) << "*)" + << getLValueOffset().getQuantity(); + } else { + Out << "(" << Ty.stream(Ctx.getPrintingPolicy()) << ")" + << getLValueOffset().getQuantity(); + } + return; + } + if (!hasLValuePath()) { // No lvalue path: just print the offset. CharUnits O = getLValueOffset(); diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp index f99d7841f39..c0d0e453fc8 100644 --- a/clang/lib/AST/ExprConstant.cpp +++ b/clang/lib/AST/ExprConstant.cpp @@ -6191,12 +6191,12 @@ bool PointerExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E, BuiltinOp == Builtin::BI__builtin_wmemmove; // The result of mem* is the first argument. - if (!Visit(E->getArg(0)) || Result.Designator.Invalid) + if (!Visit(E->getArg(0))) return false; LValue Dest = Result; LValue Src; - if (!EvaluatePointer(E->getArg(1), Src, Info) || Src.Designator.Invalid) + if (!EvaluatePointer(E->getArg(1), Src, Info)) return false; APSInt N; @@ -6209,6 +6209,20 @@ bool PointerExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E, if (!N) return true; + // Otherwise, if either of the operands is null, we can't proceed. Don't + // try to determine the type of the copied objects, because there aren't + // any. + if (!Src.Base || !Dest.Base) { + APValue Val; + (!Src.Base ? Src : Dest).moveInto(Val); + Info.FFDiag(E, diag::note_constexpr_memcpy_null) + << Move << WChar << !!Src.Base + << Val.getAsString(Info.Ctx, E->getArg(0)->getType()); + return false; + } + if (Src.Designator.Invalid || Dest.Designator.Invalid) + return false; + // We require that Src and Dest are both pointers to arrays of // trivially-copyable type. (For the wide version, the designator will be // invalid if the designated object is not a wchar_t.) |

