{"record":{"id":"5816981883af70eb","repo":"lionsoul2014/ip2region","slug":"invalid-content-buffer-d-bytes-d-expected","errorCode":null,"errorMessage":"invalid content buffer: %d bytes, %d expected","messagePattern":"invalid content buffer: (.+?) bytes, (.+?) expected","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"binding/golang/xdb/util.go","lineNumber":226,"sourceCode":"func LoadHeaderFromFile(dbFile string) (*Header, error) {\n\thandle, err := os.OpenFile(dbFile, os.O_RDONLY, 0600)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"open xdb file `%s`: %w\", dbFile, err)\n\t}\n\tdefer handle.Close()\n\n\theader, err := LoadHeader(handle)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\treturn header, nil\n}\n\n// LoadHeaderFromBuff wrap the header info from the content buffer\nfunc LoadHeaderFromBuff(cBuff []byte) (*Header, error) {\n\tif len(cBuff) < HeaderInfoLength {\n\t\treturn nil, fmt.Errorf(\"invalid content buffer: %d bytes, %d expected\", len(cBuff), HeaderInfoLength)\n\t}\n\n\treturn NewHeader(cBuff[0:HeaderInfoLength])\n}\n\n// LoadVectorIndex util function to load the vector index from the specified file handle\nfunc LoadVectorIndex(handle io.ReadSeeker) ([]byte, error) {\n\t// load all the vector index block\n\t_, err := handle.Seek(HeaderInfoLength, 0)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"seek to vector index: %w\", err)\n\t}\n\n\tvar buff = make([]byte, VectorIndexRows*VectorIndexCols*VectorIndexSize)\n\trLen, err := handle.Read(buff)\n\tif err != nil {\n\t\treturn nil, err\n\t}","sourceCodeStart":208,"sourceCodeEnd":244,"githubUrl":"https://github.com/lionsoul2014/ip2region/blob/c1a1fc7d5941760db3f8431dc05c48cf7f0e30a1/binding/golang/xdb/util.go#L208-L244","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nbuff, _ := os.ReadFile(p)\nheader, err := xdb.LoadHeaderFromBuff(buff[:100])\n// after\nbuff, _ := os.ReadFile(p)\nif len(buff) < xdb.HeaderInfoLength {\n    return fmt.Errorf(\"xdb too small: %d\", len(buff))\n}\nheader, err := xdb.LoadHeaderFromBuff(buff)","handlingStrategy":"validation","validationCode":"if len(cBuff) < xdb.HeaderInfoLength {\n    return fmt.Errorf(\"need %d bytes for header, got %d\", xdb.HeaderInfoLength, len(cBuff))\n}","typeGuard":"func isValidHeaderBuffer(buf []byte) bool {\n    return len(buf) >= xdb.HeaderInfoLength\n}","tryCatchPattern":"header, err := xdb.LoadHeaderFromBuff(cBuff)\nif err != nil {\n    if strings.Contains(err.Error(), \"invalid content buffer\") {\n        return fmt.Errorf(\"buffer too short (%d bytes)\", len(cBuff))\n    }\n    return err\n}","preventionTips":["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"],"tags":["golang","validation","buffer","input-validation","xdb"],"backgroundTag":"buffer-too-small","analyzedSha":"c1a1fc7d5941760db3f8431dc05c48cf7f0e30a1","analyzedAt":"2026-09-02T16:58:39.988Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T21:17:11.164Z"}