{"record":{"id":"0e28adafa84be0f9","repo":"pypa/pip","slug":"typecode-should-be-0-127","errorCode":null,"errorMessage":"typecode should be 0-127","messagePattern":"typecode should be 0-127","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/pip/_vendor/msgpack/fallback.py","lineNumber":846,"sourceCode":"        if self._autoreset:\n            ret = self._buffer.getvalue()\n            self._buffer = BytesIO()\n            return ret\n\n    def pack_map_header(self, n):\n        if n >= 2**32:\n            raise ValueError\n        self._pack_map_header(n)\n        if self._autoreset:\n            ret = self._buffer.getvalue()\n            self._buffer = BytesIO()\n            return ret\n\n    def pack_ext_type(self, typecode, data):\n        if not isinstance(typecode, int):\n            raise TypeError(\"typecode must have int type.\")\n        if not 0 <= typecode <= 127:\n            raise ValueError(\"typecode should be 0-127\")\n        if not isinstance(data, bytes):\n            raise TypeError(\"data must have bytes type\")\n        L = len(data)\n        if L > 0xFFFFFFFF:\n            raise ValueError(\"Too large data\")\n        if L == 1:\n            self._buffer.write(b\"\\xd4\")\n        elif L == 2:\n            self._buffer.write(b\"\\xd5\")\n        elif L == 4:\n            self._buffer.write(b\"\\xd6\")\n        elif L == 8:\n            self._buffer.write(b\"\\xd7\")\n        elif L == 16:\n            self._buffer.write(b\"\\xd8\")\n        elif L <= 0xFF:\n            self._buffer.write(b\"\\xc7\" + struct.pack(\"B\", L))\n        elif L <= 0xFFFF:","sourceCodeStart":828,"sourceCodeEnd":864,"githubUrl":"https://github.com/pypa/pip/blob/f399c3718970b1b0e2478dac5296eb62679a9b86/src/pip/_vendor/msgpack/fallback.py#L828-L864","documentation":"Raised by msgpack's pure-Python Packer.pack_ext_type when the integer typecode is outside the range 0–127 inclusive. The msgpack ext format uses a single unsigned byte (0–127 for user-defined types; the format reserves negative codes for system types like Timestamp). Note that pack_ext_type accepts 0–127, whereas ExtType.__new__ in ext.py:14 accepts 0–127 as well — both are consistent.","triggerScenarios":"Calling packer.pack_ext_type(typecode, data) where typecode is negative or greater than 127. Common when typecode is derived from an unvalidated user/config value or an enum with large values.","commonSituations":"Config-driven type codes that allow arbitrary integers; auto-incrementing counter used as typecode that exceeds 127; negative typecode intended for a system type.","solutions":["Clamp/validate the typecode to 0–127 before calling pack_ext_type.","Use a bounded registry/enum for extension type codes.","If you need more than 128 types, encode a sub-type byte inside the data payload."],"exampleFix":"# before\npacker.pack_ext_type(200, b'data')  # out of range — raises\n\n# after\nTYPE_CODE = 42  # validated to be 0-127\nassert 0 <= TYPE_CODE <= 127\npacker.pack_ext_type(TYPE_CODE, b'data')","handlingStrategy":"validation","validationCode":"def validate_ext_typecode_range(typecode: int) -> int:\n    if not 0 <= typecode <= 127:\n        raise ValueError(f'typecode must be 0-127, got {typecode}')\n    return typecode","typeGuard":"def is_valid_ext_typecode(typecode) -> bool:\n    return isinstance(typecode, int) and 0 <= typecode <= 127","tryCatchPattern":"try:\n    packer.pack_ext_type(code, data)\nexcept ValueError as e:\n    if '0-127' in str(e):\n        code = code % 128  # wrap or choose a valid code\n        packer.pack_ext_type(code, data)\n    else:\n        raise","preventionTips":["Use an IntEnum bounded to 0-127 for extension type codes.","Validate typecode range at configuration time.","Document the 128-type-code limit for downstream consumers."],"tags":["msgpack","ext-type","validation","range"],"backgroundTag":null,"analyzedSha":"f399c3718970b1b0e2478dac5296eb62679a9b86","analyzedAt":"2026-08-08T23:01:42.227Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}