lionsoul2014/ip2region · error

invalid input buffer

Error message

invalid input buffer

What it means

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.

Source

Thrown at binding/golang/xdb/header.go:65

// --- Header define

type Header struct {
	// data []byte
	Version       uint16
	IndexPolicy   IndexPolicy
	CreatedAt     uint32
	StartIndexPtr uint32
	EndIndexPtr   uint32

	// since IPv6 supporting
	IPVersion       int
	RuntimePtrBytes int
}

func NewHeader(input []byte) (*Header, error) {
	if len(input) < 20 {
		return nil, fmt.Errorf("invalid input buffer")
	}

	return &Header{
		Version:       binary.LittleEndian.Uint16(input[0:]),
		IndexPolicy:   IndexPolicy(binary.LittleEndian.Uint16(input[2:])),
		CreatedAt:     binary.LittleEndian.Uint32(input[4:]),
		StartIndexPtr: binary.LittleEndian.Uint32(input[8:]),
		EndIndexPtr:   binary.LittleEndian.Uint32(input[12:]),

		IPVersion:       int(binary.LittleEndian.Uint16(input[16:])),
		RuntimePtrBytes: int(binary.LittleEndian.Uint16(input[18:])),
	}, nil
}

func (h *Header) String() string {
	return fmt.Sprintf(
		"{version:%d, index_policy:%d, created_at:%d, start_index_ptr:%d, end_index_ptr:%d, ip_version:%d, runtime_ptr_bytes:%d}",
		h.Version, h.IndexPolicy, h.CreatedAt, h.StartIndexPtr, h.EndIndexPtr, h.IPVersion, h.RuntimePtrBytes,

View on GitHub (pinned to c1a1fc7d59)

Solutions

  1. Verify the xdb file exists and its size is >= 20 bytes before loading (os.Stat).
  2. Re-download / regenerate the xdb file; it is likely truncated or corrupted.
  3. If using LoadHeaderFromBuff, confirm the slice starts at offset 0 of the file and contains the full header (at least 20 bytes).
  4. Confirm you are passing an xdb data file, not another file type.

Example fix

// before
header, _ := xdb.LoadHeaderFromBuff(buff)
// after
if len(buff) < 20 {
    return fmt.Errorf("xdb buffer too small: %d bytes", len(buff))
}
header, err := xdb.LoadHeaderFromBuff(buff)
if err != nil {
    return err
}
Defensive patterns

Strategy: validation

Validate before calling

fi, err := os.Stat(xdbPath)
if err != nil { return err }
if fi.Size() < 20 {
    return fmt.Errorf("xdb file too small: %d bytes", fi.Size())
}

Type guard

func isValidHeaderInput(b []byte) bool { return len(b) >= 20 }

Try / catch

header, err := xdb.LoadHeaderFromBuff(buff)
if err != nil {
    return fmt.Errorf("xdb header load failed (truncated file?): %w", err)
}

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Related errors


AI-assisted analysis of lionsoul2014/ip2region@c1a1fc7d59 (2026-09-02). Data as JSON: /api/errors/d8b3c59cbd3c2ceb. Report an issue: GitHub.