authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-03-23 10:00:46+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-03-23 09:13:52-04:00
log0cd953d40ea0f6934a0900f7ad8593e5deaf6efd
treeb594359b1f26078553200d6510e983f3a859243a
parent13d04f9963be930360ab728edd47f1a6ecfb1777

ir: Prevent crash when slicing hardcoded pointer

Closes #4780

2 files changed, 15 insertions(+), 1 deletions(-)

src/ir.cpp+1-1
...@@ -12819,7 +12819,7 @@ static IrInstGen *ir_resolve_ptr_of_array_to_slice(IrAnalyze *ira, IrInst* sourc...@@ -12819,7 +12819,7 @@ static IrInstGen *ir_resolve_ptr_of_array_to_slice(IrAnalyze *ira, IrInst* sourc
12819 result->value->type = wanted_type;12819 result->value->type = wanted_type;
12820 return result;12820 return result;
12821 }12821 }
12822 } else {12822 } else if (array_ptr_val->data.x_ptr.special != ConstPtrSpecialHardCodedAddr) {
12823 ZigValue *pointee = const_ptr_pointee(ira, ira->codegen, array_ptr_val, source_instr->source_node);12823 ZigValue *pointee = const_ptr_pointee(ira, ira->codegen, array_ptr_val, source_instr->source_node);
12824 if (pointee == nullptr)12824 if (pointee == nullptr)
12825 return ira->codegen->invalid_inst_gen;12825 return ira->codegen->invalid_inst_gen;
test/stage1/behavior/slice.zig+14
...@@ -285,3 +285,17 @@ test "slice syntax resulting in pointer-to-array" {...@@ -285,3 +285,17 @@ test "slice syntax resulting in pointer-to-array" {
285 S.doTheTest();285 S.doTheTest();
286 comptime S.doTheTest();286 comptime S.doTheTest();
287}287}
288
289test "slice of hardcoded address to pointer" {
290 const S = struct {
291 fn doTheTest() void {
292 const pointer = @intToPtr([*]u8, 0x04)[0..2];
293 comptime expect(@TypeOf(pointer) == *[2]u8);
294 const slice: []const u8 = pointer;
295 expect(@ptrToInt(slice.ptr) == 4);
296 expect(slice.len == 2);
297 }
298 };
299
300 S.doTheTest();
301}