Skip to content

Commit 101f977

Browse files
authored
[flang][CodeGen] Avoid out-of-bounds memory access in SelectCaseOp (#92955)
`SelectCaseOp::getCompareOperands` may return an empty range for the "default" case. Do not dereference the range until it is expected to be non-empty. This was detected by address-sanitizer.
1 parent eeb9fcd commit 101f977

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

flang/lib/Optimizer/CodeGen/CodeGen.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2981,39 +2981,40 @@ struct SelectCaseOpConversion : public fir::FIROpConversion<fir::SelectCaseOp> {
29812981
caseOp.getSuccessorOperands(adaptor.getOperands(), t);
29822982
std::optional<mlir::ValueRange> cmpOps =
29832983
*caseOp.getCompareOperands(adaptor.getOperands(), t);
2984-
mlir::Value caseArg = *(cmpOps.value().begin());
29852984
mlir::Attribute attr = cases[t];
2985+
assert(mlir::isa<mlir::UnitAttr>(attr) || cmpOps.has_value());
29862986
if (mlir::isa<fir::PointIntervalAttr>(attr)) {
29872987
auto cmp = rewriter.create<mlir::LLVM::ICmpOp>(
2988-
loc, mlir::LLVM::ICmpPredicate::eq, selector, caseArg);
2988+
loc, mlir::LLVM::ICmpPredicate::eq, selector, cmpOps->front());
29892989
genCaseLadderStep(loc, cmp, dest, destOps, rewriter);
29902990
continue;
29912991
}
29922992
if (mlir::isa<fir::LowerBoundAttr>(attr)) {
29932993
auto cmp = rewriter.create<mlir::LLVM::ICmpOp>(
2994-
loc, mlir::LLVM::ICmpPredicate::sle, caseArg, selector);
2994+
loc, mlir::LLVM::ICmpPredicate::sle, cmpOps->front(), selector);
29952995
genCaseLadderStep(loc, cmp, dest, destOps, rewriter);
29962996
continue;
29972997
}
29982998
if (mlir::isa<fir::UpperBoundAttr>(attr)) {
29992999
auto cmp = rewriter.create<mlir::LLVM::ICmpOp>(
3000-
loc, mlir::LLVM::ICmpPredicate::sle, selector, caseArg);
3000+
loc, mlir::LLVM::ICmpPredicate::sle, selector, cmpOps->front());
30013001
genCaseLadderStep(loc, cmp, dest, destOps, rewriter);
30023002
continue;
30033003
}
30043004
if (mlir::isa<fir::ClosedIntervalAttr>(attr)) {
3005-
auto cmp = rewriter.create<mlir::LLVM::ICmpOp>(
3006-
loc, mlir::LLVM::ICmpPredicate::sle, caseArg, selector);
3005+
mlir::Value caseArg0 = *cmpOps->begin();
3006+
auto cmp0 = rewriter.create<mlir::LLVM::ICmpOp>(
3007+
loc, mlir::LLVM::ICmpPredicate::sle, caseArg0, selector);
30073008
auto *thisBlock = rewriter.getInsertionBlock();
30083009
auto *newBlock1 = createBlock(rewriter, dest);
30093010
auto *newBlock2 = createBlock(rewriter, dest);
30103011
rewriter.setInsertionPointToEnd(thisBlock);
3011-
rewriter.create<mlir::LLVM::CondBrOp>(loc, cmp, newBlock1, newBlock2);
3012+
rewriter.create<mlir::LLVM::CondBrOp>(loc, cmp0, newBlock1, newBlock2);
30123013
rewriter.setInsertionPointToEnd(newBlock1);
3013-
mlir::Value caseArg0 = *(cmpOps.value().begin() + 1);
3014-
auto cmp0 = rewriter.create<mlir::LLVM::ICmpOp>(
3015-
loc, mlir::LLVM::ICmpPredicate::sle, selector, caseArg0);
3016-
genCondBrOp(loc, cmp0, dest, destOps, rewriter, newBlock2);
3014+
mlir::Value caseArg1 = *(cmpOps->begin() + 1);
3015+
auto cmp1 = rewriter.create<mlir::LLVM::ICmpOp>(
3016+
loc, mlir::LLVM::ICmpPredicate::sle, selector, caseArg1);
3017+
genCondBrOp(loc, cmp1, dest, destOps, rewriter, newBlock2);
30173018
rewriter.setInsertionPointToEnd(newBlock2);
30183019
continue;
30193020
}

0 commit comments

Comments
 (0)