Commit 56b52dd0a3

LemonBoy <thatlemon@gmail.com>
2020-09-28 17:16:12
stage1: Detect OOB access of vector value
Fixes #5710
1 parent 8794ce6
Changed files (2)
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;
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(){};
         \\}