HelloZeroNet/ZeroNet · error · Exception

Invalid bit: %s

Error message

Invalid bit: %s

What it means

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.

Source

Thrown at plugins/Bigfile/BigfilePiecefield.py:52

    if not data:
        return b""

    res = []
    char = b"\x01"
    for times in data:
        if times > 10000:
            return b""
        res.append(char * times)
        if char == b"\x01":
            char = b"\x00"
        else:
            char = b"\x01"
    return b"".join(res)


def spliceBit(data, idx, bit):
    if bit != b"\x00" and bit != b"\x01":
        raise Exception("Invalid bit: %s" % bit)

    if len(data) < idx:
        data = data.ljust(idx + 1, b"\x00")
    return data[:idx] + bit + data[idx+ 1:]

class Piecefield(object):
    def tostring(self):
        return "".join(["1" if b else "0" for b in self.tobytes()])


class BigfilePiecefield(Piecefield):
    __slots__ = ["data"]

    def __init__(self):
        self.data = b""

    def frombytes(self, s):
        if not isinstance(s, bytes) and not isinstance(s, bytearray):

View on GitHub (pinned to 454c0b2e7e)

Solutions

  1. Pass bytes: pf[idx] = b"\x01" (or b"\x00"), not 1/True.
  2. Wrap the assignment: convert int/bool with b"\x01" if value else b"\x00" before setting.
  3. Fix the piece-tracking code that produces non-byte flags.
  4. Use Piecefield methods instead of raw indexing where possible.

Example fix

// before
piecefield[3] = 1  # int -> Invalid bit: 1
// after
piecefield[3] = b"\x01" if downloaded else b"\x00"
Defensive patterns

Strategy: type-guard

Validate before calling

def to_bit(value):
    if value in (b"\x00", b"\x01"):
        return value
    return b"\x01" if value in (1, True, b"\x01", "1") else b"\x00"

Type guard

def is_valid_bit(bit):
    return bit in (b"\x00", b"\x01")

Try / catch

try:
    piecefield[idx] = value
except Exception as e:
    if "Invalid bit" in str(e):
        piecefield[idx] = b"\x01" if value else b"\x00"
    else:
        raise

Prevention

When it happens

Trigger: 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.

Common situations: 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'.

Related errors


AI-assisted analysis of HelloZeroNet/ZeroNet@454c0b2e7e (2026-09-02). Data as JSON: /api/errors/9ec473b3ea6797b6. Report an issue: GitHub.