Skip to content

Commit 8db94ce

Browse files
committed
std.mem.asBytes: fix footgun when passing non-single pointer
I was just bitten by this footgun, where I actually wanted `sliceAsBytes` but unintentionally used `asBytes`, which in practice ignored all but the first element. Just add a comptime assertion to trigger a compile error in this case.
1 parent 43f64d7 commit 8db94ce

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

lib/std/mem.zig

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3965,7 +3965,9 @@ fn CopyPtrAttrs(
39653965
}
39663966

39673967
fn AsBytesReturnType(comptime P: type) type {
3968-
const size = @sizeOf(std.meta.Child(P));
3968+
const pointer = @typeInfo(P).pointer;
3969+
assert(pointer.size == .One);
3970+
const size = @sizeOf(pointer.child);
39693971
return CopyPtrAttrs(P, .One, [size]u8);
39703972
}
39713973

0 commit comments

Comments
 (0)