{"record":{"id":"bcf0aa98aede30b8","repo":"hyperledger/fabric","slug":"decoded-size-d-from-decodevarint-is-more-than-a","errorCode":null,"errorMessage":"decoded size (%d) from DecodeVarint is more than available bytes (%d)","messagePattern":"decoded size \\((.+?)\\) from DecodeVarint is more than available bytes \\((.+?)\\)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"common/ledger/util/util.go","lineNumber":68,"sourceCode":"\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":50,"sourceCodeEnd":78,"githubUrl":"https://github.com/hyperledger/fabric/blob/2736b63f8fd5932511d56fe68b7039d15977f7f6/common/ledger/util/util.go#L50-L78","documentation":"The encoded value declares a payload size s, but the available bytes after the size byte are fewer than s. This guards against out-of-range slice reads and means the buffer is truncated or the decode offset is wrong.","triggerScenarios":"Calling DecodeOrderPreservingVarUint64 on a truncated slice — e.g. passing the whole composite key where only the numeric suffix should be given, slicing with an off-by-one, or a key corrupted/shortened by an older writer version.","commonSituations":"Parsing keys returned by an iterator without trimming prefix bytes; upgrading/downgrading between versions with different composite-key layouts; corrupted ledger data after partial writes.","solutions":["Slice off the correct prefix so the decoder receives exactly size-byte + payload bytes","Verify the composite-key layout matches the writer's version (check key schema/version)","Check ledger data integrity if truncation is unexpected; resync or restore from snapshot","Add a length assertion (len(bytes)-1 >= bytes[0]) before decoding"],"exampleFix":"// before\nv, _, err := util.DecodeOrderPreservingVarUint64(fullKey) // fullKey has extra prefix\n// after\nnumPart := fullKey[len(prefix):] // pass only the encoded-number portion\nv, _, err := util.DecodeOrderPreservingVarUint64(numPart)","handlingStrategy":"validation","validationCode":"func hasEnoughBytes(b []byte) bool {\n\treturn len(b) > 0 && int(b[0]) <= len(b)-1\n}","typeGuard":"func isCompleteEncodedUint64(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(), \"more than available bytes\") {\n\t\tlog.Errorf(\"truncated encoded value: need %d payload bytes, got %d\", bytes[0], len(bytes)-1)\n\t}\n\treturn err\n}","preventionTips":["Trim composite-key prefixes so the decoder gets the full encoded value","Verify key schema/version matches the writer before parsing","Check for corruption/truncation if data was migrated or partially written","Unit-test round-trip encode/decode on real key layouts"],"tags":["encoding","varint","deserialization","truncated-data"],"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"}