Skip to content

Conversation

@taketo1113
Copy link
Contributor

Background

In the following Pull Request, an issue was fixed where the error message for InvalidAddressError did not include the invalid IP address:

This Pull Request fixes additional cases where the invalid IP address is still not shown in the InvalidAddressError message, as well as cases where the error message shows a numeric value instead of an address format.

Details

Invalid address is not shown

When an invalid non-IPv4 address value is passed to IPAddr#new, @addr is not set. As a result, the error message ends up missing the invalid address.
In this case, the argument (left) passed to the in6_addr method, where the exception is raised, is now included in the error message.

For example:

IPAddr.new("INVALID")
/Users/taketo/.rbenv/versions/4.0-dev/lib/ruby/4.0.0+1/ipaddr.rb:758:in 'IPAddr#in6_addr': invalid address:  (IPAddr::InvalidAddressError)

Numeric value instead of an address representation

In some cases, the address shown in the error message is a numeric value such as 42540766411282592856903984951653826560 instead of a human-readable address format like 2001:db8::.
This Pull Request updates the error messages to display the address in a human-readable format.

For example:

IPAddr.new('2001:db8::').ipv4_mapped
/Users/taketo/.rbenv/versions/4.0-dev/lib/ruby/4.0.0+1/ipaddr.rb:371:in 'IPAddr#ipv4_mapped': not an IPv4 address: 42540766411282592856903984951653826560 (IPAddr::InvalidAddressError)

The following methods are updated to improve their error messages:

  • #ipv4_mapped
  • #ipv4_compat
  • #ip6_arpa
  • #ip6_int

Expected behavior

# IPAddr#new with invalid string
IPAddr.new("INVALID")
/Users/taketo/path/ipaddr/lib/ipaddr.rb:758:in 'IPAddr#in6_addr': invalid address: INVALID (IPAddr::InvalidAddressError)

      raise InvalidAddressError, "invalid address: #{left}"
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
	from /Users/taketo/path/ipaddr/lib/ipaddr.rb:693:in 'IPAddr#initialize'
...

# IPAddr#ipv4_mapped with IPv6 address
IPAddr.new('2001:db8::').ipv4_mapped
/Users/taketo/path/ipaddr/lib/ipaddr.rb:371:in 'IPAddr#ipv4_mapped': not an IPv4 address: 2001:db8:: (IPAddr::InvalidAddressError)

      raise InvalidAddressError, "not an IPv4 address: #{to_s}"
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

# IPAddr#ipv4_compat with IPv6 address
IPAddr.new('2001:db8::').ipv4_compat
/Users/taketo/path/ipaddr/lib/ipaddr.rb:383:in 'IPAddr#ipv4_compat': not an IPv4 address: 2001:db8:: (IPAddr::InvalidAddressError)

      raise InvalidAddressError, "not an IPv4 address: #{to_s}"
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

# IPAddr#ip6_arpa with IPv4 address
IPAddr.new("192.168.2.1").ip6_arpa
/Users/taketo/path/ipaddr/lib/ipaddr.rb:416:in 'IPAddr#ip6_arpa': not an IPv6 address: 192.168.2.1 (IPAddr::InvalidAddressError)

      raise InvalidAddressError, "not an IPv6 address: #{to_s}"
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

# IPAddr#ip6_int with IPv4 address
IPAddr.new("192.168.2.1").ip6_int
/Users/taketo/path/ipaddr/lib/ipaddr.rb:424:in 'IPAddr#ip6_int': not an IPv6 address: 192.168.2.1 (IPAddr::InvalidAddressError)

      raise InvalidAddressError, "not an IPv6 address: #{to_s}"
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Actual behavior

$ ruby -v
ruby 4.0.0dev (2025-12-17T12:11:27Z master aee4b24829) +PRISM [arm64-darwin25]
IPAddr::VERSION
=> "1.2.8"

# IPAddr#new with invalid string
IPAddr.new("INVALID")
/Users/taketo/.rbenv/versions/4.0-dev/lib/ruby/4.0.0+1/ipaddr.rb:758:in 'IPAddr#in6_addr': invalid address:  (IPAddr::InvalidAddressError)

      raise InvalidAddressError, "invalid address: #{@addr}"
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
	from /Users/taketo/.rbenv/versions/4.0-dev/lib/ruby/4.0.0+1/ipaddr.rb:693:in 'IPAddr#initialize'
...

# IPAddr#ipv4_mapped with IPv6 address
IPAddr.new('2001:db8::').ipv4_mapped
/Users/taketo/.rbenv/versions/4.0-dev/lib/ruby/4.0.0+1/ipaddr.rb:371:in 'IPAddr#ipv4_mapped': not an IPv4 address: 42540766411282592856903984951653826560 (IPAddr::InvalidAddressError)

      raise InvalidAddressError, "not an IPv4 address: #{@addr}"
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

# IPAddr#ipv4_compat with IPv6 address
IPAddr.new('2001:db8::').ipv4_compat
/Users/taketo/.rbenv/versions/4.0-dev/lib/ruby/4.0.0+1/ipaddr.rb:383:in 'IPAddr#ipv4_compat': not an IPv4 address: 42540766411282592856903984951653826560 (IPAddr::InvalidAddressError)

      raise InvalidAddressError, "not an IPv4 address: #{@addr}"
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

# IPAddr#ip6_arpa with IPv4 address
IPAddr.new("192.168.2.1").ip6_arpa
/Users/taketo/.rbenv/versions/4.0-dev/lib/ruby/4.0.0+1/ipaddr.rb:416:in 'IPAddr#ip6_arpa': not an IPv6 address: 3232236033 (IPAddr::InvalidAddressError)

      raise InvalidAddressError, "not an IPv6 address: #{@addr}"
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

# IPAddr#ip6_int with IPv4 address
IPAddr.new("192.168.2.1").ip6_int
/Users/taketo/.rbenv/versions/4.0-dev/lib/ruby/4.0.0+1/ipaddr.rb:424:in 'IPAddr#ip6_int': not an IPv6 address: 3232236033 (IPAddr::InvalidAddressError)

      raise InvalidAddressError, "not an IPv6 address: #{@addr}"
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

@taketo1113 taketo1113 requested a review from knu as a code owner December 17, 2025 14:46
@knu knu merged commit c2e47f8 into ruby:master Dec 18, 2025
29 checks passed
@taketo1113 taketo1113 deleted the followup-invalid-address-error-message branch December 18, 2025 12:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Wrong InvalidAddressError message

2 participants