Skip to content

Commit

Permalink
Add sanity checks for zero complexity of native function since 'RideV6'.
Browse files Browse the repository at this point in the history
  • Loading branch information
nickeskov committed Jul 23, 2024
1 parent 4c4d5ca commit 21e1641
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions pkg/ride/complexity.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,22 @@ func (o complexityLimitExceededError) Error() string {
)
}

type zeroComplexityError struct {
name string
}

func newZeroComplexityError(name string) zeroComplexityError {
return zeroComplexityError{name: name}
}

func (z zeroComplexityError) EvaluationErrorWrapType() EvaluationError {
return EvaluationFailure
}

func (z zeroComplexityError) Error() string {
return fmt.Sprintf("node '%s' has zero complexity", z.name)
}

func newComplexityCalculator(lib ast.LibraryVersion, limit uint32) complexityCalculator {
if lib >= ast.LibV6 {
return &complexityCalculatorV2{l: int(limit)}
Expand Down Expand Up @@ -229,6 +245,9 @@ func (cc *complexityCalculatorV2) limit() int {
}

func (cc *complexityCalculatorV2) testNativeFunctionComplexity(name string, fc int) error {
if fc == 0 { // sanity check: zero complexity for functions is not allowed since Ride V6
return newZeroComplexityError(name)
}
nc, err := common.AddInt(cc.c, fc)
if err != nil {
return newIntegerOverflowError(name, fc, cc.complexity(), err)
Expand All @@ -240,6 +259,10 @@ func (cc *complexityCalculatorV2) testNativeFunctionComplexity(name string, fc i
}

func (cc *complexityCalculatorV2) addNativeFunctionComplexity(name string, fc int) {
if fc == 0 { // sanity check: zero complexity for functions is not allowed since Ride V6
cc.err = newZeroComplexityError(name)
return // don't add zero complexity
}
nc, err := common.AddInt(cc.c, fc)
if err != nil {
cc.err = newIntegerOverflowError(name, fc, cc.complexity(), err)
Expand Down

0 comments on commit 21e1641

Please sign in to comment.