{"record":{"id":"462ab93f46beca78","repo":"pypa/pip","slug":"invalid-magic-magic-r","errorCode":null,"errorMessage":"invalid magic: {magic!r}","messagePattern":"invalid magic: (.+?)","errorType":"validation","errorClass":"ELFInvalid","httpStatus":null,"severity":"error","filePath":"src/pip/_vendor/packaging/_elffile.py","lineNumber":54,"sourceCode":"    X8664 = 62\n    AArch64 = 183\n\n\nclass ELFFile:\n    \"\"\"\n    Representation of an ELF executable.\n    \"\"\"\n\n    def __init__(self, f: IO[bytes]) -> None:\n        self._f = f\n\n        try:\n            ident = self._read(\"16B\")\n        except struct.error as e:\n            raise ELFInvalid(\"unable to parse identification\") from e\n        magic = bytes(ident[:4])\n        if magic != b\"\\x7fELF\":\n            raise ELFInvalid(f\"invalid magic: {magic!r}\")\n\n        self.capacity = ident[4]  # Format for program header (bitness).\n        self.encoding = ident[5]  # Data structure encoding (endianness).\n\n        try:\n            # e_fmt: Format for the ELF header.\n            # p_fmt: Format for a program header.\n            # p_idx: Indexes to find p_type, p_offset, and p_filesz.\n            e_fmt, self._p_fmt, self._p_idx = {\n                (1, 1): (\"<HHIIIIIHHH\", \"<IIIIIIII\", (0, 1, 4)),  # 32-bit LSB.\n                (1, 2): (\">HHIIIIIHHH\", \">IIIIIIII\", (0, 1, 4)),  # 32-bit MSB.\n                (2, 1): (\"<HHIQQQIHHH\", \"<IIQQQQQQ\", (0, 2, 5)),  # 64-bit LSB.\n                (2, 2): (\">HHIQQQIHHH\", \">IIQQQQQQ\", (0, 2, 5)),  # 64-bit MSB.\n            }[(self.capacity, self.encoding)]\n        except KeyError as e:\n            raise ELFInvalid(\n                f\"unrecognized capacity ({self.capacity}) or encoding ({self.encoding})\"\n            ) from e","sourceCodeStart":36,"sourceCodeEnd":72,"githubUrl":"https://github.com/pypa/pip/blob/f399c3718970b1b0e2478dac5296eb62679a9b86/src/pip/_vendor/packaging/_elffile.py#L36-L72","documentation":"After reading 16 identification bytes, ELFFile checks that the first 4 bytes equal b'\\x7fELF' (the canonical ELF magic number). If they differ, the file is definitively not an ELF binary and ELFInvalid is raised with the actual magic bytes shown. This distinguishes a structurally intact but wrong-format file from a truncated one.","triggerScenarios":"Passing any file that is >=16 bytes whose first 4 bytes are not b'\\x7fELF': Windows PE executables (.exe/.dll), macOS Mach-O binaries, Java class files, scripts, or arbitrary data blobs.","commonSituations":"Cross-platform builds where Windows or macOS binaries are mistakenly fed to ELF parsing code. Processing mixed-architecture artifact directories. Downloaded files that are actually HTML error pages or checksum files.","solutions":["Check the first 4 bytes match b'\\x7fELF' before constructing ELFFile","Route binaries to the correct parser based on their magic bytes (MZ for PE, \\xfe\\xed\\xfa for Mach-O)","Catch ELFInvalid and skip non-ELF files in batch processing"],"exampleFix":"# before\nelf = ELFFile(open(binary_path, \"rb\"))  # crashes on .exe / Mach-O files\n\n# after\nwith open(binary_path, \"rb\") as f:\n    magic = f.read(4)\n    f.seek(0)\n    if magic != b\"\\x7fELF\":\n        print(f\"Skipping non-ELF file: {binary_path}\")\n        continue\n    elf = ELFFile(f)","handlingStrategy":"validation","validationCode":"def validate_elf_magic(path):\n    with open(path, \"rb\") as f:\n        magic = f.read(4)\n    if magic != b\"\\x7fELF\":\n        raise ValueError(f\"Not an ELF file: magic bytes are {magic!r}\")","typeGuard":null,"tryCatchPattern":"from packaging._elffile import ELFFile, ELFInvalid\n\ntry:\n    with open(path, \"rb\") as f:\n        elf = ELFFile(f)\nexcept ELFInvalid as e:\n    if \"invalid magic\" in str(e):\n        # not an ELF binary; try PE, Mach-O, or skip\n        elf = None","preventionTips":["Check magic bytes before parsing","Route binaries to the correct parser based on format","Log file types when batch-processing binaries"],"tags":["elf","packaging","binary-parsing","file-format","vendored"],"backgroundTag":null,"analyzedSha":"f399c3718970b1b0e2478dac5296eb62679a9b86","analyzedAt":"2026-08-08T23:01:42.227Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}