authorgravatar for 14938807+xackus@users.noreply.github.comxackus <14938807+xackus@users.noreply.github.com> 2020-04-23 03:06:41+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-04-23 12:45:32-04:00
loga9eb4a6740e0bb00e1984de3768a7de6001c25dd
tree5677ec8acfae34ec61eb74d775dc4289193c3224
parente6428f94013132b6a6284b053afb36d35af63c59

stage1: fix crash on accessing an array of size zero with runtime index


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,
38693869 assert(array_type->data.pointer.child_type->id == ZigTypeIdArray);
38703870 array_type = array_type->data.pointer.child_type;
38713871 }
3872
3873 assert(array_type->data.array.len != 0 || array_type->data.array.sentinel != nullptr);
3874
38723875 if (safety_check_on) {
38733876 uint64_t extra_len_from_sentinel = (array_type->data.array.sentinel != nullptr) ? 1 : 0;
38743877 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
2119921199 }
2120021200
2120121201 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
2120221207 ZigType *child_type = array_type->data.array.child_type;
2120321208 if (ptr_type->data.pointer.host_int_bytes == 0) {
2120421209 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 {
45524552 \\ const pointer = &array[0];
45534553 \\}
45544554 , &[_][]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",
45564566 });
45574567
45584568 cases.add("compile time division by zero",