This commit is contained in:
Rayan Salhab 2026-07-02 09:56:36 -04:00 committed by GitHub
commit 28deee185f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 10 deletions

View File

@ -150,17 +150,25 @@ func repeatString(lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error
target := lhs.CopyWithoutContent() target := lhs.CopyWithoutContent()
target.UpdateAttributesFrom(stringNode, assignPreferences{}) target.UpdateAttributesFrom(stringNode, assignPreferences{})
count, err := parseInt(intNode.Value) _, count, err := parseInt64(intNode.Value)
if err != nil { if err != nil {
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)
} }
maxResultLen := 10 * 1024 * 1024 // 10 MiB if stringNode.Value == "" {
if count > 0 && len(stringNode.Value) > maxResultLen/count { target.Value = ""
return target, nil
}
maxResultLen := int64(10 * 1024 * 1024) // 10 MiB
if count > 0 && int64(len(stringNode.Value)) > maxResultLen/count {
return nil, fmt.Errorf("result of repeating string (%v bytes) by %v would exceed %v bytes", len(stringNode.Value), count, maxResultLen) return nil, fmt.Errorf("result of repeating string (%v bytes) by %v would exceed %v bytes", len(stringNode.Value), count, maxResultLen)
} }
target.Value = strings.Repeat(stringNode.Value, count) repeatCount, err := parseInt(intNode.Value)
if err != nil {
return nil, err
}
target.Value = strings.Repeat(stringNode.Value, repeatCount)
return target, nil return target, nil
} }

View File

@ -2,7 +2,6 @@ package yqlib
import ( import (
"fmt" "fmt"
"math/bits"
"strings" "strings"
"testing" "testing"
) )
@ -709,12 +708,11 @@ var multiplyOperatorScenarios = []expressionScenario{
}, },
{ {
// Pick a count whose product with len("ab") overflows int on // Pick a count whose product with len("ab") overflows int on
// any architecture: 2^30 on 32-bit, 2^62 on 64-bit. Doubling // 64-bit architectures; it must still report the repeat size
// either yields MaxInt+1, which wraps to MinInt and bypasses // limit on 32-bit architectures instead of an int range error.
// a naive len*count guard.
skipDoc: true, skipDoc: true,
expression: fmt.Sprintf(`"ab" * %d`, 1<<(bits.UintSize-2)), expression: fmt.Sprintf(`"ab" * %d`, int64(1)<<62),
expectedError: fmt.Sprintf("result of repeating string (2 bytes) by %d would exceed 10485760 bytes", 1<<(bits.UintSize-2)), expectedError: fmt.Sprintf("result of repeating string (2 bytes) by %d would exceed 10485760 bytes", int64(1)<<62),
}, },
} }