Skip to content

Commit 039afcd

Browse files
committed
fix repeating string to emit an error when the result is too large
1 parent 05329e2 commit 039afcd

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

cli/test.yaml

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2853,7 +2853,7 @@
28532853
28542854
- name: multiply strings
28552855
args:
2856-
- '[-1.0, -0.5, 0.0, 0.5, 1.0, 1.5, 3.7, 10, 9444732965739290427392 / 4722366482869645213696, 100000000, infinite, -infinite, nan, -1e300, 1e300][] * "abc"'
2856+
- '(-1.0, -0.5, 0.0, 0.5, 1.0, 1.5, 3.7, 10, nan) * "abc"'
28572857
input: 'null'
28582858
expected: |
28592859
null
@@ -2864,14 +2864,18 @@
28642864
"abc"
28652865
"abcabcabc"
28662866
"abcabcabcabcabcabcabcabcabcabc"
2867-
"abcabc"
2868-
null
2869-
null
2870-
null
2871-
null
2872-
null
28732867
null
28742868
2869+
- name: multiply strings error
2870+
args:
2871+
- -r
2872+
- '(infinite, 1e300, 720000000) as $n | try ($n * "abc") catch .'
2873+
input: 'null'
2874+
expected: |
2875+
repeat string result too large: "abc" * 1.7976931348623157e+308
2876+
repeat string result too large: "abc" * 1e+300
2877+
repeat string result too large: "abc" * 720000000
2878+
28752879
- name: multiply empty string
28762880
args:
28772881
- '(-9223372036854775808, -1, 0, 0.001, 0.999, 1, 7, 9223372036854775807) * ""'

error.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ func (err *arrayIndexTooLargeError) Error() string {
5151
return "array index too large: " + Preview(err.v)
5252
}
5353

54+
type repeatStringTooLargeError struct {
55+
s string
56+
n float64
57+
}
58+
59+
func (err *repeatStringTooLargeError) Error() string {
60+
return "repeat string result too large: " + Preview(err.s) + " * " + Preview(err.n)
61+
}
62+
5463
type objectKeyNotStringError struct {
5564
v any
5665
}

operator.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,12 +433,15 @@ func deepMergeObjects(l, r map[string]any) any {
433433
}
434434

435435
func repeatString(s string, n float64) any {
436-
if n < 0.0 || len(s) > 0 && n > float64(0x10000000/len(s)) || math.IsNaN(n) {
436+
if n < 0.0 || math.IsNaN(n) {
437437
return nil
438438
}
439439
if s == "" {
440440
return ""
441441
}
442+
if n >= float64(math.MaxInt32/len(s)) {
443+
return &repeatStringTooLargeError{s, n}
444+
}
442445
return strings.Repeat(s, int(n))
443446
}
444447

0 commit comments

Comments
 (0)