mirror of
https://github.com/mikefarah/yq.git
synced 2026-07-07 14:25:38 +00:00
Fix repeatString overflow test on 32-bit platforms (#2680)
The test literal "ab" * 4611686018427387904 (2^62) exceeds MaxInt32,
so parseInt rejects it before the size guard runs. Compute the count
with 1 << (bits.UintSize - 2) to yield 2^30 on 32-bit and 2^62 on
64-bit. Both values, when doubled by len("ab"), wrap past MaxInt and
bypass a naive len*count guard, exercising the division-safe check
added in #2644.
Signed-off-by: Jan Dubois <jan@jandubois.com>
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
ff45fad14c
commit
1deec5e450
@ -2,6 +2,7 @@ package yqlib
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math/bits"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
@ -707,11 +708,13 @@ var multiplyOperatorScenarios = []expressionScenario{
|
|||||||
expectedError: "result of repeating string (1 bytes) by 99999999 would exceed 10485760 bytes",
|
expectedError: "result of repeating string (1 bytes) by 99999999 would exceed 10485760 bytes",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
// The size guard must not overflow: len * count can wrap to
|
// Pick a count whose product with len("ab") overflows int on
|
||||||
// a negative or small value on 64-bit, bypassing the check.
|
// any architecture: 2^30 on 32-bit, 2^62 on 64-bit. Doubling
|
||||||
|
// either yields MaxInt+1, which wraps to MinInt and bypasses
|
||||||
|
// a naive len*count guard.
|
||||||
skipDoc: true,
|
skipDoc: true,
|
||||||
expression: `"ab" * 4611686018427387904`,
|
expression: fmt.Sprintf(`"ab" * %d`, 1<<(bits.UintSize-2)),
|
||||||
expectedError: "result of repeating string (2 bytes) by 4611686018427387904 would exceed 10485760 bytes",
|
expectedError: fmt.Sprintf("result of repeating string (2 bytes) by %d would exceed 10485760 bytes", 1<<(bits.UintSize-2)),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user