[PATCH] D32455: detect integer overflow inside arms of conditional operator with non-constant expression

Nick Lewycky via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Apr 24 14:56:41 PDT 2017


nlewycky created this revision.
Herald added subscribers: rengolin, aemerson.

Descend into both the true and false expressions of a ConditionalOperator when the condition can't be evaluated and we're in an evaluation-mode that says we should continue evaluating.


https://reviews.llvm.org/D32455

Files:
  lib/AST/ExprConstant.cpp
  test/Sema/integer-overflow.c


Index: test/Sema/integer-overflow.c
===================================================================
--- test/Sema/integer-overflow.c
+++ test/Sema/integer-overflow.c
@@ -148,6 +148,9 @@
   a[4608 * 1024 * 1024] = 1i;
 
 // expected-warning at +1 2{{overflow in expression; result is 536870912 with type 'int'}}
+  (void)((i ? (4608 * 1024 * 1024) : (4608 * 1024 * 1024)) + 1);
+
+// expected-warning at +1 2{{overflow in expression; result is 536870912 with type 'int'}}
   return ((4608 * 1024 * 1024) + ((uint64_t)(4608 * 1024 * 1024)));
 }
 
Index: lib/AST/ExprConstant.cpp
===================================================================
--- lib/AST/ExprConstant.cpp
+++ lib/AST/ExprConstant.cpp
@@ -4418,8 +4418,14 @@
   bool HandleConditionalOperator(const ConditionalOperator *E) {
     bool BoolResult;
     if (!EvaluateAsBooleanCondition(E->getCond(), BoolResult, Info)) {
-      if (Info.checkingPotentialConstantExpression() && Info.noteFailure())
+      if (Info.checkingPotentialConstantExpression() && Info.noteFailure()) {
         CheckPotentialConstantConditional(E);
+        return false;
+      }
+      if (Info.noteFailure()) {
+        StmtVisitorTy::Visit(E->getTrueExpr());
+        StmtVisitorTy::Visit(E->getFalseExpr());
+      }
       return false;
     }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D32455.96474.patch
Type: text/x-patch
Size: 1290 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170424/c8460ce1/attachment.bin>


More information about the cfe-commits mailing list