From eac2bbfec9f99c27a886f4842cb9ca42584607f3 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Mon, 8 Sep 2025 18:18:27 -0700 Subject: [PATCH] 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 --- lib/std/Io/Writer.zig | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/std/Io/Writer.zig b/lib/std/Io/Writer.zig index 1c34a124ae..7f1b1fd884 100644 --- a/lib/std/Io/Writer.zig +++ b/lib/std/Io/Writer.zig @@ -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 {