From 66b2477ab668447c6cbff68770de14ec8db991be Mon Sep 17 00:00:00 2001 From: xackus <14938807+xackus@users.noreply.github.com> Date: Mon, 6 Apr 2020 09:44:06 +0200 Subject: [PATCH] fix lazy value in ir_analyze_instruction_elem_ptr --- src/ir.cpp | 6 +++++- test/stage1/behavior.zig | 1 + test/stage1/behavior/bugs/4954.zig | 8 ++++++++ 3 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 test/stage1/behavior/bugs/4954.zig diff --git a/src/ir.cpp b/src/ir.cpp index b570117177..bcadc89243 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -21149,7 +21149,11 @@ static IrInstGen *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstSrcElemP bool safety_check_on = elem_ptr_instruction->safety_check_on; if (instr_is_comptime(casted_elem_index)) { - uint64_t index = bigint_as_u64(&casted_elem_index->value->data.x_bigint); + ZigValue *index_val = ir_resolve_const(ira, casted_elem_index, UndefBad); + if (index_val == nullptr) + return ira->codegen->invalid_inst_gen; + uint64_t index = bigint_as_u64(&index_val->data.x_bigint); + if (array_type->id == ZigTypeIdArray) { uint64_t array_len = array_type->data.array.len + (array_type->data.array.sentinel != nullptr); diff --git a/test/stage1/behavior.zig b/test/stage1/behavior.zig index 52ce5979e1..61b0c1aa56 100644 --- a/test/stage1/behavior.zig +++ b/test/stage1/behavior.zig @@ -44,6 +44,7 @@ comptime { _ = @import("behavior/bugs/4769_a.zig"); _ = @import("behavior/bugs/4769_b.zig"); _ = @import("behavior/bugs/4769_c.zig"); + _ = @import("behavior/bugs/4954.zig"); _ = @import("behavior/bugs/394.zig"); _ = @import("behavior/bugs/421.zig"); _ = @import("behavior/bugs/529.zig"); diff --git a/test/stage1/behavior/bugs/4954.zig b/test/stage1/behavior/bugs/4954.zig new file mode 100644 index 0000000000..b5a9bdf851 --- /dev/null +++ b/test/stage1/behavior/bugs/4954.zig @@ -0,0 +1,8 @@ +fn f(buf: []u8) void { + var ptr = &buf[@sizeOf(u32)]; +} + +test "crash" { + var buf: [4096]u8 = undefined; + f(&buf); +}