{"record":{"id":"9d96021819efaf84","repo":"milvus-io/milvus","slug":"index-out-of-range","errorCode":null,"errorMessage":"index out of range","messagePattern":"index out of range","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"client/column/generic_base.go","lineNumber":313,"sourceCode":"\t}\n\treturn c.values[idx], nil\n}\n\nfunc (c *genericColumnBase[T]) valueIndex(idx int) int {\n\tif !c.nullable || c.sparseMode {\n\t\treturn idx\n\t}\n\treturn c.indexMapping[idx]\n}\n\nfunc (c *genericColumnBase[T]) Data() []T {\n\treturn c.values\n}\n\nfunc (c *genericColumnBase[T]) MustValue(idx int) T {\n\tidx = c.valueIndex(idx)\n\tif idx < 0 || idx > c.Len() {\n\t\tpanic(\"index out of range\")\n\t}\n\treturn c.values[idx]\n}\n\nfunc (c *genericColumnBase[T]) AppendNull() error {\n\tif !c.nullable {\n\t\treturn errors.New(\"append null to not nullable column\")\n\t}\n\n\tc.validData = append(c.validData, false)\n\tif c.sparseMode {\n\t\tvar zero T\n\t\tc.values = append(c.values, zero)\n\t} else {\n\t\tc.indexMapping = append(c.indexMapping, -1)\n\t}\n\treturn nil\n}","sourceCodeStart":295,"sourceCodeEnd":331,"githubUrl":"https://github.com/milvus-io/milvus/blob/b43a76673a9fe5f01f158979731dc8fd542df81f/client/column/generic_base.go#L295-L331","documentation":"This panic comes from the Milvus Go client's column accessor MustValue (client/column/generic_base.go:313). MustValue is the unchecked variant of Value(idx): instead of returning an error it panics when the resolved index is not usable. The index is first translated by valueIndex — for a nullable, non-sparse (dense) column this maps the row position through c.indexMapping, which stores -1 for rows that are null — and the explicit guard `idx < 0 || idx > c.Len()` then trips on both out-of-bounds row numbers and on null rows. Note the library intends this: use Value(idx) for the error-returning path.","triggerScenarios":"1) Calling column.MustValue(i) with i >= column.Len() or i < 0. 2) Calling MustValue(i) on a row that was appended with AppendNull() on a nullable dense column: valueIndex returns indexMapping[i] == -1, which fails the `idx < 0` check even though i is a valid row number. 3) Reusing an index obtained from another column of different length (e.g. iterating with the result-set row count against a sliced column).","commonSituations":"Looping `for i := 0; i <= col.Len(); i++` (off-by-one <= instead of <); reading fields of a row where some fields are NULL while using MustValue instead of Value; mixing indices between a nullable column and a non-nullable column in the same row; upgrading code from MustValue on data known to contain nulls after enabling nullable schema fields.","solutions":["Switch from MustValue(idx) to Value(idx), which performs the same valueIndex translation plus rangeCheck and returns an error instead of panicking","If the row may be null, check the null status first (e.g. column valid data / GetAsNull or the entity's IsNull-style check) and skip or default null rows before calling MustValue","Fix off-by-one loops: iterate with `i < col.Len()`, never `i <=`","Verify idx was computed against the same column object (or its Slice result) you are indexing"],"exampleFix":"// before\nfor i := 0; i <= rows; i++ { // off-by-one, or row i may be NULL\n    v := col.MustValue(i)\n}\n\n// after\nfor i := 0; i < col.Len(); i++ {\n    v, err := col.Value(i)\n    if err != nil {\n        return err // covers out-of-range AND null-mapped (-1) indices\n    }\n    _ = v\n}","handlingStrategy":"validation","validationCode":"// Guard before MustValue: bounds + null-slot check.\n// For nullable dense columns a null row maps to -1 internally,\n// so bounds alone are not enough — prefer Value() which returns an error.\nfunc safeValue[T any](c interface {\n    Value(idx int) (T, error)\n    Len() int\n}, idx int) (T, error) {\n    if idx < 0 || idx >= c.Len() {\n        var z T\n        return z, fmt.Errorf(\"index %d out of range (len %d)\", idx, c.Len())\n    }\n    return c.Value(idx) // error-returning path, no panic\n}","typeGuard":null,"tryCatchPattern":"// Go has no try/catch; if you must call MustValue on untrusted indices,\n// wrap with recover and convert to an error.\nfunc mustValueSafe[T any](c *column.genericColumnBase[T], idx int) (v T, err error) {\n    defer func() {\n        if r := recover(); r != nil {\n            err = fmt.Errorf(\"column access failed: %v\", r)\n        }\n    }()\n    return c.MustValue(idx), nil\n}","preventionTips":["Prefer the Value/GetAsX family (error-returning) over MustValue wherever input indices or nullability are not fully controlled","Never iterate with `i <= col.Len()`","For nullable columns, handle null rows explicitly (skip or default) before any Must* accessor","Compute indices from the same column you index, especially after Slice()"],"tags":["go","milvus-client","panic","index-out-of-range","nullable","column-api"],"backgroundTag":null,"analyzedSha":"b43a76673a9fe5f01f158979731dc8fd542df81f","analyzedAt":"2026-08-15T10:29:28.408Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}