{"record":{"id":"f0302bd3f506bbfb","repo":"gohugoio/hugo","slug":"the-maximum-requested-value-v-must-be-a-non-neg","errorCode":null,"errorMessage":"the maximum requested value (%v) must be a non-negative integer <= %d","messagePattern":"the maximum requested value \\((.+?)\\) must be a non-negative integer <= (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"tpl/collections/collections.go","lineNumber":568,"sourceCode":"//\n// Reference:\n//\n//\tJ. S. Vitter, \"An efficient algorithm for sequential random sampling,\" ACM Trans. Math. Softw., vol. 11, no. 1, pp. 37–57, 1985.\n//\tSee also: https://getkerf.wordpress.com/2016/03/30/the-best-algorithm-no-one-knows-about/\nfunc (ns *Namespace) D(seed, n, hi any) ([]int, error) {\n\tseedInt, err := cast.ToInt64E(seed)\n\tif err != nil || seedInt < 0 {\n\t\treturn nil, fmt.Errorf(\"the seed value (%v) must be a non-negative integer\", seed)\n\t}\n\n\tnInt, err := cast.ToIntE(n)\n\tif err != nil || nInt < 0 || nInt > maxSeqSize {\n\t\treturn nil, fmt.Errorf(\"the number of requested values (%v) must be a non-negative integer <= %d\", n, maxSeqSize)\n\t}\n\n\thiInt, err := cast.ToIntE(hi)\n\tif err != nil || hiInt < 0 || hiInt > maxSeqSize {\n\t\treturn nil, fmt.Errorf(\"the maximum requested value (%v) must be a non-negative integer <= %d\", hi, maxSeqSize)\n\t}\n\n\tif nInt == 0 || hiInt == 0 {\n\t\treturn []int{}, nil\n\t}\n\n\tkey := dKey{seed: uint64(seedInt), n: nInt, hi: hiInt}\n\n\tv, err := ns.dCache.GetOrCreate(key, func() ([]int, error) {\n\t\tif key.n > key.hi {\n\t\t\tresult := make([]int, key.hi)\n\t\t\tfor i := 0; i < key.hi; i++ {\n\t\t\t\tresult[i] = i\n\t\t\t}\n\t\t\treturn result, nil\n\t\t}\n\n\t\tprng := rand.New(rand.NewPCG(key.seed, 0))","sourceCodeStart":550,"sourceCodeEnd":586,"githubUrl":"https://github.com/gohugoio/hugo/blob/52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2/tpl/collections/collections.go#L550-L586","documentation":"Thrown by Namespace.D when the `hi` argument (exclusive upper bound of the sampling range [0, hi)) fails cast.ToIntE, is negative, or exceeds maxSeqSize (1,000,000). hi defines the population size; it must be a non-negative integer within the package safety ceiling.","triggerScenarios":"Calling `collections.D 1 3 -10`, `collections.D 1 3 1.5`, `collections.D 1 3 5000000`, or passing nil for hi. Guard: `cast.ToIntE(hi)` error OR hiInt < 0 OR hiInt > maxSeqSize.","commonSituations":"Author derives hi from a page-count that is negative (empty collection minus offset), passes a float, or intends a huge population that breaches the cap. Same shape as the n error but on the range bound.","solutions":["Pass a non-negative integer literal within [0, 1000000], e.g. `D 1 3 100`.","If hi is computed from a length, guard it: `{{ $hi := math.Max 0 (math.Min $len 1000000) }}`.","Ensure hi is an int and not nil.","If you need a larger population, reconsider the algorithm or precompute outside the template."],"exampleFix":"// before\n{{ $r := collections.D 1 3 (sub 0 5) }}\n// after\n{{ $r := collections.D 1 3 100 }}","handlingStrategy":"validation","validationCode":"const maxSeqSize = 1000000\nfunc validateDHi(hi any) (int, error) {\n    x, err := cast.ToIntE(hi)\n    if err != nil || x < 0 || x > maxSeqSize {\n        return 0, fmt.Errorf(\"hi must be in [0, %d], got %v\", maxSeqSize, hi)\n    }\n    return x, nil\n}","typeGuard":"func isValidDRange(v any) bool {\n    n, err := cast.ToIntE(v)\n    return err == nil && n >= 0 && n <= 1000000\n}","tryCatchPattern":null,"preventionTips":["Pass a non-negative integer for hi within the cap.","Clamp computed ranges (e.g. from len()) to [0, 1000000].","Ensure hi is int and non-nil.","Reconsider the approach if you need populations larger than 1,000,000."],"tags":["go","hugo","template","collections","random","validation","bounds-check"],"backgroundTag":null,"analyzedSha":"52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2","analyzedAt":"2026-08-09T21:49:36.660Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}