From 56b52dd0a357f87627fe96dc99377a397f3bb9a1 Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Mon, 28 Sep 2020 17:16:12 +0200 Subject: [PATCH] stage1: Detect OOB access of vector value Fixes #5710 --- src/ir.cpp | 10 ++++++++++ test/compile_errors.zig | 10 +++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/ir.cpp b/src/ir.cpp index 7d446c82a0..871c6739e6 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -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; diff --git a/test/compile_errors.zig b/test/compile_errors.zig index f457c74609..6ae857d1a8 100644 --- a/test/compile_errors.zig +++ b/test/compile_errors.zig @@ -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(){}; \\}