{"record":{"id":"8312787090cd3014","repo":"gohugoio/hugo","slug":"index-out-of-range-d","errorCode":null,"errorMessage":"index out of range: %d","messagePattern":"index out of range: (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"tpl/internal/go_templates/texttemplate/funcs.go","lineNumber":188,"sourceCode":"\t}\n\treturn false\n}\n\n// indexArg checks if a reflect.Value can be used as an index, and converts it to int if possible.\nfunc indexArg(index reflect.Value, cap int) (int, error) {\n\tvar x int64\n\tswitch index.Kind() {\n\tcase reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:\n\t\tx = index.Int()\n\tcase reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:\n\t\tx = int64(index.Uint())\n\tcase reflect.Invalid:\n\t\treturn 0, fmt.Errorf(\"cannot index slice/array with nil\")\n\tdefault:\n\t\treturn 0, fmt.Errorf(\"cannot index slice/array with type %s\", index.Type())\n\t}\n\tif x < 0 || int(x) < 0 || int(x) > cap {\n\t\treturn 0, fmt.Errorf(\"index out of range: %d\", x)\n\t}\n\treturn int(x), nil\n}\n\n// Indexing.\n\n// index returns the result of indexing its first argument by the following\n// arguments. Thus \"index x 1 2 3\" is, in Go syntax, x[1][2][3]. Each\n// indexed item must be a map, slice, or array.\nfunc index(item reflect.Value, indexes ...reflect.Value) (reflect.Value, error) {\n\titem = indirectInterface(item)\n\tif !item.IsValid() {\n\t\treturn reflect.Value{}, fmt.Errorf(\"index of untyped nil\")\n\t}\n\tfor _, index := range indexes {\n\t\tindex = indirectInterface(index)\n\t\tvar isNil bool\n\t\tif item, isNil = indirect(item); isNil {","sourceCodeStart":170,"sourceCodeEnd":206,"githubUrl":"https://github.com/gohugoio/hugo/blob/52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2/tpl/internal/go_templates/texttemplate/funcs.go#L170-L206","documentation":"Returned by indexArg when the integer index is negative or exceeds the slice/array/string capacity (cap). Bounds are checked against cap (not len) so the message can fire on cap-relative out-of-range for slice expressions too. The %d is the offending index value.","triggerScenarios":"Using {{index .Slice -1}} or {{index .Slice (sub (len .Slice) 1)}} that evaluates out of range; an index computed from user input that exceeds len; slicing past the end via the slice builtin.","commonSituations":"Off-by-one in computed indices; user-supplied page/offset numbers used unchecked; empty slice indexed at 0; negative index expected to count from end (Go does not support that).","solutions":["Clamp/guard the index: {{if and (ge $i 0) (lt $i (len .Slice))}}{{index .Slice $i}}{{end}}.","Validate ranges in the data layer before passing to the template.","Use a helper func that bounds-checks and returns a default on out-of-range.","For last-element access, compute len-1 explicitly and guard against empty slices."],"exampleFix":"// before\n{{index .Items (sub (len .Items) 1)}} // panics/errors when empty\n\n// after\n{{if .Items}}{{index .Items (sub (len .Items) 1)}}{{end}}","handlingStrategy":"validation","validationCode":"// register a bounds-safe accessor\ntemplate.Funcs(template.FuncMap{\n    \"at\": func(seq any, i int) any {\n        v := reflect.ValueOf(seq)\n        if i < 0 || i >= v.Len() { return nil }\n        return v.Index(i).Interface()\n    },\n})","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Guard indices: {{if and (ge $i 0) (lt $i (len .Slice))}}{{index .Slice $i}}{{end}}.","Register a bounds-checked helper returning a default out of range.","Validate user-supplied offsets before render."],"tags":["go","text-template","index","bounds","out-of-range"],"backgroundTag":null,"analyzedSha":"52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2","analyzedAt":"2026-08-09T21:49:36.660Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}