aboutsummaryrefslogtreecommitdiff
path: root/lib/AST/ExprConstant.cpp
diff options
context:
space:
mode:
authorEli Friedman <eli.friedman@gmail.com>2012-04-16 19:23:57 +0000
committerEli Friedman <eli.friedman@gmail.com>2012-04-16 19:23:57 +0000
commit2850376184b7e7aa81b5034ba44b001f8c55e07a (patch)
treec51e1f08f3dae46bbacd00330c6a909a035513bd /lib/AST/ExprConstant.cpp
parente9836a27cf93a58305ff3cf6d75ddc399c7d8ebf (diff)
Per Richard's comments on r154794, add the checks necessary to handle constant-folding relational comparisons safely in case the user is using -fwrapv or equivalent.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@154849 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/AST/ExprConstant.cpp')
-rw-r--r--lib/AST/ExprConstant.cpp15
1 files changed, 13 insertions, 2 deletions
diff --git a/lib/AST/ExprConstant.cpp b/lib/AST/ExprConstant.cpp
index 4839c29976..66a88b065c 100644
--- a/lib/AST/ExprConstant.cpp
+++ b/lib/AST/ExprConstant.cpp
@@ -5090,8 +5090,6 @@ bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
// The comparison here must be unsigned, and performed with the same
// width as the pointer.
- // FIXME: Knowing the base is the same for the LHS and RHS isn't enough
- // for relational operators.
unsigned PtrSize = Info.Ctx.getTypeSize(LHSTy);
uint64_t CompareLHS = LHSOffset.getQuantity();
uint64_t CompareRHS = RHSOffset.getQuantity();
@@ -5100,6 +5098,19 @@ bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
CompareLHS &= Mask;
CompareRHS &= Mask;
+ // If there is a base and this is a relational operator, we can only
+ // compare pointers within the object in question; otherwise, the result
+ // depends on where the object is located in memory.
+ if (!LHSValue.Base.isNull() && E->isRelationalOp()) {
+ QualType BaseTy = getType(LHSValue.Base);
+ if (BaseTy->isIncompleteType())
+ return Error(E);
+ CharUnits Size = Info.Ctx.getTypeSizeInChars(BaseTy);
+ uint64_t OffsetLimit = Size.getQuantity();
+ if (CompareLHS > OffsetLimit || CompareRHS > OffsetLimit)
+ return Error(E);
+ }
+
switch (E->getOpcode()) {
default: llvm_unreachable("missing comparison operator");
case BO_LT: return Success(CompareLHS < CompareRHS, E);