From 220138123520d3e3f8987827cc24a947322e72b3 Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Sat, 7 Dec 2024 16:28:59 +1100 Subject: [PATCH] Fixed multiply string by negative number panic #2211 --- pkg/yqlib/operator_multiply.go | 2 ++ pkg/yqlib/operator_multiply_test.go | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/pkg/yqlib/operator_multiply.go b/pkg/yqlib/operator_multiply.go index 64922c72..c4fee2df 100644 --- a/pkg/yqlib/operator_multiply.go +++ b/pkg/yqlib/operator_multiply.go @@ -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) diff --git a/pkg/yqlib/operator_multiply_test.go b/pkg/yqlib/operator_multiply_test.go index 3305fe8f..c055491e 100644 --- a/pkg/yqlib/operator_multiply_test.go +++ b/pkg/yqlib/operator_multiply_test.go @@ -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