R
Rainer Weikusat
Below is a Perl subroutine supposed to parse a CIDR IPv4 network
specification and return the numerical values of the first and last
addresses in this range (this code is (c) my employer and quoted here
for educational purposes):
sub parse_net_spec($)
{
my ($first, $mask, @octets, $prefix);
$_[0] =~ /^((?:\d{1,3}\.){0,3}\d{1,3})\/(\d\d?)$/ && do {
$prefix = $2;
die('prefix too large') unless $prefix < 33;
@octets = split(/\./, $1);
for (@octets) {
die('octet too large') unless $_ < 256;
$first = $first << 8 | $_;
}
$first <<= 8 * (4 - @octets);
$mask = (1 << (32 - $prefix)) - 1;
return ($first & ~$mask, $first | $mask);
};
die("'$_[0]' does not look like a CIDR network specification");
}
As far as I know, this function is correct in the sense that it should
reject any invalid input and produce the intended result for each
valid one (information to the contrary very much welcome). This will,
of course, given a suitably adjusted perl invocation, result in a
Use of uninitialized value $first in left bitshift (<<)
warning, despite the automatic conversion which is going to take place
here will result in the correct value (0).
What was the precise reason why this is supposed to be 'something that
should not be'?
specification and return the numerical values of the first and last
addresses in this range (this code is (c) my employer and quoted here
for educational purposes):
sub parse_net_spec($)
{
my ($first, $mask, @octets, $prefix);
$_[0] =~ /^((?:\d{1,3}\.){0,3}\d{1,3})\/(\d\d?)$/ && do {
$prefix = $2;
die('prefix too large') unless $prefix < 33;
@octets = split(/\./, $1);
for (@octets) {
die('octet too large') unless $_ < 256;
$first = $first << 8 | $_;
}
$first <<= 8 * (4 - @octets);
$mask = (1 << (32 - $prefix)) - 1;
return ($first & ~$mask, $first | $mask);
};
die("'$_[0]' does not look like a CIDR network specification");
}
As far as I know, this function is correct in the sense that it should
reject any invalid input and produce the intended result for each
valid one (information to the contrary very much welcome). This will,
of course, given a suitably adjusted perl invocation, result in a
Use of uninitialized value $first in left bitshift (<<)
warning, despite the automatic conversion which is going to take place
here will result in the correct value (0).
What was the precise reason why this is supposed to be 'something that
should not be'?