Skip to content

Amount#* overflow guard raises NameError instead of the intended RuntimeError #920

Description

@kreinba

In lib/zold/amount.rb the overflow guard inside Amount#* raises with a string that references an undefined local variable m:

def *(other)
  raise '* may only work with a number' unless other.is_a?(Integer) || other.is_a?(Float)
  c = (@zents * other).to_i
  raise "Overflow, can't multiply #{@zents} by #{m}" if c > MAX
  Amount.new(zents: c)
end

When the multiplication result exceeds MAX = 2**63, evaluating #{m} raises NameError: undefined local variable or method 'm' from the string interpolation itself, so the intended RuntimeError describing the overflow never reaches the caller and the backtrace points at the raise line instead of at the multiplier. The variable should be other, the same name used in the guard above.

The fix is to replace #{m} with #{other} on lib/zold/amount.rb:115. A regression test that asserts the raised exception is a RuntimeError whose message contains both the receiver zents and the multiplier would lock the contract going forward.

Same file carries a second misleading message on line 30: raise 'You can\'t specify both coints and zld' fires from the else branch that runs when neither zents: nor zld: is given, and it uses the spelling coints while the rest of the class names the unit zents. The message either needs to say "you must specify one of zents: or zld:" to match the actual condition, or the guard needs to be moved up to catch the both-passed case it currently allows silently.

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

    bugSomething isn't workinggood first issueGood for newcomers

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions