Skip to content

Commit 732deb7

Browse files
nikicrlavaee
authored andcommitted
[InstCombine] Fix use after free
Load the nowrap flags before calling EmitGEPOffset(), as this may free the instruction.
1 parent e2363b0 commit 732deb7

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -711,9 +711,11 @@ Instruction *InstCombinerImpl::foldGEPICmp(GEPOperator *GEPLHS, Value *RHS,
711711
Value *PtrBase = GEPLHS->getOperand(0);
712712
if (PtrBase == RHS && CanFold(GEPLHS->getNoWrapFlags())) {
713713
// ((gep Ptr, OFFSET) cmp Ptr) ---> (OFFSET cmp 0).
714+
GEPNoWrapFlags NW = GEPLHS->getNoWrapFlags();
715+
// Do not access GEPLHS after EmitGEPOffset, as the instruction may be
716+
// destroyed.
714717
Value *Offset = EmitGEPOffset(GEPLHS, /*RewriteGEP=*/true);
715-
return NewICmp(GEPLHS->getNoWrapFlags(), Offset,
716-
Constant::getNullValue(Offset->getType()));
718+
return NewICmp(NW, Offset, Constant::getNullValue(Offset->getType()));
717719
}
718720

719721
if (GEPLHS->isInBounds() && ICmpInst::isEquality(Cond) &&

0 commit comments

Comments
 (0)