authorgravatar for inkryption07@gmail.comInKryption <inkryption07@gmail.com> 2023-03-16 01:02:10+01:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2023-03-16 13:00:36+02:00
log9964f1c160acd0bf994708333cd69bc070d6c77e
tree95038ad8fe5011ad7df08a567e8c3bc545c3532e
parente1e414e62a86cc460ef215ea8050c953b68b6080

Add error for bad cast from `*T` to `*[n]T`

Casting `*T` to `*[1]T` should still work, but every other length will now be a compiler error instead of a potential OOB access.

2 files changed, 19 insertions(+), 0 deletions(-)

src/Sema.zig+1
...@@ -24853,6 +24853,7 @@ fn coerceExtra(...@@ -24853,6 +24853,7 @@ fn coerceExtra(
24853 const array_ty = dest_info.pointee_type;24853 const array_ty = dest_info.pointee_type;
24854 if (array_ty.zigTypeTag() != .Array) break :single_item;24854 if (array_ty.zigTypeTag() != .Array) break :single_item;
24855 const array_elem_ty = array_ty.childType();24855 const array_elem_ty = array_ty.childType();
24856 if (array_ty.arrayLen() != 1) break :single_item;
24856 const dest_is_mut = dest_info.mutable;24857 const dest_is_mut = dest_info.mutable;
24857 switch (try sema.coerceInMemoryAllowed(block, array_elem_ty, ptr_elem_ty, dest_is_mut, target, dest_ty_src, inst_src)) {24858 switch (try sema.coerceInMemoryAllowed(block, array_elem_ty, ptr_elem_ty, dest_is_mut, target, dest_ty_src, inst_src)) {
24858 .ok => {},24859 .ok => {},
test/cases/compile_errors/attempted_implicit_cast_from_T_to_long_array_ptr.zig created+18
...@@ -0,0 +1,18 @@
1export fn entry0(single: *u32) void {
2 _ = @as(*const [0]u32, single);
3}
4export fn entry1(single: *u32) void {
5 _ = @as(*const [1]u32, single);
6}
7export fn entry2(single: *u32) void {
8 _ = @as(*const [2]u32, single);
9}
10
11// error
12// backend=stage2
13// target=native
14//
15// :2:28: error: expected type '*const [0]u32', found '*u32'
16// :2:28: note: pointer type child 'u32' cannot cast into pointer type child '[0]u32'
17// :8:28: error: expected type '*const [2]u32', found '*u32'
18// :8:28: note: pointer type child 'u32' cannot cast into pointer type child '[2]u32'