{"record":{"id":"4faca621f82432ee","repo":"nats-io/nats-server","slug":"errbadencoding","errorCode":"ErrBadEncoding","errorMessage":"ss: bad encoding","messagePattern":"ss: bad encoding","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/avl/seqset.go","lineNumber":307,"sourceCode":"\tle.PutUint32(buf[i:], uint32(nn))\n\tle.PutUint32(buf[i+4:], uint32(ss.size))\n\ti += 8\n\tss.root.nodeIter(func(n *node) {\n\t\tle.PutUint64(buf[i:], n.base)\n\t\ti += 8\n\t\tfor _, b := range n.bits {\n\t\t\tle.PutUint64(buf[i:], b)\n\t\t\ti += 8\n\t\t}\n\t\tle.PutUint16(buf[i:], uint16(n.h))\n\t\ti += 2\n\t})\n\treturn buf[:i]\n}\n\n// ErrBadEncoding is returned when we can not decode properly.\nvar (\n\tErrBadEncoding = errors.New(\"ss: bad encoding\")\n\tErrBadVersion  = errors.New(\"ss: bad version\")\n\tErrSetNotEmpty = errors.New(\"ss: set not empty\")\n)\n\n// Decode returns the sequence set and number of bytes read from the buffer on success.\nfunc Decode(buf []byte) (*SequenceSet, int, error) {\n\tif len(buf) < minLen || buf[0] != magic {\n\t\treturn nil, -1, ErrBadEncoding\n\t}\n\n\tswitch v := buf[1]; v {\n\tcase 1:\n\t\treturn decodev1(buf)\n\tcase 2:\n\t\treturn decodev2(buf)\n\tdefault:\n\t\treturn nil, -1, ErrBadVersion\n\t}","sourceCodeStart":289,"sourceCodeEnd":325,"githubUrl":"https://github.com/nats-io/nats-server/blob/3a66a489d262bf89b71a71c955c94920394532f3/server/avl/seqset.go#L289-L325","documentation":"ErrBadEncoding is returned by SequenceSet Decode when the buffer cannot be a valid encoded sequence set: it is shorter than the minimum encoding length or does not start with the magic byte. It also guards against a declared node count that exceeds what the buffer could possibly contain, preventing out-of-bounds reads.","triggerScenarios":"Calling Decode with a buffer shorter than minLen, whose first byte != magic, or whose node count nn is negative or exceeds (len(buf)-minLen)/((numBuckets+1)*8+2).","commonSituations":"Reading a truncated or corrupted JetStream state file, decoding bytes that are not a sequence-set encoding (wrong stream/offset), or byte-order/serialization version confusion.","solutions":["Verify the source of the bytes: only pass buffers originally produced by SequenceSet Encode","Check buffer length and first byte (magic) before calling Decode","Rebuild the state (e.g. delete and recreate the consumer/stream state) if the file is corrupted","Confirm you are not off-by-one in the buffer slice passed to Decode"],"exampleFix":"// before\nss, n, err := avl.Decode(raw[1:]) // skips magic byte\n// after\nss, n, err := avl.Decode(raw) // Decode expects the magic byte at buf[0]","handlingStrategy":"try-catch","validationCode":"func decodable(buf []byte) bool {\n    return len(buf) >= 1 && buf[0] == magicByte // check magic before Decode\n}","typeGuard":null,"tryCatchPattern":"ss, n, err := avl.Decode(buf)\nif errors.Is(err, avl.ErrBadEncoding) {\n    log.Warnf(\"corrupt sequence set (%d bytes); rebuilding state\", len(buf))\n    ss = avl.NewSequenceSet()\n}","preventionTips":["Only decode buffers produced by Encode","Persist buffers with length prefixes or checksums to detect truncation/corruption","Never slice off bytes (like the magic byte) before Decode"],"tags":["serialization","encoding","jetstream","go"],"backgroundTag":"bad-binary-encoding","analyzedSha":"3a66a489d262bf89b71a71c955c94920394532f3","analyzedAt":"2026-09-02T04:41:54.247Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}