I recently noticed a strange problem with PHP 8.1:
The line $class_name = Net_DNS2_Lookups::$classes_by_id[$class]; in Net/DNS2/Question.php causes a warning Warning: Undefined array key 20864:
//
// validate it
//
$type_name = Net_DNS2_Lookups::$rr_types_by_id[$type];
$class_name = Net_DNS2_Lookups::$classes_by_id[$class];
if ( (!isset($type_name)) || (!isset($class_name)) ) {
throw new Net_DNS2_Exception(
'invalid question section: invalid type (' . $type .
') or class (' . $class . ') specified.',
Net_DNS2_Lookups::E_QUESTION_INVALID
);
}
Steps to reproduce:
$resolver = new Net_DNS2_Resolver(['nameservers' => ['192.168.1.1']]);
$result = $resolver->query('heise.de', 'DNSKEY');
Where 192.168.1.1 is the IP address of a local DNS forwarder. When using a public name server like 1.1.1.1 the issue does not happen.
The idea is to check wether there is a key for DNSSEC for the domain. This works OK if a DNSKEY record exists. However if there is no DNSKEY record it can happen, that Net_DNS2_Lookups::$classes_by_id[$class]; may not exist at all - and PHP 8.1 will handle this as WARNING.
Possible fix:
//
// validate it
//
$type_name = Net_DNS2_Lookups::$rr_types_by_id[$type] ?? false;
$class_name = Net_DNS2_Lookups::$classes_by_id[$class] ?? false;
if ( false === $type_name || false === $class_name ) {
throw new Net_DNS2_Exception(
'invalid question section: invalid type (' . $type .
') or class (' . $class . ') specified.',
Net_DNS2_Lookups::E_QUESTION_INVALID
);
}
In this case PHP will understand, that if Net_DNS2_Lookups::$rr_types_by_id[$type] or Net_DNS2_Lookups::$classes_by_id[$class] is not defined, the result should be false. Since the existing values for the type or class will never be false this can be used as an indicator for "not defined" and it is better to rely on that than to try to assign a non-defined value to a variable and then check if the variable is "defined".
I recently noticed a strange problem with PHP 8.1:
The line
$class_name = Net_DNS2_Lookups::$classes_by_id[$class];
inNet/DNS2/Question.php
causes a warningWarning: Undefined array key 20864
:Steps to reproduce:
Where
192.168.1.1
is the IP address of a local DNS forwarder. When using a public name server like1.1.1.1
the issue does not happen.The idea is to check wether there is a key for DNSSEC for the domain. This works OK if a DNSKEY record exists. However if there is no DNSKEY record it can happen, that
Net_DNS2_Lookups::$classes_by_id[$class];
may not exist at all - and PHP 8.1 will handle this as WARNING.Possible fix:
In this case PHP will understand, that if
Net_DNS2_Lookups::$rr_types_by_id[$type]
orNet_DNS2_Lookups::$classes_by_id[$class]
is not defined, the result should befalse
. Since the existing values for the type or class will never befalse
this can be used as an indicator for "not defined" and it is better to rely on that than to try to assign a non-defined value to a variable and then check if the variable is "defined".