From 21e1641c23cfbcc0bf5153f4ccf9e7b4dbede5a2 Mon Sep 17 00:00:00 2001 From: Nikolay Eskov Date: Fri, 19 Jul 2024 21:38:48 +0300 Subject: [PATCH] Add sanity checks for zero complexity of native function since 'RideV6'. --- pkg/ride/complexity.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/pkg/ride/complexity.go b/pkg/ride/complexity.go index 4722bbcf66..a21f5a40a4 100644 --- a/pkg/ride/complexity.go +++ b/pkg/ride/complexity.go @@ -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)} @@ -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) @@ -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)