{"record":{"id":"d8b3c59cbd3c2ceb","repo":"lionsoul2014/ip2region","slug":"invalid-input-buffer","errorCode":null,"errorMessage":"invalid input buffer","messagePattern":"invalid input buffer","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"binding/golang/xdb/header.go","lineNumber":65,"sourceCode":"\n// --- Header define\n\ntype Header struct {\n\t// data []byte\n\tVersion       uint16\n\tIndexPolicy   IndexPolicy\n\tCreatedAt     uint32\n\tStartIndexPtr uint32\n\tEndIndexPtr   uint32\n\n\t// since IPv6 supporting\n\tIPVersion       int\n\tRuntimePtrBytes int\n}\n\nfunc NewHeader(input []byte) (*Header, error) {\n\tif len(input) < 20 {\n\t\treturn nil, fmt.Errorf(\"invalid input buffer\")\n\t}\n\n\treturn &Header{\n\t\tVersion:       binary.LittleEndian.Uint16(input[0:]),\n\t\tIndexPolicy:   IndexPolicy(binary.LittleEndian.Uint16(input[2:])),\n\t\tCreatedAt:     binary.LittleEndian.Uint32(input[4:]),\n\t\tStartIndexPtr: binary.LittleEndian.Uint32(input[8:]),\n\t\tEndIndexPtr:   binary.LittleEndian.Uint32(input[12:]),\n\n\t\tIPVersion:       int(binary.LittleEndian.Uint16(input[16:])),\n\t\tRuntimePtrBytes: int(binary.LittleEndian.Uint16(input[18:])),\n\t}, nil\n}\n\nfunc (h *Header) String() string {\n\treturn fmt.Sprintf(\n\t\t\"{version:%d, index_policy:%d, created_at:%d, start_index_ptr:%d, end_index_ptr:%d, ip_version:%d, runtime_ptr_bytes:%d}\",\n\t\th.Version, h.IndexPolicy, h.CreatedAt, h.StartIndexPtr, h.EndIndexPtr, h.IPVersion, h.RuntimePtrBytes,","sourceCodeStart":47,"sourceCodeEnd":83,"githubUrl":"https://github.com/lionsoul2014/ip2region/blob/c1a1fc7d5941760db3f8431dc05c48cf7f0e30a1/binding/golang/xdb/header.go#L47-L83","documentation":"NewHeader parses the xDB file header from a 20+ byte buffer. The library returns this error when the supplied input is shorter than the fixed 20-byte header layout (version, index policy, created-at, start/end index pointers), so parsing would read out of bounds.","triggerScenarios":"Calling LoadHeader with a file whose first read returned fewer than 20 bytes (empty or truncated xdb file), or LoadHeaderFromBuff with a byte slice smaller than 20 bytes (wrong offset, corrupted download, or a non-xdb file passed in).","commonSituations":"A partially downloaded or truncated ip2region.xdb file; passing an empty slice when loading from a pre-read buffer; pointing the loader at a config file or a v4 xdb consumed by v6 tooling with a short header.","solutions":["Verify the xdb file exists and its size is >= 20 bytes before loading (os.Stat).","Re-download / regenerate the xdb file; it is likely truncated or corrupted.","If using LoadHeaderFromBuff, confirm the slice starts at offset 0 of the file and contains the full header (at least 20 bytes).","Confirm you are passing an xdb data file, not another file type."],"exampleFix":"// before\nheader, _ := xdb.LoadHeaderFromBuff(buff)\n// after\nif len(buff) < 20 {\n    return fmt.Errorf(\"xdb buffer too small: %d bytes\", len(buff))\n}\nheader, err := xdb.LoadHeaderFromBuff(buff)\nif err != nil {\n    return err\n}","handlingStrategy":"validation","validationCode":"fi, err := os.Stat(xdbPath)\nif err != nil { return err }\nif fi.Size() < 20 {\n    return fmt.Errorf(\"xdb file too small: %d bytes\", fi.Size())\n}","typeGuard":"func isValidHeaderInput(b []byte) bool { return len(b) >= 20 }","tryCatchPattern":"header, err := xdb.LoadHeaderFromBuff(buff)\nif err != nil {\n    return fmt.Errorf(\"xdb header load failed (truncated file?): %w\", err)\n}","preventionTips":["Stat the xdb file before loading and check size >= 20 bytes.","Re-download the xdb after any interrupted download.","Never pass a manually sliced buffer; pass full file content.","Validate the loaded header's Length against the file size."],"tags":["golang","xdb","invalid-input","corrupt-file"],"backgroundTag":"invalid-xdb-header","analyzedSha":"c1a1fc7d5941760db3f8431dc05c48cf7f0e30a1","analyzedAt":"2026-09-02T16:58:39.988Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T21:17:11.164Z"}