{"record":{"id":"ae5ebfaf5ac71264","repo":"nodejs/node","slug":"unrecognized-capacity-self-capacity-or-encodin","errorCode":null,"errorMessage":"unrecognized capacity ({self.capacity}) or encoding ({self.encoding})","messagePattern":"unrecognized capacity \\((.+?)\\) or encoding \\((.+?)\\)","errorType":"validation","errorClass":"ELFInvalid","httpStatus":null,"severity":"error","filePath":"tools/gyp/pylib/packaging/_elffile.py","lineNumber":68,"sourceCode":"            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})\"\n            )\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:","sourceCodeStart":50,"sourceCodeEnd":86,"githubUrl":"https://github.com/nodejs/node/blob/1b2de5e052fc0fb95fd7fb6846dcec4ade598e9e/tools/gyp/pylib/packaging/_elffile.py#L50-L86","documentation":"Raised by ELFFile.__init__ when the capacity (EI_CLASS, byte index 4: 1=32-bit, 2=64-bit) and encoding (EI_DATA, byte index 5: 1=LSB/little-endian, 2=MSB/big-endian) pair is not one of the four supported combinations: (1,1), (1,2), (2,1), (2,2). A KeyError on the lookup dict is caught and re-raised as ELFInvalid with the unrecognized values.","triggerScenarios":"The ELF identification bytes 4 and 5 contain values outside {1,2}, or the magic is valid but EI_CLASS/EI_DATA are set to 0, 3, or any reserved/invalid value. This is rare for legitimate ELF files.","commonSituations":"A deliberately corrupted or fuzzed ELF file. A file that starts with the ELF magic but is not a real ELF (e.g., a test fixture or anti-analysis artifact). Future ELF class/encoding values not yet handled by this parser.","solutions":["Validate the ELF file is well-formed using readelf or objdump before parsing.","Catch ELFInvalid and report the file as unsupported rather than crashing.","If processing untrusted binaries, wrap ELFFile construction in a try/except and skip unsupported files.","Regenerate or re-fetch the binary if it should be a standard 32/64-bit ELF."],"exampleFix":"try:\n    elf = ELFFile(open(path, 'rb'))\nexcept ELFInvalid as e:\n    print(f'skipping {path}: {e}')\n    continue","handlingStrategy":"try-catch","validationCode":"# Check EI_CLASS and EI_DATA are valid before constructing ELFFile\nwith open(path, 'rb') as f:\n    ident = f.read(16)\nif ident[:4] != b'\\x7fELF' or ident[4] not in (1, 2) or ident[5] not in (1, 2):\n    raise ValueError('unsupported ELF class/encoding')","typeGuard":"def is_supported_elf(path: str) -> bool:\n    with open(path, 'rb') as f:\n        ident = f.read(16)\n    return (\n        len(ident) >= 6\n        and ident[:4] == b'\\x7fELF'\n        and ident[4] in (1, 2)\n        and ident[5] in (1, 2)\n    )","tryCatchPattern":"from packaging._elffile import ELFFile, ELFInvalid\ntry:\n    elf = ELFFile(open(path, 'rb'))\nexcept ELFInvalid as e:\n    print(f'unsupported ELF: {e}')\n    elf = None","preventionTips":["When processing untrusted binaries, always wrap ELFFile in try/except ELFInvalid.","Pre-validate the identification header bytes for capacity and encoding ranges."],"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"}