commit cbc85f4516a5bd545ce365dedec19f6fcad47b58 (tree)
parent 75c33ba85e47eec9f7257cfb972a54b22a5283eb
Author: Cody Tapscott <topolarity@tapscott.me>
Date: Thu, 7 Jul 2022 11:21:39 -0700
stage1: Fix seg-fault when slicing string literal with sentinel
Diffstat:
3 files changed, 14 insertions(+), 0 deletions(-)
diff --git 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
@@ -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
@@ -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);
+}