{"record":{"id":"77c431fb89e453d2","repo":"pypa/pip","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":"src/pip/_vendor/packaging/_elffile.py","lineNumber":51,"sourceCode":"    I386 = 3\n    S390 = 22\n    Arm = 40\n    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:","sourceCodeStart":33,"sourceCodeEnd":69,"githubUrl":"https://github.com/pypa/pip/blob/f399c3718970b1b0e2478dac5296eb62679a9b86/src/pip/_vendor/packaging/_elffile.py#L33-L69","documentation":"The ELFFile class from packaging._elffile parses ELF executables. The constructor reads the first 16 bytes (the e_ident identification array). If the underlying file is too short to provide 16 bytes, struct.unpack raises struct.error, which is caught and re-raised as ELFInvalid('unable to parse identification'). The file is not a valid ELF binary or is severely truncated.","triggerScenarios":"Constructing ELFFile(f) where f is a file object containing fewer than 16 bytes: an empty file, a text script, a truncated download, or a completely non-ELF binary format.","commonSituations":"pip uses this internally to find interpreter paths in wheel-installed scripts. Developers hit it when passing wrong file handles, using fixture files that are not real ELF binaries, or processing corrupted/incomplete downloads.","solutions":["Check the file is at least 64 bytes before constructing ELFFile","Verify the magic bytes b'\\x7fELF' at offset 0 before parsing","Catch ELFInvalid and skip or handle non-ELF files gracefully"],"exampleFix":"# before\nwith open(path, \"rb\") as f:\n    elf = ELFFile(f)  # raises on tiny/non-ELF file\n\n# after\nimport os\nfrom packaging._elffile import ELFFile, ELFInvalid\n\nif os.path.getsize(path) < 64:\n    raise ValueError(f\"{path} is too small to be an ELF file\")\nwith open(path, \"rb\") as f:\n    if f.read(4) != b\"\\x7fELF\":\n        raise ValueError(f\"{path} is not an ELF file\")\n    f.seek(0)\n    elf = ELFFile(f)","handlingStrategy":"try-catch","validationCode":"import os\n\ndef is_likely_elf(path):\n    if os.path.getsize(path) < 16:\n        return False\n    with open(path, \"rb\") as f:\n        return f.read(4) == b\"\\x7fELF\"","typeGuard":null,"tryCatchPattern":"from packaging._elffile import ELFFile, ELFInvalid\n\ntry:\n    with open(path, \"rb\") as f:\n        elf = ELFFile(f)\nexcept ELFInvalid:\n    elf = None  # not a valid ELF file, handle accordingly","preventionTips":["Validate file type before parsing","Check file size minimums (at least 64 bytes)","Always catch ELFInvalid when processing arbitrary binaries"],"tags":["elf","packaging","binary-parsing","file-validation","vendored"],"backgroundTag":null,"analyzedSha":"f399c3718970b1b0e2478dac5296eb62679a9b86","analyzedAt":"2026-08-08T23:01:42.227Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}