Skip to content

Commit

Permalink
Fix InvalidAddressError message
Browse files Browse the repository at this point in the history
  • Loading branch information
ishikawa999 committed Apr 27, 2024
1 parent 036836d commit 96b3ecd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/ipaddr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -693,8 +693,8 @@ def in_addr(addr)
octets = addr.split('.')
end
octets.inject(0) { |i, s|
(n = s.to_i) < 256 or raise InvalidAddressError, "invalid address: #{@addr}"
(s != '0') && s.start_with?('0') and raise InvalidAddressError, "zero-filled number in IPv4 address is ambiguous: #{@addr}"
(n = s.to_i) < 256 or raise InvalidAddressError, "invalid address: #{addr}"
(s != '0') && s.start_with?('0') and raise InvalidAddressError, "zero-filled number in IPv4 address is ambiguous: #{addr}"
i << 8 | n
}
end
Expand Down
12 changes: 12 additions & 0 deletions test/test_ipaddr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -557,4 +557,16 @@ def test_hash
assert_equal(true, s.include?(a5))
assert_equal(true, s.include?(a6))
end

def test_raises_invalid_address_error_with_error_message
e = assert_raise(IPAddr::InvalidAddressError) do
IPAddr.new('192.168.0.1000')
end
assert_equal('invalid address: 192.168.0.1000', e.message)

e = assert_raise(IPAddr::InvalidAddressError) do
IPAddr.new('192.168.01.100')
end
assert_equal('zero-filled number in IPv4 address is ambiguous: 192.168.01.100', e.message)
end
end

0 comments on commit 96b3ecd

Please sign in to comment.