Skip to content

Commit 3d0b459

Browse files
committed
Check for index out of bounds
1 parent 6a21dc2 commit 3d0b459

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

clang/lib/CIR/Dialect/IR/CIRDialect.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1411,7 +1411,10 @@ OpFoldResult cir::VecExtractOp::fold(FoldAdaptor adaptor) {
14111411
return {};
14121412

14131413
const mlir::ArrayAttr elements = vectorAttr.getElts();
1414-
const int64_t index = indexAttr.getSInt();
1414+
const uint64_t index = indexAttr.getUInt();
1415+
if (index >= elements.size())
1416+
return {};
1417+
14151418
return elements[index];
14161419
}
14171420

clang/test/CIR/Transforms/vector-extract-fold.cir

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,21 @@ module {
1515
// CHECK: %[[INIT:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["e", init]
1616
// CHECK: %[[VALUE:.*]] = cir.const #cir.int<2> : !s32i
1717
// CHECK: cir.store %[[VALUE]], %[[INIT]] : !s32i, !cir.ptr<!s32i>
18+
19+
cir.func @fold_extract_vector_op_index_out_of_bounds_test() {
20+
%init = cir.alloca !s32i, !cir.ptr<!s32i>, ["e", init]
21+
%const_vec = cir.const #cir.const_vector<[#cir.int<1> : !s32i, #cir.int<2> : !s32i, #cir.int<3> : !s32i, #cir.int<4> : !s32i]> : !cir.vector<4 x !s32i>
22+
%index = cir.const #cir.int<9> : !s32i
23+
%ele = cir.vec.extract %const_vec[%index : !s32i] : !cir.vector<4 x !s32i>
24+
cir.store %ele, %init : !s32i, !cir.ptr<!s32i>
25+
cir.return
26+
}
27+
28+
// CHECK: %[[INIT:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["e", init]
29+
// CHECK: %[[CONST_VEC:.*]] = cir.const #cir.const_vector<[#cir.int<1> : !s32i, #cir.int<2> : !s32i, #cir.int<3> : !s32i, #cir.int<4> : !s32i]> : !cir.vector<4 x !s32i>
30+
// CHECK: %[[INDEX:.*]] = cir.const #cir.int<9> : !s32i
31+
// CHECK: %[[ELE:.*]] = cir.vec.extract %[[CONST_VEC]][%[[INDEX]] : !s32i] : !cir.vector<4 x !s32i>
32+
// CHECK: cir.store %[[ELE]], %[[INIT]] : !s32i, !cir.ptr<!s32i>
1833
}
1934

2035

0 commit comments

Comments
 (0)