{"record":{"id":"13e62e44e0a3013b","repo":"kopia/kopia","slug":"invalid-key-length-v-for-v","errorCode":null,"errorMessage":"invalid key length: %v for %v","messagePattern":"invalid key length: (.+?) for (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"repo/content/index/index_v2.go","lineNumber":506,"sourceCode":"\t}, nil\n}\n\n// buildV2 writes the pack index to the provided output.\nfunc buildV2(sortedInfos []*Info, output io.Writer) error { //nolint:gocyclo\n\tb2, err := newIndexBuilderV2(sortedInfos)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\tw := bufio.NewWriter(output)\n\n\t// prepare extra data to be appended at the end of an index.\n\textraData := b2.prepareExtraData(sortedInfos)\n\n\tif b2.keyLength == -1 {\n\t\tb2.keyLength = unknownKeySize\n\t} else if b2.keyLength <= 1 || b2.keyLength > maxUInt8 {\n\t\treturn errors.Errorf(\"invalid key length: %v for %v\", b2.keyLength, len(sortedInfos))\n\t}\n\n\tuniqueFormatInfo2IndexLen := len(b2.uniqueFormatInfo2Index)\n\tif uniqueFormatInfo2IndexLen > maxUInt8 {\n\t\treturn errors.Errorf(\"invalid unique format v2 info index length: %v\", uniqueFormatInfo2IndexLen)\n\t}\n\n\t// write header\n\theader := make([]byte, v2IndexHeaderSize)\n\theader[0] = Version2                                                   // version\n\theader[1] = byte(b2.keyLength)                                         //nolint:gosec // range checked above\n\tbinary.BigEndian.PutUint16(header[2:4], uint16(b2.entrySize))          //nolint:gosec\n\tbinary.BigEndian.PutUint32(header[4:8], uint32(b2.entryCount))         //nolint:gosec\n\tbinary.BigEndian.PutUint32(header[8:12], uint32(len(b2.packID2Index))) //nolint:gosec\n\theader[12] = byte(uniqueFormatInfo2IndexLen)\n\tbinary.BigEndian.PutUint32(header[13:17], uint32(b2.baseTimestamp)) //nolint:gosec\n\n\tif _, err := w.Write(header); err != nil {","sourceCodeStart":488,"sourceCodeEnd":524,"githubUrl":"https://github.com/kopia/kopia/blob/82495e54b584c1ef6073c9e1be048f57f8aef078/repo/content/index/index_v2.go#L488-L524","documentation":"The v2 index header stores the key (content ID) length in a single byte, so it must be greater than 1 and at most maxUInt8 (255). During Finish/build of the index, if the builder's keyLength is still unset it defaults to unknownKeySize; otherwise any value <= 1 or > 255 is rejected with this error, together with the number of indexed contents. This guards against corrupt or inconsistent content ID metadata being serialized.","triggerScenarios":"Finishing a v2 index build where b2.keyLength was computed from the sorted content IDs as <= 1 or > maxUInt8 — e.g. contents whose IDs are empty/single-character, or a key length that overflowed 255.","commonSituations":"Corrupted or foreign content metadata in the pack, custom hashers producing non-standard ID lengths, or mixing content formats with wildly different ID sizes into one index build.","solutions":["Inspect which contents have anomalous content ID lengths and remove or repair them before rebuilding the index.","Verify the hasher/algorithm used — content IDs must be a fixed, sane length (e.g. 22–64 bytes), not empty or > 255.","Ensure all contents in one index build use the same content-ID format (same key length).","Check for metadata corruption in the source packs via integrity verification tools."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"if b2.keyLength != unknownKeySize && (b2.keyLength <= 1 || b2.keyLength > maxUInt8) {\n    // reject the content set: content IDs have anomalous lengths\n}","typeGuard":null,"tryCatchPattern":"if err := idxBuilder.Finish(w); err != nil && strings.Contains(err.Error(), \"invalid key length\") {\n    // inspect content ID lengths; repair or exclude bad contents\n}","preventionTips":["Use a single, standard content ID hasher for all contents.","Validate content ID lengths at write time (non-empty, <= 255).","Run integrity verification on source packs before index builds.","Never mix content-ID formats in one index build."],"tags":["go","index-format","validation","content-id"],"backgroundTag":"invalid-argument-value","analyzedSha":"82495e54b584c1ef6073c9e1be048f57f8aef078","analyzedAt":"2026-09-07T20:35:21.689Z","contentChangedAt":"2026-09-07T20:35:21.689Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}