| author | |
| committer | |
| log | a9eb4a6740e0bb00e1984de3768a7de6001c25dd |
| tree | 5677ec8acfae34ec61eb74d775dc4289193c3224 |
| parent | e6428f94013132b6a6284b053afb36d35af63c59 |
3 files changed, 19 insertions(+), 1 deletions(-)
src/codegen.cpp+3| ... | ... | @@ -3869,6 +3869,9 @@ static LLVMValueRef ir_render_elem_ptr(CodeGen *g, IrExecutableGen *executable, |
| 3869 | 3869 | assert(array_type->data.pointer.child_type->id == ZigTypeIdArray); |
| 3870 | 3870 | array_type = array_type->data.pointer.child_type; |
| 3871 | 3871 | } |
| 3872 | ||
| 3873 | assert(array_type->data.array.len != 0 || array_type->data.array.sentinel != nullptr); | |
| 3874 | ||
| 3872 | 3875 | if (safety_check_on) { |
| 3873 | 3876 | uint64_t extra_len_from_sentinel = (array_type->data.array.sentinel != nullptr) ? 1 : 0; |
| 3874 | 3877 | uint64_t full_len = array_type->data.array.len + extra_len_from_sentinel; |
src/ir.cpp+5| ... | ... | @@ -21199,6 +21199,11 @@ static IrInstGen *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstSrcElemP |
| 21199 | 21199 | } |
| 21200 | 21200 | |
| 21201 | 21201 | if (array_type->id == ZigTypeIdArray) { |
| 21202 | if(array_type->data.array.len == 0 && array_type->data.array.sentinel == nullptr){ | |
| 21203 | ir_add_error(ira, &elem_ptr_instruction->base.base, buf_sprintf("accessing a zero length array is not allowed")); | |
| 21204 | return ira->codegen->invalid_inst_gen; | |
| 21205 | } | |
| 21206 | ||
| 21202 | 21207 | ZigType *child_type = array_type->data.array.child_type; |
| 21203 | 21208 | if (ptr_type->data.pointer.host_int_bytes == 0) { |
| 21204 | 21209 | return_type = get_pointer_to_type_extra(ira->codegen, child_type, |
test/compile_errors.zig+11-1| ... | ... | @@ -4552,7 +4552,17 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { |
| 4552 | 4552 | \\ const pointer = &array[0]; |
| 4553 | 4553 | \\} |
| 4554 | 4554 | , &[_][]const u8{ |
| 4555 | "tmp.zig:3:27: error: index 0 outside array of size 0", | |
| 4555 | "tmp.zig:3:27: error: accessing a zero length array is not allowed", | |
| 4556 | }); | |
| 4557 | ||
| 4558 | cases.add("indexing an array of size zero with runtime index", | |
| 4559 | \\const array = [_]u8{}; | |
| 4560 | \\export fn foo() void { | |
| 4561 | \\ var index: usize = 0; | |
| 4562 | \\ const pointer = &array[index]; | |
| 4563 | \\} | |
| 4564 | , &[_][]const u8{ | |
| 4565 | "tmp.zig:4:27: error: accessing a zero length array is not allowed", | |
| 4556 | 4566 | }); |
| 4557 | 4567 | |
| 4558 | 4568 | cases.add("compile time division by zero", |