LowerSwitch: Avoid some undefined behaviour
authorJustin Bogner <mail@justinbogner.com>
Sat, 20 Jun 2015 00:28:25 +0000 (00:28 +0000)
committerJustin Bogner <mail@justinbogner.com>
Sat, 20 Jun 2015 00:28:25 +0000 (00:28 +0000)
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.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@240201 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/Utils/LowerSwitch.cpp

index a057f5d0c0fadebbb915e1c1540b910f9103f872..b90a03c7976327035ef35a73733120d7068df91b 100644 (file)
@@ -374,7 +374,8 @@ unsigned LowerSwitch::Clusterify(CaseVector& Cases, SwitchInst *SI) {
 
       // 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 {