{"id":"77c431fb89e453d2","repo":"pypa/pip","slug":"unable-to-parse-identification","errorCode":null,"errorMessage":"unable to parse identification","messagePattern":"unable to parse identification","errorType":"exception","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    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 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 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 as e:","sourceCodeStart":33,"sourceCodeEnd":69,"githubUrl":"https://github.com/pypa/pip/blob/d7d0d0a39494e28ec1c407bd0680e4a4d1067791/src/pip/_vendor/packaging/_elffile.py#L33-L69","documentation":"Raised by the vendored `packaging._elffile.ELFFile` constructor when `struct.unpack` fails reading the initial 16-byte `e_ident` header. It means the file object yielded fewer than 16 bytes (truncated, empty, or not seekable correctly), so the parser cannot even begin. The original `struct.error` is chained as the cause.","triggerScenarios":"Instantiating `ELFFile(f)` where `f` is empty, shorter than 16 bytes, a text-mode file, or a stream already at EOF. Also when the wrong file object is passed (e.g. a directory handle) or the path points to a 0-byte file.","commonSituations":"A build tool or manylinux/pip audit reading a `.so`/wheel payload that was incompletely downloaded; passing a non-ELF artifact (a shell script with no shebang magic, a Mach-O binary, a PE `.exe`) to `ELFFile`; reading from a pipe that closed early.","solutions":["Verify the file size is at least 16 bytes before constructing ELFFile: `if os.path.getsize(path) < 16: skip`","Guard construction with try/except ELFInvalid and skip the file","Confirm you actually intend to parse an ELF (Linux/Android binary), not Mach-O or PE"],"exampleFix":"// before\nelf = ELFFile(open(path, 'rb'))\n// after\nimport os\nif os.path.getsize(path) < 64:\n    raise ValueError(f'{path} too small to be ELF')\ntry:\n    elf = ELFFile(open(path, 'rb'))\nexcept ELFInvalid:\n    raise ValueError(f'{path} is not a valid ELF file')","handlingStrategy":"try-catch","validationCode":"import os\nfrom packaging._elffile import ELFFile, ELFInvalid\n\ndef open_elf(path: str):\n    if os.path.getsize(path) < 16:\n        raise ValueError(f'{path} too small to be ELF (<16 bytes)')\n    return ELFFile(open(path, 'rb'))","typeGuard":"def looks_like_elf(path: str) -> bool:\n    import os\n    return os.path.getsize(path) >= 16 and open(path,'rb').read(4) == b'\\x7fELF'","tryCatchPattern":"from packaging._elffile import ELFFile, ELFInvalid\ntry:\n    elf = ELFFile(f)\nexcept ELFInvalid as e:\n    log.warning('skipping %s: %s', path, e)\n    continue","preventionTips":["Check file size >= 16 bytes before parsing","Open files in binary mode ('rb')","Treat ELFInvalid as a skip signal in bulk scanners"],"tags":["elf","packaging","binary-parsing","input-validation"],"analyzedSha":"d7d0d0a39494e28ec1c407bd0680e4a4d1067791","analyzedAt":"2026-08-04T20:55:04.259Z","schemaVersion":2}