{"record":{"id":"cafb748d37940a47","repo":"dgraph-io/dgraph","slug":"count-length-4-for-key-q-parsed-key-v","errorCode":null,"errorMessage":"count length < 4 for key: %q, parsed key: %+v","messagePattern":"count length < 4 for key: %q, parsed key: %\\+v","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"x/keys.go","lineNumber":601,"sourceCode":"\t\tk = k[8:]\n\t\tp.StartUid = binary.BigEndian.Uint64(k)\n\tcase ByteIndex:\n\t\tif !p.HasStartUid {\n\t\t\tp.Term = string(k)\n\t\t\tbreak\n\t\t}\n\n\t\tif len(k) < 8 {\n\t\t\treturn p, errors.Errorf(\"StartUid length < 8 for key: %q, parsed key: %+v\", key, p)\n\t\t}\n\n\t\tterm := k[:len(k)-8]\n\t\tstartUid := k[len(k)-8:]\n\t\tp.Term = string(term)\n\t\tp.StartUid = binary.BigEndian.Uint64(startUid)\n\tcase ByteCount, ByteCountRev:\n\t\tif len(k) < 4 {\n\t\t\treturn p, errors.Errorf(\"count length < 4 for key: %q, parsed key: %+v\", key, p)\n\t\t}\n\t\tp.Count = binary.BigEndian.Uint32(k)\n\n\t\tif !p.HasStartUid {\n\t\t\tbreak\n\t\t}\n\n\t\tif len(k) != 12 {\n\t\t\treturn p, errors.Errorf(\"StartUid length != 8 for key: %q, parsed key: %+v\", key, p)\n\t\t}\n\n\t\tk = k[4:]\n\t\tp.StartUid = binary.BigEndian.Uint64(k)\n\tdefault:\n\t\t// Some other data type.\n\t\treturn p, errors.Errorf(\"Invalid data type\")\n\t}\n\treturn p, nil","sourceCodeStart":583,"sourceCodeEnd":619,"githubUrl":"https://github.com/dgraph-io/dgraph/blob/759e242be62c91f8d084da06ad0c8d21256d9c07/x/keys.go#L583-L619","documentation":"Error returned by x/keys.go while parsing a key when fewer than 4 bytes remain for the count field. The raw key and parsed structure are reported via %q and %+v. Indicates a truncated or malformed key; fix by correcting or regenerating the key data.","triggerScenarios":"Calling Parse/IsDropOpKey with a ByteCount or ByteCountRev key whose payload is shorter than 4 bytes.","commonSituations":"Corrupted count index entries in storage, hand-built keys missing the 4-byte count, or truncated data from a faulty migration/backup restore.","solutions":["Verify the key payload is >= 4 bytes with hex.Dump","Rebuild the key with the count-key construction helper","Audit the code path that wrote the key for truncation","Restore affected data from a backup if on-disk corruption"],"exampleFix":"// before: count key without 4-byte count\nkey := []byte{byte(x.ByteCount)}\np, err := x.Parse(key) // count length < 4\n// after\nkey := x.CountKey(attr, term, startUid) // includes 4-byte count\np, err := x.Parse(key)","handlingStrategy":"validation","validationCode":"if len(key) < 5 { return fmt.Errorf(\"count key too short: %d\", len(key)) }\nok, err := x.IsDropOpKey(key)","typeGuard":"func isMinCountKey(key []byte) bool { return len(key) >= 5 }","tryCatchPattern":null,"preventionTips":["Build count keys with the library helper including the 4-byte count","Checksum keys on write paths to catch truncation early","Validate fixtures in tests for exact key lengths"],"tags":["go","badger","key-parsing","count-index"],"backgroundTag":"malformed-key-length","analyzedSha":"759e242be62c91f8d084da06ad0c8d21256d9c07","analyzedAt":"2026-09-01T14:42:12.034Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}