{"record":{"id":"a8d5e9688e89cd8c","repo":"mikefarah/yq","slug":"cannot-repeat-string-by-a-negative-number-v","errorCode":null,"errorMessage":"cannot repeat string by a negative number (%v)","messagePattern":"cannot repeat string by a negative number \\((.+?)\\)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/yqlib/operator_multiply.go","lineNumber":157,"sourceCode":"\nfunc repeatString(lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error) {\n\tvar stringNode *CandidateNode\n\tvar intNode *CandidateNode\n\tif lhs.Tag == \"!!str\" {\n\t\tstringNode = lhs\n\t\tintNode = rhs\n\t} else {\n\t\tstringNode = rhs\n\t\tintNode = lhs\n\t}\n\ttarget := lhs.CopyWithoutContent()\n\ttarget.UpdateAttributesFrom(stringNode, assignPreferences{})\n\n\tcount, err := parseInt(intNode.Value)\n\tif err != nil {\n\t\treturn nil, err\n\t} else if count < 0 {\n\t\treturn nil, fmt.Errorf(\"cannot repeat string by a negative number (%v)\", count)\n\t}\n\tmaxResultLen := 10 * 1024 * 1024 // 10 MiB\n\tif count > 0 && len(stringNode.Value) > maxResultLen/count {\n\t\treturn nil, fmt.Errorf(\"result of repeating string (%v bytes) by %v would exceed %v bytes\", len(stringNode.Value), count, maxResultLen)\n\t}\n\ttarget.Value = strings.Repeat(stringNode.Value, count)\n\n\treturn target, nil\n}\n\nfunc mergeObjects(d *dataTreeNavigator, context Context, lhs *CandidateNode, rhs *CandidateNode, preferences multiplyPreferences) (*CandidateNode, error) {\n\tvar results = list.New()\n\n\t// only need to recurse the array if we are doing a deep merge\n\tprefs := recursiveDescentPreferences{RecurseArray: preferences.DeepMergeArrays,\n\t\tTraversePreferences: traversePreferences{DontFollowAlias: true, IncludeMapKeys: true, ExactKeyMatch: true}}\n\tlog.Debugf(\"merge - preferences.DeepMergeArrays %v\", preferences.DeepMergeArrays)\n\tlog.Debugf(\"merge - preferences.AppendArrays %v\", preferences.AppendArrays)","sourceCodeStart":139,"sourceCodeEnd":175,"githubUrl":"https://github.com/mikefarah/yq/blob/8b5af0694bb82b41d4ae180fac9972029066f90a/pkg/yqlib/operator_multiply.go#L139-L175","documentation":"When using '*' to repeat a string ('\"ab\" * 3'), yq parses the integer operand and rejects negative counts because strings.Repeat semantics for negative counts would yield an empty/meaningless result. The library treats a negative repeat count as a user error.","triggerScenarios":"An expression like '\"abc\" * -1' or '\"x\" * (0 - 5)', or a computed count that evaluates to a negative int, e.g. '\"ab\" * (1 - 3)'.","commonSituations":"Subtraction-based counters that go negative (e.g. repeat = desired - current when current > desired), config values read from files that are negative, sign errors in expressions.","solutions":["Clamp the count before repeating: '\"ab\" * ([0, (.n)] | max)' or guard with select(.n >= 0)","Fix the source value producing the negative count","If an empty result is desired, use count 0 instead of a negative number"],"exampleFix":"// before\nyq '\"-\" * (.n - 10)' f.yml   // .n=8 -> cannot repeat string by a negative number (-2)\n// after\nyq '\"-\" * ([0, .n - 10] | max)' f.yml","handlingStrategy":"validation","validationCode":"yq 'select(.n >= 0) | \"ab\" * .n' f.yml","typeGuard":null,"tryCatchPattern":"// shell\nout=$(yq '\"ab\" * .n' f.yml 2>&1) || { echo \"bad count: $out\"; exit 1; }","preventionTips":["Clamp computed counts with '[0, .n] | max'","Validate numeric inputs from external sources before repeat expressions"],"tags":["yq","string-repetition","negative-number","validation"],"backgroundTag":"invalid-argument-value","analyzedSha":"8b5af0694bb82b41d4ae180fac9972029066f90a","analyzedAt":"2026-09-05T10:57:22.766Z","contentChangedAt":"2026-09-05T10:57:22.766Z","schemaVersion":2},"datasetVersion":"2026-09-12T17:17:11.597Z"}