authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-09-28 17:16:12+02:00
committergravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-09-28 17:16:12+02:00
log56b52dd0a357f87627fe96dc99377a397f3bb9a1
treee5f493a75bf845d7c88c5ebc6fe5025afe366d15
parent8794ce6f79886e5ebbf0476d56917e219b52c561

stage1: Detect OOB access of vector value

Fixes #5710

2 files changed, 19 insertions(+), 1 deletions(-)

src/ir.cpp+10
......@@ -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+9-1
......@@ -2,6 +2,14 @@ const tests = @import("tests.zig");
22const std = @import("std");
33
44pub 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 \\}