From 0cd953d40ea0f6934a0900f7ad8593e5deaf6efd Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Mon, 23 Mar 2020 10:00:46 +0100 Subject: [PATCH] ir: Prevent crash when slicing hardcoded pointer Closes #4780 --- src/ir.cpp | 2 +- test/stage1/behavior/slice.zig | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/ir.cpp b/src/ir.cpp index b64128bef9..f0f0930762 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -12819,7 +12819,7 @@ static IrInstGen *ir_resolve_ptr_of_array_to_slice(IrAnalyze *ira, IrInst* sourc result->value->type = wanted_type; return result; } - } else { + } else if (array_ptr_val->data.x_ptr.special != ConstPtrSpecialHardCodedAddr) { ZigValue *pointee = const_ptr_pointee(ira, ira->codegen, array_ptr_val, source_instr->source_node); if (pointee == nullptr) return ira->codegen->invalid_inst_gen; diff --git a/test/stage1/behavior/slice.zig b/test/stage1/behavior/slice.zig index f7d6037a1f..e357ad2f0f 100644 --- a/test/stage1/behavior/slice.zig +++ b/test/stage1/behavior/slice.zig @@ -285,3 +285,17 @@ test "slice syntax resulting in pointer-to-array" { S.doTheTest(); comptime S.doTheTest(); } + +test "slice of hardcoded address to pointer" { + const S = struct { + fn doTheTest() void { + const pointer = @intToPtr([*]u8, 0x04)[0..2]; + comptime expect(@TypeOf(pointer) == *[2]u8); + const slice: []const u8 = pointer; + expect(@ptrToInt(slice.ptr) == 4); + expect(slice.len == 2); + } + }; + + S.doTheTest(); +}