{"record":{"id":"d66a537856497d5f","repo":"OpenNHP/opennhp","slug":"length-d-exceeds-maximum-encoded-value-d","errorCode":null,"errorMessage":"length %d exceeds maximum encoded value (%d)","messagePattern":"length (.+?) exceeds maximum encoded value \\((.+?)\\)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"nhp/core/ztdo/ztdo.go","lineNumber":660,"sourceCode":"\t\tnewArray := reflect.New(arrayType).Elem()\n\n\t\tfor i := range len {\n\t\t\tnewArray.Index(i).SetUint(uint64(dst[i]))\n\t\t}\n\n\t\trvalue.Set(newArray)\n\n\t} else {\n\t\tpanic(\"not support\")\n\t}\n}\n\n// encodeMetadataLength encodes a length continuation bit into the MSB of metadata length\nfunc encodeMetadataLength(length int, continuation bool) ([2]byte, error) {\n\tvar result [2]byte\n\n\tif length > MetadataChunkMaxSize {\n\t\treturn result, fmt.Errorf(\"length %d exceeds maximum encoded value (%d)\", length, MetadataChunkMaxSize)\n\t}\n\n\tif length < 0 {\n\t\treturn result, fmt.Errorf(\"length %d is negative\", length)\n\t}\n\n\t//nolint:gosec // G602: result is [2]byte array, indices 0 and 1 are always valid\n\tresult[0] = byte((length >> 8) & 0x7F)\n\tif continuation {\n\t\tresult[0] |= 0x80\n\t}\n\tresult[1] = byte(length & 0xFF)\n\n\tif littleEndian {\n\t\tresult[0], result[1] = result[1], result[0]\n\t}\n\n\treturn result, nil","sourceCodeStart":642,"sourceCodeEnd":678,"githubUrl":"https://github.com/OpenNHP/opennhp/blob/6e04ca5ff03222a699c24205cd4bf8fee9af7ffe/nhp/core/ztdo/ztdo.go#L642-L678","documentation":"Metadata chunk lengths are encoded into 2 bytes where the MSB is a continuation bit, leaving 15 bits of range (MetadataChunkMaxSize = 32767). encodeMetadataLength refuses any length above that maximum because it cannot be represented in the on-disk encoding. Callers like SetMetadata split large metadata into chunks; this error indicates a chunk bigger than the encoder supports.","triggerScenarios":"Calling encodeMetadataLength (directly or via the anonymous function used by metadata processing) with length > 32767; building a single metadata chunk larger than MetadataChunkMaxSize instead of splitting it.","commonSituations":"Writing custom metadata handling that bypasses SetMetadata's chunk-splitting loop; computing a chunk size from a wrong constant (e.g. using 65535 for a '2-byte' length); passing a total metadata length instead of a per-chunk length.","solutions":["Split metadata into chunks of at most MetadataChunkMaxSize (32767) bytes, as SetMetadata does.","Use SetMetadata instead of calling the low-level encoder directly so chunking is handled for you.","Double-check that you pass the chunk length, not the total metadata length, to encodeMetadataLength.","If you need larger logical metadata, emit multiple continuation-chained chunks rather than one oversized chunk."],"exampleFix":"// before\nenc, err := encodeMetadataLength(len(bigMetadata), true) // fails if > 32767\n// after\nconst chunkMax = 32767\nfor i := 0; i < len(bigMetadata); i += chunkMax {\n    end := min(i+chunkMax, len(bigMetadata))\n    cont := end < len(bigMetadata)\n    enc, err := encodeMetadataLength(end-i, cont)\n    // write chunk bigMetadata[i:end] ...\n}","handlingStrategy":"validation","validationCode":"if len(meta) > 32767 {\n    // split before encoding, as SetMetadata does\n    chunks := (len(meta) + 32767 - 1) / 32767\n}\n// or per-chunk check:\nif chunkLen > 32767 { return errors.New(\"chunk exceeds MetadataChunkMaxSize\") }","typeGuard":null,"tryCatchPattern":"enc, err := encodeMetadataLength(n, cont)\nif err != nil {\n    if strings.Contains(err.Error(), \"exceeds maximum\") {\n        return fmt.Errorf(\"metadata chunk %d > %d; split it\", n, 32767)\n    }\n    return err\n}","preventionTips":["Never call encodeMetadataLength with total length; pass per-chunk length","Reuse SetMetadata instead of hand-rolling chunking","Reference the MetadataChunkMaxSize constant, not a magic number"],"tags":["ztdo","metadata","encoding","value-out-of-range"],"backgroundTag":"value-out-of-range","analyzedSha":"6e04ca5ff03222a699c24205cd4bf8fee9af7ffe","analyzedAt":"2026-09-07T15:44:59.941Z","contentChangedAt":"2026-09-07T15:44:59.941Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}