Subject: | MX record wih 0 preference fails to parse |
Date: | Thu, 22 Jan 2015 16:12:35 +0100 |
To: | bug-Net-DNS [...] rt.cpan.org |
From: | Jan-Pieter Cornet <johnpc [...] xs4all.net> |
Hi Net-DNS maintainers,
easy to reproduce bug:
$ perl -MNet::DNS -le 'print Net::DNS::RR->new(". MX 0 foo")->exchange'
Can't call method "name" on an undefined value at /Users/johnpc/perl5/perlbrew/perls/perl-5.20.0/lib/site_perl/5.20.1/darwin-2level/Net/DNS/RR/MX.pm line 79.
Calling with non-zero pref works as expected:
$ perl -MNet::DNS -le 'print Net::DNS::RR->new(". MX 1 foo")->exchange'
foo
This is for:
$ perl -MNet::DNS -le 'print $Net::DNS::VERSION'
0.82
The root cause is found in lib/Net/DNS/RR/MX.pm sub parse_rdata, which has:
...
$self->preference( shift || return );
Since the preference "0" evaluates as false, this returns before setting the preference and the exchange.
The same "shift || return" construct is found in Net::DNS::RR::SOA, where it is less likely to encounter a 0, but still. Also, that construct had me check if I had a large enough bottle of EyeBleach nearby.
A side effect is that calling Net::DNS::RR->new(...)->exchange can crash because the exchange is unset.
Attached is a proposed patch that fixes it (and adds tests for this). I notice that calling ->name() on elements is much more common, and might cause other crashes on uninitialised records, so a more general solution might be better in this case.
--
Jan-Pieter Cornet <johnpc@xs4all.net>
Systeembeheer XS4ALL Internet bv
www.xs4all.nl
Message body is not shown because sender requested not to inline it.
Message body not shown because it is not plain text.