Repository navigation
Return an error instead of panicking on integer division by zero - #567
Open
shaurya703 wants to merge 1 commit into
Open
shaurya703 wants to merge 1 commit into
shaurya703 wants to merge 1 commit into
Conversation
Value.Divide and Value.Modulo used Go's integer / and % directly when both operands were ints, so a selector such as `1 / 0` or `5 % 0` crashed the process with "panic: runtime error: integer divide by zero". Check the divisor first and return ErrDivisionByZero instead.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug
Dividing or taking the modulo of two integers by zero crashes dasel:
5 % 0panics the same way. The same happens through the Go API (dasel.Select,Query), so a library caller evaluating a user-supplied selector gets a panic rather than an error.Cause
Value.DivideandValue.Moduloapply Go's/and%directly in the int/int branch, and Go panics on integer division by zero. The float branches don't panic (they produce+Inf/NaN), so this only affects the int/int case.Fix
Check for a zero divisor in the int/int branches and return a new
model.ErrDivisionByZero. After the change:Test
TestValue_IntegerDivisionByZeroinmodel/value_math_test.gocoversDivideandModulo. It panics before the change and passes after.go test ./...andgolangci-lint run ./model/...pass.No documentation changes needed.