{"record":{"id":"efc7c643fbd9e4ee","repo":"gohugoio/hugo","slug":"sequence-bounds-out-of-range-nv","errorCode":null,"errorMessage":"sequence bounds out of range [{nv}:]","messagePattern":"sequence bounds out of range \\[(.+?):\\]","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"tpl/collections/collections.go","lineNumber":76,"sourceCode":"\tloc      *time.Location\n\tsortComp *compare.Namespace\n\tdCache   *hmaps.Cache[dKey, []int]\n\tdeps     *deps.Deps\n}\n\n// After returns all the items after the first n items in list l.\nfunc (ns *Namespace) After(n any, l any) (any, error) {\n\tif n == nil || l == nil {\n\t\treturn nil, errors.New(\"both limit and seq must be provided\")\n\t}\n\n\tnv, err := cast.ToIntE(n)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\tif nv < 0 {\n\t\treturn nil, errors.New(\"sequence bounds out of range [\" + cast.ToString(nv) + \":]\")\n\t}\n\n\tlv := reflect.ValueOf(l)\n\tlv, isNil := hreflect.Indirect(lv)\n\tif isNil {\n\t\treturn nil, errors.New(\"can't iterate over a nil value\")\n\t}\n\n\tswitch lv.Kind() {\n\tcase reflect.Array, reflect.Slice, reflect.String:\n\t\t// okay\n\tdefault:\n\t\treturn nil, errors.New(\"can't iterate over \" + reflect.ValueOf(l).Type().String())\n\t}\n\n\tif nv >= lv.Len() {\n\t\treturn lv.Slice(0, 0).Interface(), nil\n\t}","sourceCodeStart":58,"sourceCodeEnd":94,"githubUrl":"https://github.com/gohugoio/hugo/blob/52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2/tpl/collections/collections.go#L58-L94","documentation":"After (collections.go:75-77) rejects a negative offset because Go slice bounds cannot be negative. The offending numeric value is interpolated into the message.","triggerScenarios":"{{ after -1 .Pages }} or any computed offset that goes below zero (e.g. {{ after (sub $a $b) .Pages }} where b > a).","commonSituations":"Subtracting too much when deriving an offset; user-controlled param passed through without clamping; off-by-one in pagination math.","solutions":["Clamp the offset to >= 0: {{ after (math.Max 0 $n) .Pages }}","Guard: {{ if ge $n 0 }}{{ after $n .Pages }}{{ end }}","Use (cond (lt $n 0) 0 $n) to coerce"],"exampleFix":"// before\n{{ after (sub $a $b) .Pages }}\n// after\n{{ after (math.Max 0 (sub $a $b)) .Pages }}","handlingStrategy":"validation","validationCode":"{{ $n := math.Max 0 $n }}\n{{ after $n .Pages }}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Clamp computed offsets with math.Max 0 before calling after","Validate user-supplied offsets are >= 0","Guard subtraction results that can go negative"],"tags":["hugo","go-template","collections","validation","bounds-check"],"backgroundTag":null,"analyzedSha":"52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2","analyzedAt":"2026-08-09T21:49:36.660Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}