{"record":{"id":"bc319088e6d99852","repo":"gohugoio/hugo","slug":"the-number-of-requested-values-v-must-be-a-non","errorCode":null,"errorMessage":"the number of requested values (%v) must be a non-negative integer <= %d","messagePattern":"the number of requested values \\((.+?)\\) must be a non-negative integer <= (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"tpl/collections/collections.go","lineNumber":563,"sourceCode":"// Method D for sequential random sampling.\n//\n// If n > hi, it returns the full, sorted range [0, hi) of size hi.\n//\n// If n == 0 or hi == 0, it returns an empty slice.\n//\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","sourceCodeStart":545,"sourceCodeEnd":581,"githubUrl":"https://github.com/gohugoio/hugo/blob/52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2/tpl/collections/collections.go#L545-L581","documentation":"Thrown by Namespace.D when the `n` argument (number of requested random values) fails cast.ToIntE, is negative, or exceeds the maxSeqSize cap (1,000,000). n bounds how many unique samples are drawn from [0, hi); it must be a sane non-negative integer within the package's safety limit.","triggerScenarios":"Calling `collections.D 1 -3 10`, `collections.D 1 1.5 10`, `collections.D 1 2000000 10`, or passing nil for n. The guard is `cast.ToIntE(n)` error OR nInt < 0 OR nInt > maxSeqSize.","commonSituations":"Author passes a computed count that overflows or is negative due to subtraction, feeds a float from a params dict, or hits the 1,000,000 ceiling with a very large hi range. Also when n comes from user-supplied front matter.","solutions":["Pass a small non-negative integer literal within [0, 1000000], e.g. `D 1 5 100`.","If n is computed, clamp it: `{{ $n := math.Max 0 (math.Min $computed 1000000) }}`.","Ensure n is an int and not nil before the call.","Audit front matter / data files feeding n for numeric validity."],"exampleFix":"// before\n{{ $r := collections.D 1 -5 10 }}\n// after\n{{ $r := collections.D 1 5 10 }}","handlingStrategy":"validation","validationCode":"const maxSeqSize = 1000000\nfunc validateDN(n any) (int, error) {\n    x, err := cast.ToIntE(n)\n    if err != nil || x < 0 || x > maxSeqSize {\n        return 0, fmt.Errorf(\"n must be in [0, %d], got %v\", maxSeqSize, n)\n    }\n    return x, nil\n}","typeGuard":"func isValidDCount(v any) bool {\n    n, err := cast.ToIntE(v)\n    return err == nil && n >= 0 && n <= 1000000\n}","tryCatchPattern":null,"preventionTips":["Pass a small non-negative integer for n.","Clamp computed counts to [0, 1000000] before calling D.","Ensure n is int and non-nil.","Treat front-matter-supplied counts as untrusted; validate them."],"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-23T08:17:48.524Z"}