{"record":{"id":"ce22733298c82f72","repo":"geektutu/7days-golang","slug":"invalid-checksum","errorCode":null,"errorMessage":"invalid checksum","messagePattern":"invalid checksum","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"gee-bolt/day1-pages/meta.go","lineNumber":30,"sourceCode":"type meta struct {\n\tmagic    uint32\n\tpageSize uint32\n\tpgid     uint64\n\tchecksum uint64\n}\n\nfunc (m *meta) sum64() uint64 {\n\tvar h = fnv.New64a()\n\t_, _ = h.Write((*[unsafe.Offsetof(meta{}.checksum)]byte)(unsafe.Pointer(m))[:])\n\treturn h.Sum64()\n}\n\nfunc (m *meta) validate() error {\n\tif m.magic != magic {\n\t\treturn errors.New(\"invalid magic number\")\n\t}\n\tif m.checksum != m.sum64() {\n\t\treturn errors.New(\"invalid checksum\")\n\t}\n\treturn nil\n}\n","sourceCodeStart":12,"sourceCodeEnd":34,"githubUrl":"https://github.com/geektutu/7days-golang/blob/cf3644382101dc13e7fd92e8f5c66cabc51bcd3b/gee-bolt/day1-pages/meta.go#L12-L34","documentation":"The meta page's magic number matched but its stored checksum no longer equals the recomputed xxhash over the meta struct, so validate() deems the page corrupt. This guards against torn writes and bit rot in the metadata page.","triggerScenarios":"Process crash mid-write leaving a partially updated meta page, external modification of the file, disk corruption, or writing meta without updating the checksum field.","commonSituations":"Power loss or kill -9 during a commit; editing the DB with a hex editor; copying the file while it was being written; a buggy custom writer that forgot to recompute sum64().","solutions":["Restore the database from backup — the meta page is inconsistent","If a second meta page exists, fall back to the other valid meta copy","Re-create the database and re-import data from another source","Never write the meta struct without recomputing and storing m.sum64() into the checksum field"],"exampleFix":"// before\nm.magic = magic\n// forgot: m.checksum = m.sum64() -> invalid checksum on next open\n// after\nm.magic = magic\nm.checksum = m.sum64()","handlingStrategy":"validation","validationCode":"func metaIsConsistent(m *meta) bool { return m.checksum == m.sum64() }","typeGuard":"func hasValidChecksum(m *meta) bool { return m != nil && m.checksum == m.sum64() }","tryCatchPattern":null,"preventionTips":["Always recompute checksum after any meta mutation, before writing","Use atomic write patterns (write temp file + fsync + rename)","Restore from backup on checksum failure instead of patching bytes","Avoid copying DB files while writers are active"],"tags":["database","corruption","checksum"],"backgroundTag":"checksum-mismatch","analyzedSha":"cf3644382101dc13e7fd92e8f5c66cabc51bcd3b","analyzedAt":"2026-09-03T18:31:24.087Z","contentChangedAt":"2026-09-03T18:31:24.087Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}