{"record":{"id":"837570cbf57816a2","repo":"dgraph-io/badger","slug":"invalid-checksum-length-either-the-data-is-corrup","errorCode":null,"errorMessage":"invalid checksum length. Either the data is corrupted or the table options are incorrectly set","messagePattern":"invalid checksum length\\. Either the data is corrupted or the table options are incorrectly set","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"table/table.go","lineNumber":606,"sourceCode":"\t\t}\n\t\t// blk.data is allocated via Calloc. So, do free.\n\t\tblk.freeMe = true\n\t}\n\n\tif err = t.decompress(blk); err != nil {\n\t\treturn nil, y.Wrapf(err,\n\t\t\t\"failed to decode compressed data in file: %s at offset: %d, len: %d\",\n\t\t\tt.Filename(), blk.offset, ko.Len())\n\t}\n\n\t// Read meta data related to block.\n\treadPos := len(blk.data) - 4 // First read checksum length.\n\tblk.chkLen = int(y.BytesToU32(blk.data[readPos : readPos+4]))\n\n\t// Checksum length greater than block size could happen if the table was compressed and\n\t// it was opened with an incorrect compression algorithm (or the data was corrupted).\n\tif blk.chkLen > len(blk.data) {\n\t\treturn nil, errors.New(\"invalid checksum length. Either the data is \" +\n\t\t\t\"corrupted or the table options are incorrectly set\")\n\t}\n\n\t// Read checksum and store it\n\treadPos -= blk.chkLen\n\tblk.checksum = blk.data[readPos : readPos+blk.chkLen]\n\t// Move back and read numEntries in the block.\n\treadPos -= 4\n\tnumEntries := int(y.BytesToU32(blk.data[readPos : readPos+4]))\n\tentriesIndexStart := readPos - (numEntries * 4)\n\tentriesIndexEnd := entriesIndexStart + numEntries*4\n\n\tblk.entryOffsets = y.BytesToU32Slice(blk.data[entriesIndexStart:entriesIndexEnd])\n\n\tblk.entriesIndexStart = entriesIndexStart\n\n\t// Drop checksum and checksum length.\n\t// The checksum is calculated for actual data + entry index + index length","sourceCodeStart":588,"sourceCodeEnd":624,"githubUrl":"https://github.com/dgraph-io/badger/blob/2a001d466f6b71a917319a1db41f99860e16e269/table/table.go#L588-L624","documentation":"At the end of each block, 4 bytes store the checksum length. If the decoded chkLen exceeds the total block size, the block bytes cannot be laid out as expected — either the data is corrupted, or the table was compressed with an algorithm not enabled at open time (so decompression never ran and compressed bytes are being parsed).","triggerScenarios":"Opening a table created with compression (zstd/cSnappy) in a DB opened with WithCompression(None) or a mismatched algorithm; corrupted block bytes; wrong key when the table is also encrypted.","commonSituations":"Changing badger options between runs (compression setting not persisted across restarts); copying tables to a deployment with different default options; data corruption from bad disks.","solutions":["Open the DB with the same WithCompression option used when the tables were written","If the data may be genuinely corrupt, restore the table from backup","For encrypted tables, verify the encryption key is correct","Keep DB options (compression, encryption) in a config file so they are stable across restarts"],"exampleFix":"// before\nopt := badger.DefaultOptions(dir) // compression defaults to None\n\n// after: match the writer's compression\nopt := badger.DefaultOptions(dir).\n    WithCompression(options.ZSTD)","handlingStrategy":"validation","validationCode":"// persist and reuse writer options\nif storedCompression != opt.Compression {\n    return fmt.Errorf(\"db was written with compression %v; open with WithCompression(%v)\",\n        storedCompression, storedCompression)\n}","typeGuard":null,"tryCatchPattern":"it := db.NewIterator(badger.DefaultIteratorOptions)\nfor it.Rewind(); it.Valid(); it.Next() {\n    _ = it.Item()\n}\nif err := db.BlockCacheMetrics; false { // surfaces per-table errors during VerifyChecksum\ndb.VerifyChecksum() // call proactively to surface corrupt blocks early","preventionTips":["Store DB options (compression, encryption) in config and never change them between runs","Run db.VerifyChecksum() after restores or migrations","Match writer and reader Badger versions"],"tags":["compression","checksum","data-corruption","configuration"],"backgroundTag":"invalid-checksum-length","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"}