zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit 56b52dd0a357f87627fe96dc99377a397f3bb9a1 (tree)
parent 8794ce6f79886e5ebbf0476d56917e219b52c561
Author: LemonBoy <thatlemon@gmail.com>
Date:   Mon, 28 Sep 2020 17:16:12 +0200

stage1: Detect OOB access of vector value

Fixes #5710

Diffstat:
Msrc/ir.cpp | 10++++++++++
Mtest/compile_errors.zig | 10+++++++++-
2 files changed, 19 insertions(+), 1 deletion(-)

diff --git 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 @@ -4,6 +4,14 @@ 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 }; \\} , &[_][]const u8{ @@ -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(){}; \\}