{"record":{"id":"7cc0d04923ffeb88","repo":"golang/go","slug":"bytes-negative-repeat-count","errorCode":null,"errorMessage":"bytes: negative Repeat count","messagePattern":"bytes: negative Repeat count","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/bytes/bytes.go","lineNumber":639,"sourceCode":"//\n// Note that this comment is not part of the doc comment.\n//\n//go:linkname Repeat\n\n// Repeat returns a new byte slice consisting of count copies of b.\n//\n// It panics if count is negative or if the result of (len(b) * count)\n// overflows.\nfunc Repeat(b []byte, count int) []byte {\n\tif count == 0 {\n\t\treturn []byte{}\n\t}\n\n\t// Since we cannot return an error on overflow,\n\t// we should panic if the repeat will generate an overflow.\n\t// See golang.org/issue/16237.\n\tif count < 0 {\n\t\tpanic(\"bytes: negative Repeat count\")\n\t}\n\thi, lo := bits.Mul(uint(len(b)), uint(count))\n\tif hi > 0 || lo > uint(maxInt) {\n\t\tpanic(\"bytes: Repeat output length overflow\")\n\t}\n\tn := int(lo) // lo = len(b) * count\n\n\tif len(b) == 0 {\n\t\treturn []byte{}\n\t}\n\n\t// Past a certain chunk size it is counterproductive to use\n\t// larger chunks as the source of the write, as when the source\n\t// is too large we are basically just thrashing the CPU D-cache.\n\t// So if the result length is larger than an empirically-found\n\t// limit (8KB), we stop growing the source string once the limit\n\t// is reached and keep reusing the same source string - that\n\t// should therefore be always resident in the L1 cache - until we","sourceCodeStart":621,"sourceCodeEnd":657,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/bytes/bytes.go#L621-L657","documentation":"bytes.Repeat returns count copies of b. It panics with \"bytes: negative Repeat count\" when count < 0, before any multiplication. This is a pure argument-validation panic; count == 0 returns an empty slice and is explicitly allowed, and the subsequent multiplication overflow is a separate panic (error 1427).","triggerScenarios":"Calling bytes.Repeat(b, count) with count < 0; computing count as a subtraction (a - b) that goes negative; forwarding an unvalidated signed length from a parsed field; off-by-one where count = desiredLen/len(b) - 1 underflows when len(b) > desiredLen.","commonSituations":"Generating padding/indentation strings where the padding width is derived from a column that can be negative; repeating a pattern based on a user-supplied multiplier that is not range-checked; computing repetitions from a target length and a divisor, with the divisor larger than the target.","solutions":["Validate count >= 0 before calling Repeat (decide whether 0 is acceptable for your use case).","If count is computed as a difference, clamp the floor at 0.","Range-check any externally sourced multiplier against both 0 and a sane upper bound."],"exampleFix":"// before\npad := bytes.Repeat([]byte(\" \"), width - cur) // negative when width < cur\n\n// after\nn := width - cur\nif n < 0 {\n    n = 0\n}\npad := bytes.Repeat([]byte(\" \"), n)","handlingStrategy":"validation","validationCode":"// Repeat only with a non-negative count.\nfunc safeRepeat(b []byte, count int) ([]byte, error) {\n    if count < 0 {\n        return nil, fmt.Errorf(\"repeat count %d must be >= 0\", count)\n    }\n    return bytes.Repeat(b, count), nil\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Range-check externally sourced multipliers before Repeat.","Clamp computed counts (e.g. width - cur) to a floor of 0.","Remember count == 0 is valid and returns an empty slice; only negative is rejected."],"tags":["bytes","repeat","validation","panic","go"],"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T06:17:24.410Z"}