{"record":{"id":"9bf8f8c9be812131","repo":"geektutu/7days-golang","slug":"invalid-magic-number-9bf8f8","errorCode":null,"errorMessage":"invalid magic number","messagePattern":"invalid magic number","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"gee-bolt/day3-tree/meta.go","lineNumber":27,"sourceCode":"// Represent a marker value to indicate that a file is a gee-bolt DB\nconst magic uint32 = 0xED0CDAED\n\ntype 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":9,"sourceCodeEnd":34,"githubUrl":"https://github.com/geektutu/7days-golang/blob/cf3644382101dc13e7fd92e8f5c66cabc51bcd3b/gee-bolt/day3-tree/meta.go#L9-L34","documentation":"Sentinel validation failure in meta.validate(): the meta page read from disk does not carry the gee-bolt magic constant 0xED0CDAED, so the file is not a gee-bolt database (or is corrupted/truncated). It fires for any DB open whose first-page magic field differs from the expected marker.","triggerScenarios":"Opening a nonexistent-format file, an empty file created with O_CREATE but never initialized with magic, or reading at the wrong page offset.","commonSituations":"Reusing a day1 file path with a later schema/version, tests pointing at a temp file that was never initialized, mistyped file path resolving to another format.","solutions":["Initialize the file through the library (create + write meta with magic) before opening/validating","Confirm the path resolves to a bolt file created by the same codebase version","Remove the stale/corrupt file and let the library create a fresh one","Hex-dump the first bytes to compare against the magic constant"],"exampleFix":"// before\nf, _ := os.Create(\"data.db\")\nf.Close() // empty file, no magic written\nm.validate() // -> invalid magic number\n// after\ndb, _ := bolt.Open(\"data.db\", os.O_CREATE|os.O_RDWR, 0600) // meta written with magic","handlingStrategy":"validation","validationCode":"func ensureInitialized(path string) error {\n    if fi, err := os.Stat(path); err != nil || fi.Size() == 0 {\n        return bolt.Open(path, os.O_CREATE|os.O_RDWR, 0600) // writes magic\n    }\n    return nil\n}","typeGuard":"func isValidBoltMeta(m *meta) bool { return m != nil && m.magic == magic }","tryCatchPattern":null,"preventionTips":["Initialize files through the library before manual inspection","Use one codebase version consistently on a given DB file","Sanity-check file size > 0 before opening","Don't point configs at temp/scratch files"],"tags":["database","file-format","corruption"],"backgroundTag":"invalid-file-magic-number","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"}