{"record":{"id":"44d80b398fa74961","repo":"lionsoul2014/ip2region","slug":"seek-to-the-header-w","errorCode":null,"errorMessage":"seek to the header: %w","messagePattern":"seek to the header: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"binding/golang/xdb/util.go","lineNumber":191,"sourceCode":"\treturn nil\n}\n\n// VerifyFromFile check Verify for details\nfunc VerifyFromFile(dbFile string) error {\n\thandle, err := os.OpenFile(dbFile, os.O_RDONLY, 0600)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"open xdb file `%s`: %w\", dbFile, err)\n\t}\n\tdefer handle.Close()\n\n\treturn Verify(handle)\n}\n\n// LoadHeader load the header info from the specified handle\nfunc LoadHeader(handle io.ReadSeeker) (*Header, error) {\n\t_, err := handle.Seek(0, 0)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"seek to the header: %w\", err)\n\t}\n\n\tvar buff = make([]byte, HeaderInfoLength)\n\trLen, err := handle.Read(buff)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\tif rLen != len(buff) {\n\t\treturn nil, fmt.Errorf(\"incomplete read: readed bytes should be %d\", len(buff))\n\t}\n\n\treturn NewHeader(buff)\n}\n\n// LoadHeaderFromFile load header info from the specified db file path\nfunc LoadHeaderFromFile(dbFile string) (*Header, error) {\n\thandle, err := os.OpenFile(dbFile, os.O_RDONLY, 0600)","sourceCodeStart":173,"sourceCodeEnd":209,"githubUrl":"https://github.com/lionsoul2014/ip2region/blob/c1a1fc7d5941760db3f8431dc05c48cf7f0e30a1/binding/golang/xdb/util.go#L173-L209","documentation":"LoadHeader seeks the given io.ReadSeeker to offset 0 before reading the 256-byte xdb header. If the Seek(0,0) call fails (e.g. the handle is not seekable or an underlying read error occurs), the error is wrapped as 'seek to the header: %w'. It signals that the header could not be located, not that the header content is invalid.","triggerScenarios":"Calling LoadHeader (directly or via LoadHeaderFromFile, Verify, or Searcher creation) with a handle whose Seek to 0 fails — typically a non-seekable reader such as an HTTP response body, a pipe, os.Stdin, or a bytes.Reader already consumed without rewind support.","commonSituations":"Passing a network stream or io.Pipe to LoadHeader instead of an *os.File or bytes.Reader; a corrupted or closed file descriptor underlying the handle; wrapping a file in a reader that does not implement io.Seeker.","solutions":["Pass an io.ReadSeeker such as *os.File, bytes.Reader, or bytes.Buffer — never a pipe or network stream.","Load the whole content with LoadContentFromFile / LoadContentFromFS instead of streaming a non-seekable source.","Unwrap the wrapped error with errors.Unwrap or errors.As to see the underlying cause and fix it."],"exampleFix":"// before\nheader, err := xdb.LoadHeader(httpResp.Body) // Body is not a ReadSeeker\n// after\nbody, _ := io.ReadAll(httpResp.Body)\nheader, err := xdb.LoadHeader(bytes.NewReader(body)","handlingStrategy":"type-guard","validationCode":"func isSeekable(r io.Reader) bool {\n    _, ok := r.(io.Seeker)\n    return ok\n}\n// call site: if !isSeekable(myReader) { ... load via LoadContentFromBuff instead ... }","typeGuard":"func asReadSeeker(r io.Reader) (io.ReadSeeker, bool) {\n    s, ok := r.(io.ReadSeeker)\n    return s, ok\n}","tryCatchPattern":"header, err := xdb.LoadHeader(handle)\nif err != nil {\n    var seekErr *fs.PathError\n    if errors.As(err, &seekErr) {\n        // handle is not seekable; fall back to buffering\n    }\n    return fmt.Errorf(\"header load failed: %w\", err)\n}","preventionTips":["Only pass io.ReadSeeker types (*os.File, bytes.Reader, bytes.Buffer) to Load* functions","For network/stream sources, buffer fully with io.ReadAll then wrap with bytes.NewReader","Prefer LoadContentFromFile/LoadContentFromFS over manual handle construction"],"tags":["golang","io","seek","xdb"],"backgroundTag":"seek-failed-nonseekable-stream","analyzedSha":"c1a1fc7d5941760db3f8431dc05c48cf7f0e30a1","analyzedAt":"2026-09-02T16:58:39.988Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T21:17:11.164Z"}