Skip to content

Amount#/ has no division-by-zero or overflow guard, contradicting the contract of other operators #923

Description

@edmoffo

Amount#/ at lib/zold/amount.rb:119-122 accepts any Integer or Float divisor but performs no further validation before dividing. Dividing by 0 raises ZeroDivisionError (or FloatDomainError for 0.0, since (@zents / 0.0).to_i evaluates Infinity.to_i), and the call site sees a generic standard-library exception instead of the descriptive RuntimeError that every other binary operator in this class raises (+, -, *, ==, <, >, <=, <=>).

There is also no overflow guard symmetrical to the one in #*. Dividing by a positive Float less than 1.0 can produce a zent count beyond MAX, and the result is only caught indirectly by Amount.new(zents: ...) raising "The amount is too big". The error then references the result, not the operation.

Fix on lib/zold/amount.rb:119: add raise 'Cannot divide by zero' if other.zero? and the same > MAX / < -MAX overflow check that the multiplication path needs. Aligning the contract across * and / keeps callers like Tax#debt from converting one library's exception into another.

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