jackc/pgx · error
invalid length for bit/varbit
Error message
invalid length for bit/varbit: %w
What it means
Reading the 4-byte bit-length header of a binary bit/varbit value failed (truncated or unreadable message); the wrapped pgio error names the underlying read problem.
Solutions
- Fail the scan; the binary payload is malformed
- Check for truncated network data or a proxy altering binary results
- Verify the column is actually bit/varbit and binary format is being used correctly
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at pgtype/bits.go:174 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of jackc/pgx@ec1a0befd2 (2026-08-04).
Data as JSON: /api/errors/780d755f45aa53af.
Report an issue: GitHub.
Appendix: source
Thrown at pgtype/bits.go:174
return nil, err
}
return box, nil
}
type scanPlanBinaryBitsToBitsScanner struct{}
func (scanPlanBinaryBitsToBitsScanner) Scan(src []byte, dst any) error {
scanner := (dst).(BitsScanner)
if src == nil {
return scanner.ScanBits(Bits{})
}
r := pgio.NewReader(src)
bitLen := r.Int32()
if err := r.Err(); err != nil {
return fmt.Errorf("invalid length for bit/varbit: %w", err)
}
if bitLen < 0 {
return fmt.Errorf("invalid length for bit/varbit: bitLen=%d", bitLen)
}
// Finish rejects trailing bytes, so together with the read below the data
// must be exactly the number of bytes bitLen calls for.
data := r.Bytes((int(bitLen) + 7) / 8)
if err := r.Finish(); err != nil {
return fmt.Errorf("invalid length for bit/varbit: bitLen=%d: %w", bitLen, err)
}
buf := make([]byte, len(data))
copy(buf, data)
return scanner.ScanBits(Bits{Bytes: buf, Len: bitLen, Valid: true})
}
View on GitHub (pinned to ec1a0befd2)