From cbc85f4516a5bd545ce365dedec19f6fcad47b58 Mon Sep 17 00:00:00 2001 From: Cody Tapscott Date: Thu, 7 Jul 2022 11:21:39 -0700 Subject: [PATCH] stage1: Fix seg-fault when slicing string literal with sentinel --- src/stage1/ir.cpp | 1 + test/behavior.zig | 1 + test/behavior/bugs/12033.zig | 12 ++++++++++++ 3 files changed, 14 insertions(+) create mode 100644 test/behavior/bugs/12033.zig diff --git a/src/stage1/ir.cpp b/src/stage1/ir.cpp index c26a65aac2..5a3952dc67 100644 --- a/src/stage1/ir.cpp +++ b/src/stage1/ir.cpp @@ -21575,6 +21575,7 @@ done_with_return_type: // handle `[N]T` target_len = target->type->data.array.len; target_sentinel = target->type->data.array.sentinel; + expand_undef_array(ira->codegen, target); target_elements = target->data.x_array.data.s_none.elements; break; } else if (target->type->id == ZigTypeIdPointer && target->type->data.pointer.child_type->id == ZigTypeIdArray) { diff --git a/test/behavior.zig b/test/behavior.zig index 087b821c7d..7d87f01dbc 100644 --- a/test/behavior.zig +++ b/test/behavior.zig @@ -83,6 +83,7 @@ test { _ = @import("behavior/bugs/11181.zig"); _ = @import("behavior/bugs/11213.zig"); _ = @import("behavior/bugs/12003.zig"); + _ = @import("behavior/bugs/12033.zig"); _ = @import("behavior/byteswap.zig"); _ = @import("behavior/byval_arg_var.zig"); _ = @import("behavior/call.zig"); diff --git a/test/behavior/bugs/12033.zig b/test/behavior/bugs/12033.zig new file mode 100644 index 0000000000..563ed8e79b --- /dev/null +++ b/test/behavior/bugs/12033.zig @@ -0,0 +1,12 @@ +const std = @import("std"); + +test { + const string = "Hello!\x00World!"; + try std.testing.expect(@TypeOf(string) == *const [13:0]u8); + + const slice_without_sentinel: []const u8 = string[0..6]; + try std.testing.expect(@TypeOf(slice_without_sentinel) == []const u8); + + const slice_with_sentinel: [:0]const u8 = string[0..6 :0]; + try std.testing.expect(@TypeOf(slice_with_sentinel) == [:0]const u8); +}