mikepultz / netdns2

Native PHP DNS Resolver and Updater
https://netdns2.com/
Other
119 stars 64 forks source link

PHP 7.4: Bug when reusing the resolver to resolve a record #98

Closed ahoshaiyan closed 4 years ago

ahoshaiyan commented 4 years ago

I have faced this little bug when using Net_DNS2 with PHP 7.4. The following code is used to reproduce the problem:

$resolver = new Net_DNS2_Resolver([
    'nameservers' => [
        '8.8.8.8',
        '8.8.4.4'
    ],
    'dnssec' => true,
    'dnssec_cd_flag' => true,
    'timeout' => 4,
    'use_tcp' => true
]);

try {
    $result = $resolver->query('citc.com.sa', 'A');
    var_dump($result);
} catch (\Net_DNS2_Exception $e) {
    var_dump($e);
}

try {
    $result = $resolver->query('citc.com.sa', 'A');
    var_dump($result);
} catch (\Net_DNS2_Exception $e) {
    var_dump($e);
}

In the second query, inside Sockets.php, $data will be null and in previuos versions of PHP when trying to access a null value as an array NULL will be returned, but in 7.4 an exception is thrown.

if ($this->type == Net_DNS2_Socket::SOCK_STREAM) {

    if (($size = @socket_recv($this->sock, $data, 2, 0)) === false) {

        $this->last_error = socket_strerror(socket_last_error());
        return false;
    }

    $length = ord($data[0]) << 8 | ord($data[1]);
    if ($length < Net_DNS2_Lookups::DNS_HEADER_SIZE) {

        return false;
    }
}

Thank you for reading this issue

mikepultz commented 4 years ago

Thanks for the report @eachali - i'll see if I can re-produce this on my system- I'll keep you posted.

Mike

mikepultz commented 4 years ago

I've commited some changes that should resolve the issue under 7.4; this will be included in the next release.

https://github.com/mikepultz/netdns2/commit/1d61f1efaba56a0bb04ac3a546b44610c07308b9

Thanks @eachali,

Mike