Skip to content

DecimalArithmetic panics on division or modulo by zero #123

Open

Description

@JayOfTheKeyboard

With gval.DecimalArithmetic(), division or modulo by zero panics with decimal division by 0 instead of returning an error. The panic comes out of Evaluate, so any expression containing / 0 or % 0 crashes an application that evaluates user-supplied expressions in decimal mode.

lang := gval.NewLanguage(gval.Full(), gval.DecimalArithmetic())
lang.Evaluate("1 / 0", nil)        // panic: decimal division by 0
lang.Evaluate("1 % 0", nil)        // panic: decimal division by 0
lang.Evaluate("0 ** (0 - 1)", nil) // panic: decimal division by 0
lang.Evaluate("x / y", map[string]interface{}{"x": 1, "y": 0}) // same panic

The /, % and ** operators in gval.go call shopspring/decimal's Div, Mod and Pow directly, and those panic on a zero divisor (Pow divides by the base when the exponent is -1 or less). The default float arithmetic is fine: 1 / 0 gives +Inf.

Reproduced on v1.2.4 and on master (c622ea2). I have a fix and will open a PR.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions