{"record":{"id":"9ec473b3ea6797b6","repo":"HelloZeroNet/ZeroNet","slug":"invalid-bit-s","errorCode":null,"errorMessage":"Invalid bit: %s","messagePattern":"Invalid bit: (.+?)","errorType":"validation","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"plugins/Bigfile/BigfilePiecefield.py","lineNumber":52,"sourceCode":"    if not data:\n        return b\"\"\n\n    res = []\n    char = b\"\\x01\"\n    for times in data:\n        if times > 10000:\n            return b\"\"\n        res.append(char * times)\n        if char == b\"\\x01\":\n            char = b\"\\x00\"\n        else:\n            char = b\"\\x01\"\n    return b\"\".join(res)\n\n\ndef spliceBit(data, idx, bit):\n    if bit != b\"\\x00\" and bit != b\"\\x01\":\n        raise Exception(\"Invalid bit: %s\" % bit)\n\n    if len(data) < idx:\n        data = data.ljust(idx + 1, b\"\\x00\")\n    return data[:idx] + bit + data[idx+ 1:]\n\nclass Piecefield(object):\n    def tostring(self):\n        return \"\".join([\"1\" if b else \"0\" for b in self.tobytes()])\n\n\nclass BigfilePiecefield(Piecefield):\n    __slots__ = [\"data\"]\n\n    def __init__(self):\n        self.data = b\"\"\n\n    def frombytes(self, s):\n        if not isinstance(s, bytes) and not isinstance(s, bytearray):","sourceCodeStart":34,"sourceCodeEnd":70,"githubUrl":"https://github.com/HelloZeroNet/ZeroNet/blob/454c0b2e7e000fda7000cba49027541fbf327b96/plugins/Bigfile/BigfilePiecefield.py#L34-L70","documentation":"spliceBit sets a single byte in a piecefield bytes blob at idx to bit, which must be exactly b'\\x00' or b'\\x01'. Any other value (b'', b'True', 0/1 ints, etc.) raises Exception(\"Invalid bit: %s\"). It's the guard for the Piecefield __setitem__ path.","triggerScenarios":"Piecefield.__setitem__(idx, value) called with an int 0/1 instead of bytes b'\\x00'/b'\\x01', with an empty bytes value, or with a bool/string; indexing a piecefile with arbitrary truthy values.","commonSituations":"Code treating Piecefield like a list of bools (pf[i] = 1); deserialization layers converting bytes flags to ints; typos like b'' or '0' instead of b'\\x01'.","solutions":["Pass bytes: pf[idx] = b\"\\x01\" (or b\"\\x00\"), not 1/True.","Wrap the assignment: convert int/bool with b\"\\x01\" if value else b\"\\x00\" before setting.","Fix the piece-tracking code that produces non-byte flags.","Use Piecefield methods instead of raw indexing where possible."],"exampleFix":"// before\npiecefield[3] = 1  # int -> Invalid bit: 1\n// after\npiecefield[3] = b\"\\x01\" if downloaded else b\"\\x00\"","handlingStrategy":"type-guard","validationCode":"def to_bit(value):\n    if value in (b\"\\x00\", b\"\\x01\"):\n        return value\n    return b\"\\x01\" if value in (1, True, b\"\\x01\", \"1\") else b\"\\x00\"","typeGuard":"def is_valid_bit(bit):\n    return bit in (b\"\\x00\", b\"\\x01\")","tryCatchPattern":"try:\n    piecefield[idx] = value\nexcept Exception as e:\n    if \"Invalid bit\" in str(e):\n        piecefield[idx] = b\"\\x01\" if value else b\"\\x00\"\n    else:\n        raise","preventionTips":["Always assign byte flags b'\\x00'/b'\\x01', not int/bool","Add helper setters that coerce ints/bools to bytes","Document Piecefield's __setitem__ contract in team docs","Add asserts in download code before setting piece bits"],"tags":["type-error","bigfile","validation","piecefield"],"backgroundTag":"invalid-data-type","analyzedSha":"454c0b2e7e000fda7000cba49027541fbf327b96","analyzedAt":"2026-09-02T19:46:57.278Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-10T02:17:09.455Z"}