{"id":"d2de542036956761","repo":"pypa/pip","slug":"unrecognized-capacity-self-capacity-or-encodin","errorCode":null,"errorMessage":"unrecognized capacity ({self.capacity}) or encoding ({self.encoding})","messagePattern":"unrecognized capacity \\((.+?)\\) or encoding \\((.+?)\\)","errorType":"exception","errorClass":"ELFInvalid","httpStatus":null,"severity":"error","filePath":"src/pip/_vendor/packaging/_elffile.py","lineNumber":70,"sourceCode":"        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:\n            raise ELFInvalid(\n                f\"unrecognized capacity ({self.capacity}) or encoding ({self.encoding})\"\n            ) from e\n\n        try:\n            (\n                _,\n                self.machine,  # Architecture type.\n                _,\n                _,\n                self._e_phoff,  # Offset of program header.\n                _,\n                self.flags,  # Processor-specific flags.\n                _,\n                self._e_phentsize,  # Size of section.\n                self._e_phnum,  # Number of sections.\n            ) = self._read(e_fmt)\n        except struct.error as e:\n            raise ELFInvalid(\"unable to parse machine and section information\") from e","sourceCodeStart":52,"sourceCodeEnd":88,"githubUrl":"https://github.com/pypa/pip/blob/d7d0d0a39494e28ec1c407bd0680e4a4d1067791/src/pip/_vendor/packaging/_elffile.py#L52-L88","documentation":"Raised by `ELFFile.__init__` when the `(capacity, encoding)` pair from `e_ident` bytes 4 and 5 is not one of the four supported combinations (32/64-bit × LSB/MSB). The capacity field is the bitness (1=32, 2=64) and the encoding field is endianness (1=LSB/little, 2=MSB/big). Any other value means the file is corrupted or uses an exotic/unsupported ELF variant.","triggerScenarios":"Parsing a corrupted ELF whose header bytes 4-5 were clobbered; a fuzzed test artifact; a real ELF on an unusual architecture where the OS/ABI byte was misinterpreted. The supported set is intentionally minimal (the four mainstream combinations).","commonSituations":"A truncated/partially-written `.so` from a crashed compiler; byte-flip during download; test fixtures generated by truncating real binaries; rare big-endian embedded targets that use a non-1/2 encoding byte.","solutions":["Re-fetch or rebuild the binary — corrupted ELF headers are not recoverable","Validate bytes 4-5 fall in {(1,1),(1,2),(2,1),(2,2)} before constructing ELFFile","If scanning many files, treat ELFInvalid as 'skip this file'"],"exampleFix":"// before\nelf = ELFFile(f)\n// after\nf.seek(4)\nce, en = struct.unpack('BB', f.read(2))\nif (ce, en) not in {(1,1),(1,2),(2,1),(2,2)}:\n    raise ValueError(f'unsupported ELF class/endianness {(ce,en)}')\nf.seek(0)\nelf = ELFFile(f)","handlingStrategy":"try-catch","validationCode":"import struct\nSUPPORTED = {(1,1),(1,2),(2,1),(2,2)}\n\ndef elf_class_encoding(path: str):\n    with open(path,'rb') as f:\n        f.seek(4)\n        ce, en = struct.unpack('BB', f.read(2))\n    if (ce,en) not in SUPPORTED:\n        raise ValueError(f'unsupported ELF class/endianness {(ce,en)}')\n    return ce, en","typeGuard":"def is_supported_elf_variant(ce: int, en: int) -> bool:\n    return (ce, en) in {(1,1),(1,2),(2,1),(2,2)}","tryCatchPattern":"from packaging._elffile import ELFFile, ELFInvalid\ntry:\n    elf = ELFFile(f)\nexcept ELFInvalid as e:\n    if 'unrecognized capacity' in str(e):\n        log.warning('exotic ELF variant in %s, skipping', path)\n        continue\n    raise","preventionTips":["Treat a corrupted class/endianness byte as a corrupt file, re-fetch it","Skip files that fail ELFInvalid instead of crashing scanners","Validate bytes 4-5 before parsing in security-sensitive tooling"],"tags":["elf","packaging","binary-parsing","corruption"],"analyzedSha":"d7d0d0a39494e28ec1c407bd0680e4a4d1067791","analyzedAt":"2026-08-04T20:55:04.259Z","schemaVersion":2}