authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2021-12-28 00:03:03+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-12-31 14:23:49-07:00
log9a0010b186a281740af812452514b260b6063e1a
tree3bf4ec21a7714f0b7c8295a9869eea962153b1a0
parentb86aadfa385ad2af8df601c0c69fa2e45eacd0ff

stage1: fix access of slice sentinel at comptime


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

src/stage1/ir.cpp+5-1
...@@ -14982,12 +14982,16 @@ static Stage1AirInst *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, Stage1ZirI...@@ -14982,12 +14982,16 @@ static Stage1AirInst *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, Stage1ZirI
14982 case ConstPtrSpecialSubArray:14982 case ConstPtrSpecialSubArray:
14983 case ConstPtrSpecialBaseArray:14983 case ConstPtrSpecialBaseArray:
14984 {14984 {
14985 uint64_t array_len = ptr_field->data.x_ptr.data.base_array.array_val->type->data.array.len;
14986 if (ptr_field->data.x_ptr.data.base_array.array_val->type->data.array.sentinel != nullptr) {
14987 array_len += 1;
14988 }
14985 size_t offset = ptr_field->data.x_ptr.data.base_array.elem_index;14989 size_t offset = ptr_field->data.x_ptr.data.base_array.elem_index;
14986 uint64_t new_index = offset + index;14990 uint64_t new_index = offset + index;
14987 if (ptr_field->data.x_ptr.data.base_array.array_val->data.x_array.special !=14991 if (ptr_field->data.x_ptr.data.base_array.array_val->data.x_array.special !=
14988 ConstArraySpecialBuf)14992 ConstArraySpecialBuf)
14989 {14993 {
14990 if (new_index >= ptr_field->data.x_ptr.data.base_array.array_val->type->data.array.len) {14994 if (new_index >= array_len) {
14991 ir_add_error_node(ira, elem_ptr_instruction->base.source_node, buf_sprintf("out of bounds slice"));14995 ir_add_error_node(ira, elem_ptr_instruction->base.source_node, buf_sprintf("out of bounds slice"));
14992 return ira->codegen->invalid_inst_gen;14996 return ira->codegen->invalid_inst_gen;
14993 }14997 }
test/behavior/slice_stage1.zig+18
...@@ -339,3 +339,21 @@ test "slice bounds in comptime concatenation" {...@@ -339,3 +339,21 @@ test "slice bounds in comptime concatenation" {
339 try expect(str2.len == 1);339 try expect(str2.len == 1);
340 try expect(std.mem.eql(u8, str2, "1"));340 try expect(std.mem.eql(u8, str2, "1"));
341}341}
342
343test "slice sentinel access at comptime" {
344 {
345 const str0 = &[_:0]u8{ '1', '2', '3' };
346 const slice0: [:0]const u8 = str0;
347
348 try expect(slice0.len == 3);
349 try expect(slice0[slice0.len] == 0);
350 }
351 {
352 const str0 = "123";
353 _ = &str0[0];
354 const slice0: [:0]const u8 = str0;
355
356 try expect(slice0.len == 3);
357 try expect(slice0[slice0.len] == 0);
358 }
359}