From c8d6404fb8263691578cc731cc2b5978834cf626 Mon Sep 17 00:00:00 2001 From: Josh Cooper Date: Mon, 9 Jul 2018 21:57:33 -0700 Subject: Don't use global matchdata Simplify logic for validating each IPv4 tuple. Remove explicit return based on the result of the conditional. --- lib/puppet/type/host.rb | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/lib/puppet/type/host.rb b/lib/puppet/type/host.rb index 52f0e87..03059b9 100644 --- a/lib/puppet/type/host.rb +++ b/lib/puppet/type/host.rb @@ -11,27 +11,18 @@ Puppet::Type.newtype(:host) do desc "The host's IP address, IPv4 or IPv6." def valid_v4?(addr) - if %r{^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$} =~ addr - $LAST_MATCH_INFO.captures.all? do |i| - i = i.to_i - i >= 0 && i <= 255 - end - else - false - end + data = addr.match(%r{^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$}) + data && data.captures.map(&:to_i).all? { |i| i >= 0 && i <= 255 } end def valid_v6?(addr) # http://forums.dartware.com/viewtopic.php?t=452 # ...and, yes, it is this hard. Doing it programmatically is harder. - return true if addr =~ %r{^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3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3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$} end def valid_newline?(addr) - return false if addr =~ %r{\n} || addr =~ %r{\r} - true + addr !~ %r{\n} && addr !~ %r{\r} end validate do |value| @@ -63,7 +54,9 @@ Puppet::Type.newtype(:host) do newproperty(:comment) do desc 'A comment that will be attached to the line with a # character.' validate do |value| - raise Puppet::Error, _('Comment cannot include newline') if value =~ %r{\n} || value =~ %r{\r} + if value =~ %r{\n} || value =~ %r{\r} + raise Puppet::Error, _('Comment cannot include newline') + end end end @@ -87,11 +80,13 @@ Puppet::Type.newtype(:host) do validate do |value| value.split('.').each do |hostpart| - unless hostpart =~ %r{^([\w]+|[\w][\w\-]+[\w])$} + if hostpart !~ %r{^([\w]+|[\w][\w\-]+[\w])$} raise Puppet::Error, _('Invalid host name') end end - raise Puppet::Error, _('Hostname cannot include newline') if value =~ %r{\n} || value =~ %r{\r} + if value =~ %r{\n} || value =~ %r{\r} + raise Puppet::Error, _('Hostname cannot include newline') + end end end end -- cgit v1.2.3