{"record":{"id":"9b01f51211a88fb6","repo":"geektutu/7days-golang","slug":"invalid-magic-number","errorCode":null,"errorMessage":"invalid magic number","messagePattern":"invalid magic number","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"gee-bolt/day1-pages/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/day1-pages/meta.go#L9-L34","documentation":"The bolt database file header does not start with the expected magic number constant, so meta.validate() in gee-bolt/day1-pages rejects the page as not a valid database file. The magic number is a fixed marker written when the DB is initialized; mismatch means the buffer is not a bolt file or is corrupted.","triggerScenarios":"Opening a file that is not a bolt database, reading an empty/zero-filled page, byte-swapped or truncated file, or reading a buffer from the wrong offset so the magic field lands on garbage.","commonSituations":"Pointing the DB at a log/text file or a different format's file; a crashed first write left an all-zero file; manually copying/editing the file; opening a file produced by an incompatible version.","solutions":["Verify the file path points to a file previously created by this bolt implementation","Delete or archive the corrupt/foreign file and re-create the database from scratch","Check you are reading the meta page at the correct offset (page 0 for the first meta)","Compare the file's first bytes against the expected magic constant with a hex dump to confirm corruption"],"exampleFix":"// before\nf, _ := os.OpenFile(\"notes.txt\", os.O_RDWR, 0600)\ndb := bolt.Open(f) // -> invalid magic number\n// after\nf, _ := os.OpenFile(\"notes.bolt\", os.O_RDWR|os.O_CREATE, 0600)\ndb := bolt.Open(f) // freshly created file has magic written","handlingStrategy":"validation","validationCode":"func looksLikeBoltFile(path string) bool {\n    b, err := os.ReadFile(path)\n    if err != nil || len(b) < 4 { return false }\n    return bytes.Equal(b[:4], magicBytes()) // compare first bytes to magic\n}","typeGuard":"func isValidBoltMeta(m *meta) bool { return m != nil && m.magic == magic }","tryCatchPattern":null,"preventionTips":["Always create DB files via the library's open/create path, never os.Create alone","Validate file existence and format before opening in tooling","Hex-dump first bytes when diagnosing file issues","Keep DB files out of editors/sync tools that may rewrite them"],"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"}