{"record":{"id":"4b54af7949572fb1","repo":"hyperledger/fabric","slug":"decoded-size-from-decodevarint-is-invalid-expecte","errorCode":null,"errorMessage":"decoded size from DecodeVarint is invalid, expected <=8, but got %d","messagePattern":"decoded size from DecodeVarint is invalid, expected <=8, but got (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"common/ledger/util/util.go","lineNumber":66,"sourceCode":"\tencodedBytes := make([]byte, size+1)\n\tencodedBytes[0] = sizeBytes[0]\n\tcopy(encodedBytes[1:], bytes[startingIndex:])\n\treturn encodedBytes\n}\n\n// DecodeOrderPreservingVarUint64 decodes the number from the bytes obtained from method 'EncodeOrderPreservingVarUint64'.\n// It returns the decoded number, the number of bytes that are consumed in the process, and an error if the input bytes are invalid.\nfunc DecodeOrderPreservingVarUint64(bytes []byte) (uint64, int, error) {\n\ts, numBytes := protowire.ConsumeVarint(bytes)\n\tif numBytes < 0 {\n\t\ts, numBytes = 0, 0\n\t}\n\n\tswitch {\n\tcase numBytes != 1:\n\t\treturn 0, 0, errors.Errorf(\"number of consumed bytes from DecodeVarint is invalid, expected 1, but got %d\", numBytes)\n\tcase s > 8:\n\t\treturn 0, 0, errors.Errorf(\"decoded size from DecodeVarint is invalid, expected <=8, but got %d\", s)\n\tcase int(s) > len(bytes)-1:\n\t\treturn 0, 0, errors.Errorf(\"decoded size (%d) from DecodeVarint is more than available bytes (%d)\", s, len(bytes)-1)\n\tdefault:\n\t\t// no error\n\t\tsize := int(s)\n\t\tdecodedBytes := make([]byte, 8)\n\t\tcopy(decodedBytes[8-size:], bytes[1:size+1])\n\t\tnumBytesConsumed := size + 1\n\t\treturn binary.BigEndian.Uint64(decodedBytes), numBytesConsumed, nil\n\t}\n}\n","sourceCodeStart":48,"sourceCodeEnd":78,"githubUrl":"https://github.com/hyperledger/fabric/blob/2736b63f8fd5932511d56fe68b7039d15977f7f6/common/ledger/util/util.go#L48-L78","documentation":"After DecodeVarint reads the size byte of an order-preserving encoded uint64, that size must be in 1..8 (a uint64 is at most 8 bytes). A decoded size of 0 or > 8 means the byte stream is not a valid order-preserving encoding, so this error is returned.","triggerScenarios":"Calling DecodeOrderPreservingVarUint64 on a size byte that is 0 or 9..127 — i.e. bytes not produced by EncodeOrderPreservingVarUint64, or decoding at the wrong offset within a composite key so the 'size' byte is actually payload data.","commonSituations":"Corrupted or hand-edited ledger keys; passing raw varint-encoded values (where 0x09+ prefix bytes differ); test inputs like TestDecodingBadInputBytes; mixing up which portion of a composite key is the encoded number.","solutions":["Confirm the input came from EncodeOrderPreservingVarUint64 and you're slicing the correct key offset","Hex-dump the leading byte and verify it is a plausible size (0x01–0x08)","Check for data corruption if the same key previously decoded fine; restore from backup/resync","Add a pre-check on bytes[0] <= 8 before calling the decoder"],"exampleFix":"// before\nv, n, err := util.DecodeOrderPreservingVarUint64(b)\n// after\nif len(b) == 0 || b[0] == 0 || b[0] > 8 {\n\treturn fmt.Errorf(\"invalid encoded size byte: %#x\", b[0])\n}\nv, n, err := util.DecodeOrderPreservingVarUint64(b)","handlingStrategy":"validation","validationCode":"func validSizeByte(b []byte) bool {\n\treturn len(b) > 0 && b[0] >= 0x01 && b[0] <= 0x08\n}","typeGuard":"func isOrderPreservingEncoded(b []byte) bool {\n\treturn len(b) > 0 && b[0] >= 1 && b[0] <= 8 && len(b) >= int(b[0])+1\n}","tryCatchPattern":"v, n, err := util.DecodeOrderPreservingVarUint64(bytes)\nif err != nil {\n\tif strings.Contains(err.Error(), \"expected <=8\") {\n\t\tlog.Errorf(\"bad size byte %#x — not order-preserving encoded\", bytes[0])\n\t}\n\treturn err\n}","preventionTips":["Verify the leading size byte is 1..8 before decoding","Ensure decode offset is correct within composite keys","Restore from backup/resync if previously-valid keys now fail","Share one encode/decode helper to avoid divergent formats"],"tags":["encoding","varint","deserialization","ledger"],"backgroundTag":"invalid-varint-encoding","analyzedSha":"2736b63f8fd5932511d56fe68b7039d15977f7f6","analyzedAt":"2026-09-04T08:52:36.465Z","contentChangedAt":"2026-09-04T08:52:36.465Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}