zold-io/zold

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

开放

#920 创建于 2026年6月17日

 (1 条评论) (0 个反应) (0 位负责人)Ruby (54 个派生)github user discovery
buggood first issue

仓库指标

星标
 (203 个星标)
PR 合并指标
 (PR 指标待抓取)

描述

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.

贡献者指南