std.Io.Writer.writeSliceEndian: add compile error

check when an auto-layout struct is attempted to be memory reinterpreted
and written out. it would be writing undefined memory
This commit is contained in:
Andrew Kelley 2025-09-08 18:18:27 -07:00
parent 1872c85ac2
commit eac2bbfec9

View File

@ -865,6 +865,11 @@ pub inline fn writeSliceEndian(
slice: []const Elem,
endian: std.builtin.Endian,
) Error!void {
switch (@typeInfo(Elem)) {
.@"struct" => |info| comptime assert(info.layout != .auto),
.int, .@"enum" => {},
else => @compileError("ill-defined memory layout"),
}
if (native_endian == endian) {
return writeAll(w, @ptrCast(slice));
} else {