Skip to content

Commit 2201381

Browse files
committed
Fixed multiply string by negative number panic #2211
1 parent 5273715 commit 2201381

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

pkg/yqlib/operator_multiply.go

+2
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ func repeatString(lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error
153153
count, err := parseInt(intNode.Value)
154154
if err != nil {
155155
return nil, err
156+
} else if count < 0 {
157+
return nil, fmt.Errorf("Cannot repeat string by a negative number (%v)", count)
156158
}
157159
target.Value = strings.Repeat(stringNode.Value, count)
158160

pkg/yqlib/operator_multiply_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,13 @@ var multiplyOperatorScenarios = []expressionScenario{
201201
fmt.Sprintf("D0, P[], (!!str)::%s\n", strings.Repeat("banana", 4)),
202202
},
203203
},
204+
{
205+
description: "Multiply string X by negative int",
206+
skipDoc: true,
207+
document: `n: -4`,
208+
expression: `"banana" * .n`,
209+
expectedError: "Cannot repeat string by a negative number (-4)",
210+
},
204211
{
205212
description: "Multiply int node X string",
206213
document: `n: 4

0 commit comments

Comments
 (0)