Skip to content

Commit

Permalink
Validate that step takes the direction of sequence towards end
Browse files Browse the repository at this point in the history
  • Loading branch information
darkdrag00nv2 committed Jun 25, 2023
1 parent 82a8814 commit 9813453
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions runtime/interpreter/value_range.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
package interpreter

import (
"fmt"

"github.com/onflow/cadence/runtime/common"
"github.com/onflow/cadence/runtime/errors"
"github.com/onflow/cadence/runtime/sema"
Expand Down Expand Up @@ -61,8 +63,6 @@ func NewInclusiveRangeValueWithStep(
rangeType InclusiveRangeStaticType,
) *CompositeValue {

// TODO: Validate if the sequence is moving away from the end value.

// Validate that the step is non-zero.
if step.Equal(interpreter, locationRange, getValueForIntegerType(0, rangeType.ElementType)) {
panic(InclusiveRangeConstructionError{
Expand All @@ -71,6 +71,19 @@ func NewInclusiveRangeValueWithStep(
})
}

// Validate that the sequence is moving towards the end value.
// If start < end, step must be > 0
// If start > end, step must be < 0
// If start == end, step doesn't matter.
if (start.Less(interpreter, end, locationRange) && step.Less(interpreter, getValueForIntegerType(0, rangeType.ElementType), locationRange)) ||
(start.Greater(interpreter, end, locationRange) && step.Greater(interpreter, getValueForIntegerType(0, rangeType.ElementType), locationRange)) {

panic(InclusiveRangeConstructionError{
LocationRange: locationRange,
Message: fmt.Sprintf("sequence is moving away from end: %s due to the value of step: %s and start: %s", end, step, start),
})
}

fields := []CompositeField{
{
Name: sema.InclusiveRangeTypeStartFieldName,
Expand Down

0 comments on commit 9813453

Please sign in to comment.