lionsoul2014/ip2region · error
invalid content buffer: %d bytes, %d expected
Error message
invalid content buffer: %d bytes, %d expected
What it means
LoadHeaderFromBuff requires at least HeaderInfoLength (256) bytes because the header struct is fixed-size. If the supplied buffer is shorter, the function refuses to slice it and returns this error naming the actual and required byte counts. It is a defensive length check against parsing garbage.
Source
Thrown at binding/golang/xdb/util.go:226
func LoadHeaderFromFile(dbFile string) (*Header, error) {
handle, err := os.OpenFile(dbFile, os.O_RDONLY, 0600)
if err != nil {
return nil, fmt.Errorf("open xdb file `%s`: %w", dbFile, err)
}
defer handle.Close()
header, err := LoadHeader(handle)
if err != nil {
return nil, err
}
return header, nil
}
// LoadHeaderFromBuff wrap the header info from the content buffer
func LoadHeaderFromBuff(cBuff []byte) (*Header, error) {
if len(cBuff) < HeaderInfoLength {
return nil, fmt.Errorf("invalid content buffer: %d bytes, %d expected", len(cBuff), HeaderInfoLength)
}
return NewHeader(cBuff[0:HeaderInfoLength])
}
// LoadVectorIndex util function to load the vector index from the specified file handle
func LoadVectorIndex(handle io.ReadSeeker) ([]byte, error) {
// load all the vector index block
_, err := handle.Seek(HeaderInfoLength, 0)
if err != nil {
return nil, fmt.Errorf("seek to vector index: %w", err)
}
var buff = make([]byte, VectorIndexRows*VectorIndexCols*VectorIndexSize)
rLen, err := handle.Read(buff)
if err != nil {
return nil, err
}View on GitHub (pinned to c1a1fc7d59)
Solutions
- Ensure the buffer is at least xdb.HeaderInfoLength bytes: len(cBuff) >= HeaderInfoLength before calling.
- If you have the full file content, pass the whole buffer or cBuff[0:HeaderInfoLength].
- Re-load the source file completely with LoadContentFromFile if it was truncated.
Example fix
// before
buff, _ := os.ReadFile(p)
header, err := xdb.LoadHeaderFromBuff(buff[:100])
// after
buff, _ := os.ReadFile(p)
if len(buff) < xdb.HeaderInfoLength {
return fmt.Errorf("xdb too small: %d", len(buff))
}
header, err := xdb.LoadHeaderFromBuff(buff) Defensive patterns
Strategy: validation
Validate before calling
if len(cBuff) < xdb.HeaderInfoLength {
return fmt.Errorf("need %d bytes for header, got %d", xdb.HeaderInfoLength, len(cBuff))
} Type guard
func isValidHeaderBuffer(buf []byte) bool {
return len(buf) >= xdb.HeaderInfoLength
} Try / catch
header, err := xdb.LoadHeaderFromBuff(cBuff)
if err != nil {
if strings.Contains(err.Error(), "invalid content buffer") {
return fmt.Errorf("buffer too short (%d bytes)", len(cBuff))
}
return err
} Prevention
- Check len(cBuff) >= HeaderInfoLength before slicing/parsing
- Pass full file content, not arbitrary slices, when loading header from buffer
- Validate embedded/downloaded assets before parsing
When it happens
Trigger: Calling LoadHeaderFromBuff with a byte slice shorter than 256 bytes — e.g. passing a whole small file's content when the file is truncated, or slicing the wrong range of a loaded xdb buffer.
Common situations: Unit tests deliberately passing short buffers; reading only part of an xdb file into memory; mistaking the header length constant for a smaller value; a corrupt/empty embedded asset.
Related errors
- invalid structure version {}
- invalid input buffer
- read vector index block at %d: %w
- read segment index at %d: %w
- read region at %d: %w
AI-assisted analysis of lionsoul2014/ip2region@c1a1fc7d59 (2026-09-02).
Data as JSON: /api/errors/5816981883af70eb.
Report an issue: GitHub.