Skip to content

Commit 2c1881a

Browse files
authored
Fix double dot range for invalid input (#164)
* Fix double dot range for invalid input. * Fix a corner case and add test case.
1 parent d389a61 commit 2c1881a

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

expr_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,14 @@ func TestExpr(t *testing.T) {
820820
`1 + 2 + Three`,
821821
6,
822822
},
823+
{
824+
`4 in 5..1`,
825+
false,
826+
},
827+
{
828+
`4..0`,
829+
[]int{},
830+
},
823831
{
824832
`MapArg({foo: "bar"})`,
825833
"bar",

optimizer/const_range.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ func (*constRange) Exit(node *Node) {
1414
if min, ok := n.Left.(*IntegerNode); ok {
1515
if max, ok := n.Right.(*IntegerNode); ok {
1616
size := max.Value - min.Value + 1
17+
// In case the max < min, patch empty slice
18+
// as max must be greater than equal to min.
19+
if size < 1 {
20+
Patch(node, &ConstantNode{
21+
Value: make([]int, 0),
22+
})
23+
return
24+
}
1725
// In this case array is too big. Skip generation,
1826
// and wait for memory budget detection on runtime.
1927
if size > 1e6 {

vm/runtime.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,9 @@ func exponent(a, b interface{}) float64 {
220220

221221
func makeRange(min, max int) []int {
222222
size := max - min + 1
223+
if size <= 0 {
224+
return []int{}
225+
}
223226
rng := make([]int, size)
224227
for i := range rng {
225228
rng[i] = min + i

0 commit comments

Comments
 (0)