Skip to content

Commit a7202bd

Browse files
committed
Fix heap-use-after-free bug in expandSDiv when the operands are
constants, as discovered by ASAN. Patch by Mehdi Amini! llvm-svn: 221401
1 parent 43270c3 commit a7202bd

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

llvm/lib/Transforms/Utils/IntegerDivision.cpp

+10-6
Original file line numberDiff line numberDiff line change
@@ -398,11 +398,13 @@ bool llvm::expandRemainder(BinaryOperator *Rem) {
398398
Rem->dropAllReferences();
399399
Rem->eraseFromParent();
400400

401-
// If we didn't actually generate a udiv instruction, we're done
402-
BinaryOperator *BO = dyn_cast<BinaryOperator>(Builder.GetInsertPoint());
403-
if (!BO || BO->getOpcode() != Instruction::URem)
401+
// If we didn't actually generate an urem instruction, we're done
402+
// This happens for example if the input were constant. In this case the
403+
// Builder insertion point was unchanged
404+
if (Rem == Builder.GetInsertPoint())
404405
return true;
405406

407+
BinaryOperator *BO = dyn_cast<BinaryOperator>(Builder.GetInsertPoint());
406408
Rem = BO;
407409
}
408410

@@ -456,11 +458,13 @@ bool llvm::expandDivision(BinaryOperator *Div) {
456458
Div->dropAllReferences();
457459
Div->eraseFromParent();
458460

459-
// If we didn't actually generate a udiv instruction, we're done
460-
BinaryOperator *BO = dyn_cast<BinaryOperator>(Builder.GetInsertPoint());
461-
if (!BO || BO->getOpcode() != Instruction::UDiv)
461+
// If we didn't actually generate an udiv instruction, we're done
462+
// This happens for example if the input were constant. In this case the
463+
// Builder insertion point was unchanged
464+
if (Div == Builder.GetInsertPoint())
462465
return true;
463466

467+
BinaryOperator *BO = dyn_cast<BinaryOperator>(Builder.GetInsertPoint());
464468
Div = BO;
465469
}
466470

0 commit comments

Comments
 (0)