authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2019-11-04 15:09:11+01:00
committergravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2019-11-04 20:33:41+01:00
logc47211cc60a826837191fbb87b32f0baacb167cb
treea57056e838fabfb5ac51cffed26bee084402affa
parent2b4bf1e7ced151482ce741788eadcb39f6d60f72

Prevent crash when slicing undefined ptr to slice

Fixes #3534

2 files changed, 23 insertions(+), 5 deletions(-)

src/ir.cpp+13-5
...@@ -22949,12 +22949,20 @@ static IrInstruction *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstruction...@@ -22949,12 +22949,20 @@ static IrInstruction *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstruction
22949 if (parent_ptr == nullptr)22949 if (parent_ptr == nullptr)
22950 return ira->codegen->invalid_instruction;22950 return ira->codegen->invalid_instruction;
2295122951
22952 array_val = const_ptr_pointee(ira, ira->codegen, parent_ptr, instruction->base.source_node);
22953 if (array_val == nullptr)
22954 return ira->codegen->invalid_instruction;
2295522952
22956 rel_end = child_array_type->data.array.len;22953 if (parent_ptr->special == ConstValSpecialUndef) {
22957 abs_offset = 0;22954 array_val = nullptr;
22955 abs_offset = 0;
22956 rel_end = SIZE_MAX;
22957 ptr_is_undef = true;
22958 } else {
22959 array_val = const_ptr_pointee(ira, ira->codegen, parent_ptr, instruction->base.source_node);
22960 if (array_val == nullptr)
22961 return ira->codegen->invalid_instruction;
22962
22963 rel_end = child_array_type->data.array.len;
22964 abs_offset = 0;
22965 }
22958 } else {22966 } else {
22959 array_val = const_ptr_pointee(ira, ira->codegen, &ptr_ptr->value, instruction->base.source_node);22967 array_val = const_ptr_pointee(ira, ira->codegen, &ptr_ptr->value, instruction->base.source_node);
22960 if (array_val == nullptr)22968 if (array_val == nullptr)
test/compile_errors.zig+10
...@@ -2,6 +2,16 @@ const tests = @import("tests.zig");...@@ -2,6 +2,16 @@ const tests = @import("tests.zig");
2const builtin = @import("builtin");2const builtin = @import("builtin");
33
4pub fn addCases(cases: *tests.CompileErrorContext) void {4pub fn addCases(cases: *tests.CompileErrorContext) void {
5 cases.add(
6 "slicing of global undefined pointer",
7 \\var buf: *[1]u8 = undefined;
8 \\export fn entry() void {
9 \\ _ = buf[0..1];
10 \\}
11 ,
12 "tmp.zig:3:12: error: non-zero length slice of undefined pointer",
13 );
14
5 cases.add(15 cases.add(
6 "using invalid types in function call raises an error",16 "using invalid types in function call raises an error",
7 \\const MenuEffect = enum {};17 \\const MenuEffect = enum {};