stage1: Detect OOB access of vector value

Fixes #5710
This commit is contained in:
LemonBoy 2020-09-28 17:16:12 +02:00
parent 8794ce6f79
commit 56b52dd0a3
2 changed files with 19 additions and 1 deletions

View File

@ -21934,7 +21934,17 @@ static IrInstGen *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstSrcElemP
return ira->codegen->invalid_inst_gen;
}
safety_check_on = false;
} else if (array_type->id == ZigTypeIdVector) {
uint64_t vector_len = array_type->data.vector.len;
if (index >= vector_len) {
ir_add_error_node(ira, elem_ptr_instruction->base.base.source_node,
buf_sprintf("index %" ZIG_PRI_u64 " outside vector of size %" ZIG_PRI_u64,
index, vector_len));
return ira->codegen->invalid_inst_gen;
}
safety_check_on = false;
}
if (array_type->id == ZigTypeIdVector) {
ZigType *elem_type = array_type->data.vector.elem_type;
uint32_t host_vec_len = array_type->data.vector.len;

View File

@ -2,6 +2,14 @@ const tests = @import("tests.zig");
const std = @import("std");
pub fn addCases(cases: *tests.CompileErrorContext) void {
cases.add("slice sentinel mismatch",
\\export fn entry() void {
\\ const x = @import("std").meta.Vector(3, f32){ 25, 75, 5, 0 };
\\}
, &[_][]const u8{
"tmp.zig:2:62: error: index 3 outside vector of size 3",
});
cases.add("slice sentinel mismatch",
\\export fn entry() void {
\\ const y: [:1]const u8 = &[_:2]u8{ 1, 2 };
@ -7548,7 +7556,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
});
cases.add( // fixed bug #2032
"compile diagnostic string for top level decl type",
"compile diagnostic string for top level decl type",
\\export fn entry() void {
\\ var foo: u32 = @This(){};
\\}