Fixed panic on multipling string by very large number #2211

This commit is contained in:
Mike Farah 2024-12-07 16:53:40 +11:00
parent 2201381235
commit 342efb23ff
2 changed files with 10 additions and 0 deletions

View File

@ -155,6 +155,8 @@ func repeatString(lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error
return nil, err return nil, err
} else if count < 0 { } else if count < 0 {
return nil, fmt.Errorf("Cannot repeat string by a negative number (%v)", count) return nil, fmt.Errorf("Cannot repeat string by a negative number (%v)", count)
} else if count > 10000000 {
return nil, fmt.Errorf("Cannot repeat string by more than 100 million (%v)", count)
} }
target.Value = strings.Repeat(stringNode.Value, count) target.Value = strings.Repeat(stringNode.Value, count)

View File

@ -208,6 +208,14 @@ var multiplyOperatorScenarios = []expressionScenario{
expression: `"banana" * .n`, expression: `"banana" * .n`,
expectedError: "Cannot repeat string by a negative number (-4)", expectedError: "Cannot repeat string by a negative number (-4)",
}, },
{
description: "Multiply string X by more than 100 million",
// very large string.repeats causes a panic
skipDoc: true,
document: `n: 100000001`,
expression: `"banana" * .n`,
expectedError: "Cannot repeat string by more than 100 million (100000001)",
},
{ {
description: "Multiply int node X string", description: "Multiply int node X string",
document: `n: 4 document: `n: 4