Fix heap-use-after-free bug in expandSDiv when the operands are
authorMichael Ilseman <milseman@apple.com>
Wed, 5 Nov 2014 21:28:24 +0000 (21:28 +0000)
committerMichael Ilseman <milseman@apple.com>
Wed, 5 Nov 2014 21:28:24 +0000 (21:28 +0000)
constants, as discovered by ASAN.

Patch by Mehdi Amini!

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

lib/Transforms/Utils/IntegerDivision.cpp

index 9f91eeb79531dd6a8788475d502f8c26bf6aa6fe..0ae746cc83db7c133c5fbe1fe5a21d2cc6bcd7c0 100644 (file)
@@ -398,11 +398,13 @@ bool llvm::expandRemainder(BinaryOperator *Rem) {
     Rem->dropAllReferences();
     Rem->eraseFromParent();
 
-    // If we didn't actually generate a udiv instruction, we're done
-    BinaryOperator *BO = dyn_cast<BinaryOperator>(Builder.GetInsertPoint());
-    if (!BO || BO->getOpcode() != Instruction::URem)
+    // If we didn't actually generate an urem instruction, we're done
+    // This happens for example if the input were constant. In this case the
+    // Builder insertion point was unchanged
+    if (Rem == Builder.GetInsertPoint())
       return true;
 
+    BinaryOperator *BO = dyn_cast<BinaryOperator>(Builder.GetInsertPoint());
     Rem = BO;
   }
 
@@ -456,11 +458,13 @@ bool llvm::expandDivision(BinaryOperator *Div) {
     Div->dropAllReferences();
     Div->eraseFromParent();
 
-    // If we didn't actually generate a udiv instruction, we're done
-    BinaryOperator *BO = dyn_cast<BinaryOperator>(Builder.GetInsertPoint());
-    if (!BO || BO->getOpcode() != Instruction::UDiv)
+    // If we didn't actually generate an udiv instruction, we're done
+    // This happens for example if the input were constant. In this case the
+    // Builder insertion point was unchanged
+    if (Div == Builder.GetInsertPoint())
       return true;
 
+    BinaryOperator *BO = dyn_cast<BinaryOperator>(Builder.GetInsertPoint());
     Div = BO;
   }