[llvm] r240201 - LowerSwitch: Avoid some undefined behaviour

Justin Bogner mail at justinbogner.com
Fri Jun 19 17:28:25 PDT 2015


Author: bogner
Date: Fri Jun 19 19:28:25 2015
New Revision: 240201

URL: http://llvm.org/viewvc/llvm-project?rev=240201&view=rev
Log:
LowerSwitch: Avoid some undefined behaviour

When a case of INT64_MIN was followed by a case that was greater than
zero, we were overflowing a signed integer here. Since we've sorted
the cases here anyway (and thus currentValue must be greater than
nextValue) it's simple enough to avoid this by using addition rather
than subtraction.

Found by UBSAN on existing tests.

Modified:
    llvm/trunk/lib/Transforms/Utils/LowerSwitch.cpp

Modified: llvm/trunk/lib/Transforms/Utils/LowerSwitch.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/LowerSwitch.cpp?rev=240201&r1=240200&r2=240201&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Utils/LowerSwitch.cpp (original)
+++ llvm/trunk/lib/Transforms/Utils/LowerSwitch.cpp Fri Jun 19 19:28:25 2015
@@ -374,7 +374,8 @@ unsigned LowerSwitch::Clusterify(CaseVec
 
       // If the two neighboring cases go to the same destination, merge them
       // into a single case.
-      if ((nextValue-currentValue==1) && (currentBB == nextBB)) {
+      assert(nextValue > currentValue && "Cases should be strictly ascending");
+      if ((nextValue == currentValue + 1) && (currentBB == nextBB)) {
         I->High = J->High;
         J = Cases.erase(J);
       } else {





More information about the llvm-commits mailing list