bradclawsie / Net-IP-Parse

Other
0 stars 4 forks source link

CIDR fails on IPv6 loopback address #6

Open frithnanth opened 11 months ago

frithnanth commented 11 months ago

This code:

#!/usr/bin/env raku

use Subsets::Common;
use Net::IP::Parse;

my CIDR $cidr .= new: cidr => '::1/128';

fails with this error:

Index 16 for dimension 1 out of range (must be 0..15)
  in submethod BUILD at /home/nando/.raku/sources/B5C29E3DCE27D135242DBAAF8F5158F01F7A77BF (Net::IP::Parse) line 261
  in submethod BUILD at /home/nando/.raku/sources/B5C29E3DCE27D135242DBAAF8F5158F01F7A77BF (Net::IP::Parse) line 247
  in block <unit> at test.raku line 6

hinting that the problem might be with the Subsets::Common module. 0:0:0:0:0:0:0:0/128 and 0:0:0:0:0:0:0:1/128 fail as well with the same error.

The IP class seems safe from this kind of error, so this program:

#!/usr/bin/env raku

use Subsets::Common;
use Net::IP::Parse;

my IP $ip .= new: addr => '::1';

executes without problems, hinting that the bug might be in the netmask handling.

frithnanth commented 11 months ago

I looked at your code and I see it doesn't work for any CIDR with mask between /120 and /128 (the 16th byte). I think it's easy to avoid the issue: change line 261 in lib/Net/IP/Parse.rakumod from

@b[$div] = 255 +^ (2**((($div + 1) * 8) - $prefix)-1);

to

@b[$div] = 255 +^ (2**((($div + 1) * 8) - $prefix)-1) if $div < 16;

This way if the prefix length is /128 the mask is not modified from its initial value (255 xx 16). This small change passes all the present tests and lets the loopback CIDR work fine. It seems to me that the prefix_addr, network_addr, broadcast_addr, and wildcard_addr are all reasonable; I hope I'm not overlooking anything. Do you like me to open a PR?

bradclawsie commented 10 months ago

hi! yes if you don't mind opening a PR, I will accept it immediately...I haven't played with Raku much lately and I don't have the toolchain installed on my laptop anymore.