Repository navigation
Respect explicit families for bracketed and scoped IPv6 addresses - #128
Conversation
| if requested_family != Socket::AF_UNSPEC && family != requested_family | ||
| raise AddressFamilyError, "address family mismatch" | ||
| end |
There was a problem hiding this comment.
The exception class for the following case changes from IPAddr::InvalidAddressError to IPAddr::AddressFamilyError with this change. Could you change this to preserve the existing behavior by raising IPAddr::InvalidAddressError instead?
# After fix
IPAddr.new("[192.168.1.2]", Socket::AF_INET)
/path/lib/ipaddr.rb:727:in 'IPAddr#initialize': address family mismatch (IPAddr::AddressFamilyError)
raise AddressFamilyError, "address family mismatch"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# Before fix
IPAddr.new("[192.168.1.2]", Socket::AF_INET)
/path/lib/ipaddr.rb:800:in 'IPAddr#in6_addr': invalid address: 192.168.1.2 (IPAddr::InvalidAddressError)
raise InvalidAddressError, "invalid address: #{left}"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^For example, how about making the following change?
- if requested_family != Socket::AF_UNSPEC && family != requested_family
- raise AddressFamilyError, "address family mismatch"
- end
# It seems AI_NUMERICHOST doesn't do the job.
#Socket.getaddrinfo(left, nil, Socket::AF_INET6, Socket::SOCK_STREAM, nil,
...
@family = Socket::AF_INET6
end
@zone_id = zone_id
+ if requested_family != Socket::AF_UNSPEC && @family != requested_family
- if family != Socket::AF_UNSPEC && @family != family
raise AddressFamilyError, "address family mismatch"
end|
Thank you, you are right: checking the family before parsing changed the exception for malformed literals. In f48da8c I removed the early check and compared Added regression coverage for bracketed/scoped IPv4 and malformed literals under unspecified, IPv4, and IPv6 family arguments. The new invalid-input test passes on the original upstream source, fails on the previous PR head, and passes after this change. The original valid-IPv6 family-mismatch regression remains covered. I also removed the AF_UNIX cases.
|
Signed-off-by: AC <ac@fabtek.cn>
f48da8c to
8696d8f
Compare
|
@ac1982 Thank you for the update. This change affects the behavior as follows. # After the fix
IPAddr.new('[::]', Socket::AF_INET)
/path/lib/ipaddr.rb:742:in 'IPAddr#initialize': address family mismatch (IPAddr::AddressFamilyError)
raise AddressFamilyError, "address family mismatch"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
IPAddr.new('::%eth0', Socket::AF_INET)
/path/lib/ipaddr.rb:742:in 'IPAddr#initialize': address family mismatch (IPAddr::AddressFamilyError)
raise AddressFamilyError, "address family mismatch"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
IPAddr.new('[::%eth0]', Socket::AF_INET)
/path/lib/ipaddr.rb:742:in 'IPAddr#initialize': address family mismatch (IPAddr::AddressFamilyError)
raise AddressFamilyError, "address family mismatch"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# Before the fix
IPAddr.new('[::]', Socket::AF_INET)
=> #<IPAddr: IPv6:0000:0000:0000:0000:0000:0000:0000:0000/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff>
IPAddr.new('::%eth0', Socket::AF_INET)
=> #<IPAddr: IPv6:0000:0000:0000:0000:0000:0000:0000:0000%eth0/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff>
IPAddr.new('[::%eth0]', Socket::AF_INET)
=> #<IPAddr: IPv6:0000:0000:0000:0000:0000:0000:0000:0000%eth0/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff>Passing an IPv6 address string such as In actual usage, I believe the impact will be minimal, as the I also checked the usage of Separately, I plan to address the following case in another change, as IPAddr.new("[fe80::1%eth0]/64", Socket::AF_INET6)
# => #<IPAddr: IPv6:fe80:0000:0000:0000:0000:0000:0000:0000%eth0/ffff:ffff:ffff:ffff:0000:0000:0000:0000> |
The existing tests accepted this invalid address, so I added a warning that it will raise |
Fixes #62.
The constructor forces
familytoAF_INET6when it finds brackets or a zone ID. This overwrites an explicitly suppliedAF_INET, soIPAddr.new('[::]', Socket::AF_INET)succeeds even though the undecorated IPv6 spelling raisesAddressFamilyError.Keep the requested family and compare it with the parsed address at the existing post-parse family check. Parsing first preserves
InvalidAddressErrorfor invalid bracketed or scoped literals. Automatic family detection and explicitly requested IPv6 continue to work; bracketed IPv4 addresses remain invalid.The regression covers bracketed, scoped, combined, and CIDR forms, including explicit IPv4 rejection and positive IPv6 cases. It fails on the original upstream source. Additional cases preserve the original
InvalidAddressErrorfor invalid bracketed/scoped IPv4 and malformed literals across unspecified, IPv4, and IPv6 family arguments; these fail on the previous PR head and pass after review revision.bundle exec rake testpasses on Ruby 3.3.8 / Linux arm64: 40 tests, 591 assertions, no failures/errors/omissions.git diff --checkpasses. Other Ruby versions and macOS were not run locally.AI assistance: OpenAI Codex investigated, implemented, and ran these checks.