{"record":{"id":"ded2a0e8085cf87e","repo":"nodejs/node","slug":"invalid-magic-magic-r","errorCode":null,"errorMessage":"invalid magic: {magic!r}","messagePattern":"invalid magic: (.+?)","errorType":"validation","errorClass":"ELFInvalid","httpStatus":null,"severity":"error","filePath":"tools/gyp/pylib/packaging/_elffile.py","lineNumber":52,"sourceCode":"    Arm = 40\n    X8664 = 62\n    AArc64 = 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:\n            raise ELFInvalid(\"unable to parse identification\")\n        if (magic := bytes(ident[:4])) != 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 program header.\n            # p_fmt: Format for section 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:\n            raise ELFInvalid(\n                f\"unrecognized capacity ({self.capacity}) or \"\n                f\"encoding ({self.encoding})\"","sourceCodeStart":34,"sourceCodeEnd":70,"githubUrl":"https://github.com/nodejs/node/blob/1b2de5e052fc0fb95fd7fb6846dcec4ade598e9e/tools/gyp/pylib/packaging/_elffile.py#L34-L70","documentation":"Raised by ELFFile.__init__ when the first 4 bytes of the file are not the ELF magic bytes b'\\x7fELF'. The identification field was read successfully (at least 16 bytes) but the magic signature does not match, meaning the file is well-formed enough to read but is not an ELF executable.","triggerScenarios":"ELFFile(f) reads 16 bytes successfully, but bytes(ident[:4]) != b'\\x7fELF'. This happens with any non-ELF binary: a PE/COFF Windows exe (starts with 'MZ'), a Mach-O file (starts with 0xFEEDFACE/0xFEEDFAT), an ar archive, a plain object file in another format, or a text file at least 16 bytes long.","commonSituations":"Using ELFFile to inspect a wheel/platform tag on a file that is a Windows .exe or .dll, a macOS .dylib, or a static .a archive. Pointing at a non-binary file.","solutions":["Confirm the file is an ELF binary (Linux/BSD executable or shared object) before parsing.","Check the file with the `file` command: it should report 'ELF'.","Catch ELFInvalid and handle non-ELF files as a normal case (return None or skip).","If you expected an ELF file, the file may have been corrupted or replaced — re-fetch it."],"exampleFix":"# before\nelf = ELFFile(open('app.exe', 'rb'))  # 'MZ' magic -> raises\n# after\ntry:\n    elf = ELFFile(open(path, 'rb'))\nexcept ELFInvalid as e:\n    if 'magic' in str(e):\n        print('not an ELF binary; skipping')","handlingStrategy":"type-guard","validationCode":"# Check magic bytes before constructing ELFFile\nwith open(path, 'rb') as f:\n    if f.read(4) != b'\\x7fELF':\n        raise ValueError(f'{path} is not an ELF binary')\n    f.seek(0)","typeGuard":"def is_elf_file(path: str) -> bool:\n    try:\n        with open(path, 'rb') as f:\n            return f.read(4) == b'\\x7fELF'\n    except OSError:\n        return False","tryCatchPattern":"from packaging._elffile import ELFFile, ELFInvalid\ntry:\n    elf = ELFFile(open(path, 'rb'))\nexcept ELFInvalid as e:\n    if 'magic' in str(e):\n        print('not an ELF file')\n    raise","preventionTips":["Verify the file format with `file <path>` before ELF parsing.","Use a magic-byte type guard when scanning directories that may contain mixed binary formats."],"tags":["elf","binary","parsing","packaging"],"backgroundTag":null,"analyzedSha":"1b2de5e052fc0fb95fd7fb6846dcec4ade598e9e","analyzedAt":"2026-08-13T00:53:24.642Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}