{"record":{"id":"d75645160272391b","repo":"nodejs/node","slug":"unable-to-parse-identification","errorCode":null,"errorMessage":"unable to parse identification","messagePattern":"unable to parse identification","errorType":"validation","errorClass":"ELFInvalid","httpStatus":null,"severity":"error","filePath":"tools/gyp/pylib/packaging/_elffile.py","lineNumber":50,"sourceCode":"    I386 = 3\n    S390 = 22\n    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(","sourceCodeStart":32,"sourceCodeEnd":68,"githubUrl":"https://github.com/nodejs/node/blob/1b2de5e052fc0fb95fd7fb6846dcec4ade598e9e/tools/gyp/pylib/packaging/_elffile.py#L32-L68","documentation":"Raised by ELFFile.__init__ when struct.unpack fails reading the 16-byte ELF identification field (e_ident). This means the file handle returned fewer than 16 bytes — the file is empty, truncated, or not seekable in the expected way. ELFInvalid is a ValueError subclass, so it can be caught as either.","triggerScenarios":"ELFFile(f) is constructed where f.read(16) returns fewer than 16 bytes, causing struct.unpack('16B', ...) to raise struct.error. The constructor catches it and re-raises as ELFInvalid.","commonSituations":"Passing a non-ELF file (text file, script, Mach-O binary, PE/COFF exe) to ELFFile. Passing an empty or truncated file. Passing a file opened in text mode instead of binary mode. A download or write that was interrupted mid-stream.","solutions":["Verify the file is actually an ELF binary before parsing (check it exists and has a reasonable size).","Open the file in binary mode: open(path, 'rb').","Check the file is not empty or truncated; re-download or rebuild if it is.","Catch ELFInvalid and fall back gracefully if the file might not be ELF."],"exampleFix":"# before\nELFFile(open('not-an-elf.txt'))  # raises ELFInvalid\n# after\nfrom packaging._elffile import ELFFile, ELFInvalid\ntry:\n    with open(path, 'rb') as f:\n        elf = ELFFile(f)\nexcept ELFInvalid:\n    print(f'{path} is not a valid ELF file')","handlingStrategy":"try-catch","validationCode":"# Check file size before parsing\nimport os\nif os.path.getsize(path) < 16:\n    raise ValueError(f'{path} is too small to be an ELF file')","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    with open(path, 'rb') as f:\n        elf = ELFFile(f)\nexcept ELFInvalid as e:\n    print(f'not a valid ELF: {e}')\n    elf = None","preventionTips":["Open files in binary mode ('rb') for ELFFile.","Pre-check file size and magic bytes before constructing ELFFile on untrusted input."],"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"}