Skip to content

Commit

Permalink
Fixed multiply string by negative number panic #2211
Browse files Browse the repository at this point in the history
  • Loading branch information
mikefarah committed Dec 7, 2024
1 parent 5273715 commit 2201381
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pkg/yqlib/operator_multiply.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ func repeatString(lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error
count, err := parseInt(intNode.Value)
if err != nil {
return nil, err
} else if count < 0 {
return nil, fmt.Errorf("Cannot repeat string by a negative number (%v)", count)
}
target.Value = strings.Repeat(stringNode.Value, count)

Expand Down
7 changes: 7 additions & 0 deletions pkg/yqlib/operator_multiply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,13 @@ var multiplyOperatorScenarios = []expressionScenario{
fmt.Sprintf("D0, P[], (!!str)::%s\n", strings.Repeat("banana", 4)),
},
},
{
description: "Multiply string X by negative int",
skipDoc: true,
document: `n: -4`,
expression: `"banana" * .n`,
expectedError: "Cannot repeat string by a negative number (-4)",
},
{
description: "Multiply int node X string",
document: `n: 4
Expand Down

0 comments on commit 2201381

Please sign in to comment.