diff options
| author | Rafael Batiati <rbatiati@gmail.com> | 2025-02-11 18:12:44 -0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-02-11 21:12:44 +0000 |
| commit | 33f0d458cf1ff8450297274f2e69afd64abbe816 (patch) | |
| tree | b4a36bbc3e5bbb5690cf93c4bb4a216cddb2a8e1 /lib/std/elf.zig | |
| parent | 5c39ccdddaa0a601e233aad9a146561bb8ac6b82 (diff) | |
| download | zig-33f0d458cf1ff8450297274f2e69afd64abbe816.tar.gz zig-33f0d458cf1ff8450297274f2e69afd64abbe816.zip | |
std.elf: fix panic while parsing header
When parsing an invalid (e.g., corrupted) ELF header, `@enumFromInt` can panic casting the exhaustive enum `ET`.
Diffstat (limited to 'lib/std/elf.zig')
| -rw-r--r-- | lib/std/elf.zig | 6 |
1 files changed, 6 insertions, 0 deletions
diff --git a/lib/std/elf.zig b/lib/std/elf.zig index f61114b53d..5ffcc629cc 100644 --- a/lib/std/elf.zig +++ b/lib/std/elf.zig @@ -462,6 +462,8 @@ pub const ET = enum(u16) { /// Core file CORE = 4, + _, + /// Beginning of OS-specific codes pub const LOOS = 0xfe00; @@ -532,17 +534,21 @@ pub const Header = struct { }; const need_bswap = endian != native_endian; + // Converting integers to exhaustive enums using `@enumFromInt` could cause a panic. + comptime assert(!@typeInfo(OSABI).@"enum".is_exhaustive); const os_abi: OSABI = @enumFromInt(hdr32.e_ident[EI_OSABI]); // The meaning of this value depends on `os_abi` so just make it available as `u8`. const abi_version = hdr32.e_ident[EI_ABIVERSION]; const @"type" = if (need_bswap) blk: { + comptime assert(!@typeInfo(ET).@"enum".is_exhaustive); const value = @intFromEnum(hdr32.e_type); break :blk @as(ET, @enumFromInt(@byteSwap(value))); } else hdr32.e_type; const machine = if (need_bswap) blk: { + comptime assert(!@typeInfo(EM).@"enum".is_exhaustive); const value = @intFromEnum(hdr32.e_machine); break :blk @as(EM, @enumFromInt(@byteSwap(value))); } else hdr32.e_machine; |
