Skip to content

Commit 56b52dd

Browse files
committed
stage1: Detect OOB access of vector value
Fixes #5710
1 parent 8794ce6 commit 56b52dd

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/ir.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21934,7 +21934,17 @@ static IrInstGen *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstSrcElemP
2193421934
return ira->codegen->invalid_inst_gen;
2193521935
}
2193621936
safety_check_on = false;
21937+
} else if (array_type->id == ZigTypeIdVector) {
21938+
uint64_t vector_len = array_type->data.vector.len;
21939+
if (index >= vector_len) {
21940+
ir_add_error_node(ira, elem_ptr_instruction->base.base.source_node,
21941+
buf_sprintf("index %" ZIG_PRI_u64 " outside vector of size %" ZIG_PRI_u64,
21942+
index, vector_len));
21943+
return ira->codegen->invalid_inst_gen;
21944+
}
21945+
safety_check_on = false;
2193721946
}
21947+
2193821948
if (array_type->id == ZigTypeIdVector) {
2193921949
ZigType *elem_type = array_type->data.vector.elem_type;
2194021950
uint32_t host_vec_len = array_type->data.vector.len;

test/compile_errors.zig

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@ const tests = @import("tests.zig");
22
const std = @import("std");
33

44
pub fn addCases(cases: *tests.CompileErrorContext) void {
5+
cases.add("slice sentinel mismatch",
6+
\\export fn entry() void {
7+
\\ const x = @import("std").meta.Vector(3, f32){ 25, 75, 5, 0 };
8+
\\}
9+
, &[_][]const u8{
10+
"tmp.zig:2:62: error: index 3 outside vector of size 3",
11+
});
12+
513
cases.add("slice sentinel mismatch",
614
\\export fn entry() void {
715
\\ const y: [:1]const u8 = &[_:2]u8{ 1, 2 };
@@ -7548,7 +7556,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
75487556
});
75497557

75507558
cases.add( // fixed bug #2032
7551-
"compile diagnostic string for top level decl type",
7559+
"compile diagnostic string for top level decl type",
75527560
\\export fn entry() void {
75537561
\\ var foo: u32 = @This(){};
75547562
\\}

0 commit comments

Comments
 (0)