NetAddr::IP 4.080_10: another trial, please smoke test

NetAddr::IP 4.080_10 is on CPAN as a trial release. I hope it is the last trial before 4.080 goes stable. If you use NetAddr::IP, install it and run your code against it.

The last post here covered 4.080_08. 4.080_09 only added a .perltidyrc and reformatted the source. 4.080_10 adds no new methods or functions. Almost every change below fixes behaviour that was bad or undefined. Input that used to be read as a different address is now rejected. Tied arguments are read once, and a tied value that frees another argument is reported instead of read from freed memory. Many of the fixes make XS and pure Perl give the same result and the same error. This post lists the calls worth checking in your code.

For every detail, see the Changes file on MetaCPAN.

Documented behaviour

The POD now states several rules that were missing or wrong. Read it alongside the changes below:

  • Constructors. The NetAddr::IP POD now says that an undefined mask leaves a mask written in the address in force, that a prefix with a leading zero is refused, and that a trailing-dot or range form accepts only its own mask or host.
  • Aggregation and counting. The same page says that compactref() needs an array reference, takes nothing after the list, and holds only address objects. The Count POD says that a repeated group name keeps both pairs, where version 2.01 kept only the last.
  • Storage and low-level functions. The POD now says that a subclass object freezes as a plain hash, and that :old_storable is deprecated. The Util POD already said that inet_4map6() croaks on a character above 0xFF; the code now does. The InetBase POD says that every dotted part is decimal and takes no leading zero.

Breaking changes

Almost every item here replaces a wrong result with undef or a croak at your line. Check the ones your code uses, and what the new result is where you depended on the old one.

  • Leading-zero prefix lengths are refused (GH#304, 4.080_10). A prefix written as 024 in the address string or as a separate mask is now refused in both address families. This reverses the rule from 4.080_06 that kept the zeros. new_no() still reads such a prefix as decimal.
    CallBeforeNow
    new('192.0.2.0/024')192.0.2.0/24undef
    new('192.0.2.0', '024')192.0.2.0/24undef
    new_no('192.0.2.0/024')192.0.2.0/24192.0.2.0/24
  • A trailing-dot or range form with a separate mask returns undef (GH#293, 4.080_10). The form's own mask used to replace the mask you passed. Its own mask and host are still accepted. Calls that 4.079 accepted now return undef.
    CallBeforeNow
    new('192.0.2.')192.0.2.0/24192.0.2.0/24
    new('192.0.2.', 28)192.0.2.0/24undef
    new('192.0.2.4-7')192.0.2.4/30192.0.2.4/30
    NetAddr::IP->new('192.0.2.');         # 192.0.2.0/24
    NetAddr::IP->new('192.0.2.', 28);     # undef
    NetAddr::IP->new('192.0.2.4-7');      # 192.0.2.4/30
    NetAddr::IP->new('192.0.2.4-7', 28);  # undef
  • Aggregation calls reject bad lists and extra arguments (GH#295, GH#338, GH#352, 4.080_10). compactref() needs an array reference as its list. Anything after that list, including undef and an empty array reference, now croaks where it was dropped. compact(), Compact() and compactref() croak when the list holds anything but a NetAddr::IP or NetAddr::IP::Lite object. Calling compactref() as a function was never documented and now croaks too.
    my $net = NetAddr::IP->new('192.0.2.0/30');
    
    $net->compactref('192.0.2.1');          # croaks: list is not an array reference
    $net->compactref([], undef);            # croaks: argument after the list
    NetAddr::IP::compact('192.0.2.1');      # croaks: list item is not an object
  • Wide characters in mapped-address conversion croak at the caller (GH#355, 4.080_10). inet_4map6() now reports a character above 0xFF with its own name at your line. It used to name isAnyIPv4() or ipv4to6() from a line inside the module, and it returned undef for lengths other than 4 and 16. Calling it with undef now returns undef without the two uninitialized-value warnings it used to print (GH#303).
    NetAddr::IP::Util::inet_4map6(undef);       # undef, no warnings
    NetAddr::IP::Util::inet_4map6("\x{100}");   # croaks: Wide character in ...inet_4map6
  • Numeric and hex labels are refused without a resolver lookup (GH#288, 4.080_10). Text whose last label is a number or 0x hex, or that ends in a dot after a number, now gets undef from inet_aton() without a lookup. The C resolver read 0xc0.0.2.010 as 192.0.2.8 on macOS and sent it to DNS on Linux; 0xc0000201 now also gets undef. Host names still resolve. Text with a colon that the module's IPv6 parser refuses gets an empty list from naip_gethostbyname() without a lookup (GH#290).
    NetAddr::IP::Util::inet_aton('0xc0.0.2.010');  # undef
    NetAddr::IP::Util::inet_aton('192.0.2.1.');    # undef
  • Bad address families croak (GH#289, 4.080_10). A family that is not a whole number, such as 2.5, or that does not fit in 32 bits, makes inet_pton() and inet_ntop() croak with the family error. Socket used to cut such a number down to AF_INET or AF_INET6, which skipped this module's parser. On macOS that let inet_pton(2.5, '192.0.2.010') return an address.
    NetAddr::IP::Util::inet_pton(2.5, '192.0.2.010');
    # croaks: Bad address family ... got 2.5, should be AF_INET (2) or AF_INET6 (10)

Constructors and parsing

The remaining constructor changes fix wrong results for masks and values that used to be ignored or read in the wrong address family.

  • An undefined mask leaves the mask in the address in force (GH#292, 4.080_10). Passing undef as the mask no longer clears a prefix written in the address string. With no mask in the address, undef still gives the host mask.
    CallBeforeNow
    new('192.0.2.0/24', undef)undef192.0.2.0/24
    new('192.0.2.0', undef)192.0.2.0/32192.0.2.0/32
  • The IPv4 default route keeps an IPv4 mask (GH#291, 4.080_10). Calls using default or any, and masks written as default, any or ::, now store the same mask as 0.0.0.0/0. Before, they stored 128 zero bits, so arithmetic and ordering treated them as IPv6 while == and eq called them equal. An IPv6 /0 still keeps its all-zero mask.
    NetAddr::IP->new('default')->mask;      # 0.0.0.0
    NetAddr::IP->new('default') - 1;        # 255.255.255.255/0
    NetAddr::IP->new('::/0')->mask;         # 0:0:0:0:0:0:0:0
  • Ranges with spaces pass through the deprecated constructors (GH#304, 4.080_10). The deprecated constructors new_cis() and new_cis6() leave a range with spaces around the dash for new() and new6() to parse. Both constructors already accept that form. The space to mask rewrite no longer runs on text with a dash next to whitespace.
  • Split methods take a Math::BigInt mask (GH#297, 4.080_10). Previously they warned and then croaked with a mask error. An unblessed reference given as a mask now croaks with the mask error instead of dying with a hash-reference error.
    use Math::BigInt;
    
    my $net = NetAddr::IP->new('192.0.2.0/28');
    my @pieces = $net->split(Math::BigInt->new(30));
    # 192.0.2.0/30, 192.0.2.4/30, 192.0.2.8/30, 192.0.2.12/30
  • Zero-padded indices work on /31 and /127 (GH#294, 4.080_10). On those prefix lengths, zero-padded indices return the first host, as an index of zero does on every prefix length. They used to return undef.
    my $net = NetAddr::IP->new('192.0.2.0/31');
    
    $net->nth('00');    # 192.0.2.0/31
    $net->nth('-0');    # 192.0.2.0/31
    $net->nth('-00');   # 192.0.2.0/31

Methods

  • Constructing a counter keeps pairs in order (GH#298, 4.080_10). Pairs given to new() are added in the order you write them, as calls to add() are. A group name used twice keeps the subnets of both pairs. Before, only the last pair counted. An undefined group name croaks, as it does in add().
    my $counter = NetAddr::IP::Count->new(
        docs => [ NetAddr::IP->new('192.0.2.0/31') ],
        docs => [ NetAddr::IP->new('192.0.2.2/31') ],
    );
    $counter->count(NetAddr::IP->new('192.0.2.0'));    # docs: 1
    $counter->count(NetAddr::IP->new('192.0.2.2'));    # docs: 2
  • Subclass keys survive Storable (GH#296, 4.080_10). An object of a subclass now freezes as a plain hash, so freeze, thaw and dclone keep its own keys. Before, only the address and mask came back. A plain object still freezes as its CIDR text. Storable croaks on a subclass key that holds a code reference or filehandle, as it does for any other hash.

Low-level functions and error text

Most of the rest of the release fixes undefined or inconsistent behaviour on bad input, and makes XS and pure Perl agree. These changes matter most if you call the functions directly or use tied and overloaded arguments.

  • Object arguments are read once (GH#326, GH#359, GH#300, 4.080_10). The InetBase functions that take a packed address, including isNewIPv4(), isAnyIPv4(), inet_n2dx() and inet_n2ad(), now check an object argument once. Some checked it up to four times. An object whose text holds a character above 0xFF now croaks with Wide character in and the function name at the caller, as a plain string does. Pure Perl reads object arguments through the same helper, and bcd2bin() and simple_pack() read an object's text once per call (GH#324).
  • Bad counts and tied arguments are handled alike in both builds (GH#286, GH#337, GH#325, 4.080_10). The functions shiftleft(), addconst() and bcdn2bin() read the address before the count and keep the count alive when the address could free it, so a tied address that frees the count is reported instead of read from freed memory. A bad address is reported before the count is read. The croak prints the count as given, including wide characters and NUL. In pure Perl, addconst() now takes the $$ prototype of the XS build (GH#299). A plain call that passes both arguments from an array, or passes a third one, fails to compile there too.
  • Error text matches across builds (GH#327, GH#302, GH#301, 4.080_10). A Util function called with the wrong number of arguments now croaks the XS usage line at your line in pure Perl, where it reported a bad length or returned normally. In XS, a wide character in a packed argument names the function called, such as hasbits, where it used to say Wide character in subroutine entry. Both builds name the bad character in bcd2bin() and simple_pack(). Empty input to bcdn2bin() now gets the XS length error in pure Perl, not should be 1 to 0 digits.
  • Taint survives the pure Perl functions (GH#287, 4.080_10). Under perl -T, pure Perl now keeps the taint of what goes in on what comes out, as XS does. The affected functions are add128(), addconst(), bcd2bin(), bcdn2bin(), bcdn2txt(), bin2bcd(), bin2bcdn(), ipv6to4(), shiftleft(), simple_pack() and sub128(). Code that used such a result unchecked now dies with Insecure dependency.
  • Split methods pass no extra argument (GH#328, 4.080_10). The split methods no longer pass the net's address family to new() as a third argument, which new() never read. No result changes.

Install and build

An XS install now records itself under the same distribution name as a pure Perl install (GH#358, 4.080_10). It writes auto/NetAddr/IP/.packlist for NetAddr::IP. The old path, auto/NetAddr/IP/Util/.packlist under NetAddr::IP::Util, made tools that read packlists see a second distribution and left the old file behind after an upgrade from 4.079. The XS source moved to lib/NetAddr/IP/Util.xs and builds with MakeMaker's XSMULTI, so configuring needs ExtUtils::MakeMaker 7.12.

After checking the API changes, test how the build behaves on your platform. Find out which build you got:

perl -MNetAddr::IP::Util -e 'print NetAddr::IP::Util::mode(), "\n"'

It prints CC XS or Pure Perl. If you can, run your tests once in each mode, with perl Makefile.PL -noxs for the second run.

Try it

Install the trial from CPAN:

cpanm DJZORT/NetAddr-IP-4.080_10.tar.gz

A plain cpanm NetAddr::IP still installs the last stable release, so use the tarball. Or clone and test from source:

git clone https://github.com/cpan-authors/perl-NetAddr-IP.git
cd perl-NetAddr-IP
dzil test

Thank you to everyone who filed a bug report during the 4.080 trials. Many of the changes above came from those reports. If something breaks, please open an issue with your Perl version, OS, whether you built XS or pure Perl, and the output of mode(). Smoke reports are what turn a trial into a stable 4.080.

Leave a comment

About Dean

user-pic I blog about Perl. I am back in Sydney