Skip to content

Commit

Permalink
Improvements to IPv4/6 validation.
Browse files Browse the repository at this point in the history
* Do not allow stray . at the end of IPv4 address.
* Do not allow stray : at the end of IPv6 address, but do allow :: at start or
  end.
* Get rid of warning about undefined $segs[-1] when :: is passed.
* Fix non-working is_ipv6 (split ':' -> split /:+/).
  • Loading branch information
pajamian committed Jun 9, 2017
1 parent fb8cd26 commit 699db86
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions lib/Vend/Util.pm
Expand Up @@ -890,7 +890,7 @@ sub is_hash {
# Verify that passed string is a valid IPv4 address.
sub is_ipv4 {
my $addr = shift or return;
my @segs = split /\./, $addr;
my @segs = split /\./, $addr, -1;
return unless @segs == 4;
foreach (@segs) {
return unless /^\d{1,3}$/ && !/^0\d/;
Expand All @@ -901,12 +901,14 @@ sub is_ipv4 {

# Verify that passed string is a valid IPv6 address.
sub is_ipv6 {
my $addr = shift or return;
my @segs = split ':', $addr;
my $tosplit = my $addr = shift or return;
$tosplit =~ s/^:://;
$tosplit =~ s/::$//;
my @segs = split /:+/, $tosplit, -1;

my $quads = 8;
# Check for IPv4 style ending
if ($segs[-1] =~ /\./) {
if (@segs && $segs[-1] =~ /\./) {
return unless is_ipv4(pop @segs);
$quads = 6;
}
Expand Down

0 comments on commit 699db86

Please sign in to comment.