Skip to content

Commit 33f0d45

Browse files
authored
std.elf: fix panic while parsing header
When parsing an invalid (e.g., corrupted) ELF header, `@enumFromInt` can panic casting the exhaustive enum `ET`.
1 parent 5c39ccd commit 33f0d45

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

lib/std/elf.zig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,8 @@ pub const ET = enum(u16) {
462462
/// Core file
463463
CORE = 4,
464464

465+
_,
466+
465467
/// Beginning of OS-specific codes
466468
pub const LOOS = 0xfe00;
467469

@@ -532,17 +534,21 @@ pub const Header = struct {
532534
};
533535
const need_bswap = endian != native_endian;
534536

537+
// Converting integers to exhaustive enums using `@enumFromInt` could cause a panic.
538+
comptime assert(!@typeInfo(OSABI).@"enum".is_exhaustive);
535539
const os_abi: OSABI = @enumFromInt(hdr32.e_ident[EI_OSABI]);
536540

537541
// The meaning of this value depends on `os_abi` so just make it available as `u8`.
538542
const abi_version = hdr32.e_ident[EI_ABIVERSION];
539543

540544
const @"type" = if (need_bswap) blk: {
545+
comptime assert(!@typeInfo(ET).@"enum".is_exhaustive);
541546
const value = @intFromEnum(hdr32.e_type);
542547
break :blk @as(ET, @enumFromInt(@byteSwap(value)));
543548
} else hdr32.e_type;
544549

545550
const machine = if (need_bswap) blk: {
551+
comptime assert(!@typeInfo(EM).@"enum".is_exhaustive);
546552
const value = @intFromEnum(hdr32.e_machine);
547553
break :blk @as(EM, @enumFromInt(@byteSwap(value)));
548554
} else hdr32.e_machine;

0 commit comments

Comments
 (0)