zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit 6a5463951f0aa11cbdd5575cc78e85cd2ed10b46 (tree)
parent 82c8e45a7e7659146b2ecda2929f01027b0658e4
Author: mlugg <mlugg@mlugg.co.uk>
Date:   Mon, 21 Aug 2023 02:07:40 +0100

Sema: disallow C pointer to slice coercion

Resolves: #16719

Diffstat:
Msrc/Sema.zig | 1+
Atest/cases/compile_errors/ptr_coerced_to_slice.zig | 20++++++++++++++++++++
2 files changed, 21 insertions(+), 0 deletions(-)

diff --git a/src/Sema.zig b/src/Sema.zig @@ -27289,6 +27289,7 @@ fn coerceExtra( // coercion from C pointer if (inst_ty.isCPtr(mod)) src_c_ptr: { + if (dest_info.flags.size == .Slice) break :src_c_ptr; if (!sema.checkPtrAttributes(dest_ty, inst_ty, &in_memory_result)) break :src_c_ptr; // In this case we must add a safety check because the C pointer // could be null. diff --git a/test/cases/compile_errors/ptr_coerced_to_slice.zig b/test/cases/compile_errors/ptr_coerced_to_slice.zig @@ -0,0 +1,20 @@ +export fn foo() void { + const ptr: [*]const u8 = "abc"; + _ = @as([]const u8, ptr); +} +export fn bar() void { + const ptr: [*c]const u8 = "def"; + _ = @as([]const u8, ptr); +} +export fn baz() void { + const ptr: *const u8 = &@as(u8, 123); + _ = @as([]const u8, ptr); +} + +// error +// backend=stage2 +// target=native +// +// :3:25: error: expected type '[]const u8', found '[*]const u8' +// :7:25: error: expected type '[]const u8', found '[*c]const u8' +// :11:25: error: expected type '[]const u8', found '*const u8'