{"record":{"id":"9d01407e7ba6057b","repo":"dgraph-io/badger","slug":"checksum-type-not-supported","errorCode":null,"errorMessage":"checksum type not supported","messagePattern":"checksum type not supported","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"y/checksum.go","lineNumber":28,"sourceCode":"\t\"hash/crc32\"\n\n\t\"github.com/cespare/xxhash/v2\"\n\n\t\"github.com/dgraph-io/badger/v4/pb\"\n)\n\n// ErrChecksumMismatch is returned at checksum mismatch.\nvar ErrChecksumMismatch = stderrors.New(\"checksum mismatch\")\n\n// CalculateChecksum calculates checksum for data using ct checksum type.\nfunc CalculateChecksum(data []byte, ct pb.Checksum_Algorithm) uint64 {\n\tswitch ct {\n\tcase pb.Checksum_CRC32C:\n\t\treturn uint64(crc32.Checksum(data, CastagnoliCrcTable))\n\tcase pb.Checksum_XXHash64:\n\t\treturn xxhash.Sum64(data)\n\tdefault:\n\t\tpanic(\"checksum type not supported\")\n\t}\n}\n\n// VerifyChecksum validates the checksum for the data against the given expected checksum.\nfunc VerifyChecksum(data []byte, expected *pb.Checksum) error {\n\tactual := CalculateChecksum(data, expected.Algo)\n\tif actual != expected.Sum {\n\t\treturn Wrapf(ErrChecksumMismatch, \"actual: %d, expected: %d\", actual, expected.Sum)\n\t}\n\treturn nil\n}\n","sourceCodeStart":10,"sourceCodeEnd":40,"githubUrl":"https://github.com/dgraph-io/badger/blob/2a001d466f6b71a917319a1db41f99860e16e269/y/checksum.go#L10-L40","documentation":"CalculateChecksum computes a checksum over data using the algorithm carried by a pb.Checksum. It only supports CRC32C (Castagnoli) and XXHash64; any other checksum algorithm value reaches the default branch and panics. This is a programming/config invariant violation, not a recoverable runtime condition.","triggerScenarios":"Calling CalculateChecksum (directly or via VerifyChecksum) with a pb.Checksum whose Algo is unset (0), a newer/older Badger checksum type, or any value outside {Checksum_CRC32C, Checksum_XXHash64}. VerifyChecksum on a manifest or table entry written by a different Badger version that introduced a new checksum algorithm.","commonSituations":"Upgrading/downgrading Badger so on-disk checksum algorithm IDs no longer match the compiled-in enum; hand-constructing a pb.Checksum{} without setting Algo (zero value); corrupted metadata changing the Algo field; fuzzing or generated test data with arbitrary algorithm values.","solutions":["Check that the checksum algorithm value comes from a compatible Badger version; rewrite the data with a supported algorithm (e.g. re-run badger stream/backup-restore with default checksum settings)","If constructing pb.Checksum manually, set Algo explicitly to pb.Checksum_CRC32C or pb.Checksum_XXHash64 before calling CalculateChecksum/VerifyChecksum","Verify data files are not corrupted; restore from backup if the Algo field is garbage","If wrapping calls, pre-check algo and handle unsupported values before calling"],"exampleFix":"// before\nchk := &pb.Checksum{Sum: 123} // Algo unset -> panic\nok := VerifyChecksum(data, chk)\n// after\nchk := &pb.Checksum{Algo: pb.Checksum_CRC32C, Sum: 123}\nok := VerifyChecksum(data, chk)","handlingStrategy":"validation","validationCode":"func hasSupportedAlgo(c *pb.Checksum) bool {\n\treturn c != nil && (c.Algo == pb.Checksum_CRC32C || c.Algo == pb.Checksum_XXHash64)\n}\n// call only if hasSupportedAlgo(chk)","typeGuard":"func isSupportedChecksumAlgo(a pb.Checksum_Algorithm) bool {\n\treturn a == pb.Checksum_CRC32C || a == pb.Checksum_XXHash64\n}","tryCatchPattern":"// It panics, so guard the call:\nfunc safeCalc(data []byte, algo pb.Checksum_Algorithm) (sum uint64, err error) {\n\tdefer func() {\n\t\tif r := recover(); r != nil {\n\t\t\terr = fmt.Errorf(\"checksum: %v\", r)\n\t\t}\n\t}()\n\tif !isSupportedChecksumAlgo(algo) {\n\t\treturn 0, errors.New(\"unsupported checksum algo\")\n\t}\n\treturn CalculateChecksum(data, &pb.Checksum{Algo: algo}), nil\n}","preventionTips":["Never construct pb.Checksum without explicitly setting Algo","Only round-trip checksums through the same badger version that wrote them","Prefer VerifyChecksum/CalculateChecksum on data produced by the library, not hand-built structs","When loading data from unknown sources, validate the Algo field before checksum operations"],"tags":["panic","checksum","badger"],"backgroundTag":"unsupported-checksum-algorithm","analyzedSha":"2a001d466f6b71a917319a1db41f99860e16e269","analyzedAt":"2026-09-05T13:00:02.264Z","contentChangedAt":"2026-09-05T13:00:02.264Z","schemaVersion":2},"datasetVersion":"2026-09-12T17:17:11.597Z"}