Skip to content
Snippets Groups Projects
Commit 11b73c62 authored by Thomas Bellman's avatar Thomas Bellman
Browse files

resolve_ipnets(): Canonicalize numerical addresses again.

When we started special casing numerical IP addresses to avoid doing
reverse lookups of them back in September 2015 (commit 97b58b8801), we
inadvertently stopped canonicalizing their textual representation, and
would just return whatever we got unchanged.  Restore canonicalization
by calling the #to_s() method on the IPAddr instance we just created,
and returning that.

We also explicitly handle an error case where IPAddr.new does not
raise an exception, but instead returns an IPAddr instance that is
not usable.  This happens at least when you manage to pass nil for
the family argument (which we should never do, but bugs happen).

This was originally commit 7539c236ed4e in nsc-puppet-utils.
parent c3bd97b2
No related branches found
No related tags found
No related merge requests found
...@@ -39,15 +39,23 @@ module Puppet::Parser::Functions ...@@ -39,15 +39,23 @@ module Puppet::Parser::Functions
# Socket.getaddrinfo() does reverse lookup of IP addresses. # Socket.getaddrinfo() does reverse lookup of IP addresses.
# Handle numeric address specially to avoid that for them, at least. # Handle numeric address specially to avoid that for them, at least.
# This also canonicalizes the textual representation.
wanted_families.each do |family| wanted_families.each do |family|
family = RESOLVE_IPNETS__IPFAMILIES.fetch(family) family = RESOLVE_IPNETS__IPFAMILIES.fetch(family)
begin begin
IPAddr.new(hostname, family) ip = IPAddr.new(hostname, family)
rescue ArgumentError rescue ArgumentError
next next
else else
# IPAddr.new() raises ArgumentError for some errors, but
# returns an IPAddr instance with family set to nil for
# some errors (e.g. when the family parameter is nil).
if ip.nil? or ip.family.nil?
raise ArgumentError.new(
"resolve_ipnets(): Internal error: bad return from IPAddr.new")
end
# Can only match one family, so return immediately # Can only match one family, so return immediately
return [hostname + maskspec] return [ip.to_s + maskspec]
end end
end end
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment