Skip to content

Commit f9f82d3

Browse files
authored
Merge pull request #31 from purplepixie/null-type-issue
Null type issue
2 parents 928f606 + 545c9e4 commit f9f82d3

2 files changed

Lines changed: 67 additions & 7 deletions

File tree

src/PurplePixie/PhpDns/DNSQuery.php

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ class DNSQuery
5959

6060
private bool $connectionException = false;
6161

62+
private bool $responseException = false;
63+
6264
public function __construct(string $server, int $port = 53, int $timeout = 60, bool $udp = true, bool $debug = false, bool $binarydebug = false)
6365
{
6466
$this->server = $server;
@@ -188,7 +190,7 @@ private function clearError(): void
188190

189191
/**
190192
* @return array
191-
* @throws Exceptions\InvalidQueryTypeId
193+
* @throws Exceptions\InvalidQueryTypeId|Exceptions\InvalidResponse
192194
*/
193195
private function readRecord(): array
194196
{
@@ -198,13 +200,32 @@ private function readRecord(): array
198200

199201
$ans_header_bin = $this->readResponse(10); // 10 byte header
200202

201-
$ans_header = unpack('ntype/nclass/Nttl/nlength', $ans_header_bin);
203+
$ans_header = @unpack('ntype/nclass/Nttl/nlength', $ans_header_bin);
202204

203-
if (is_array($ans_header)) $this->debug(
204-
'Record Type ' . $ans_header['type'] . ' Class ' . $ans_header['class'] .
205-
' TTL ' . $ans_header['ttl'] . ' Length ' . $ans_header['length']
206-
);
207-
else $this->debug("Error unpacking answer header, no array returned.");
205+
if ($ans_header === null || $ans_header === false || !is_array($ans_header)) // the unpack has failed - we assume an invalid return
206+
{
207+
$this->debug("Error unpacking answer header, no array returned.");
208+
209+
if ($this->responseException)
210+
throw new Exceptions\InvalidResponse("Answer header invalid format or empty");
211+
212+
return [
213+
'header' => [],
214+
'typeid' => null,
215+
'typename' => "",
216+
'data' => "",
217+
'domain' => "",
218+
'string' => "Error unpacking answer header",
219+
'extras' => "",
220+
];
221+
}
222+
else
223+
{
224+
$this->debug(
225+
'Record Type ' . $ans_header['type'] . ' Class ' . $ans_header['class'] .
226+
' TTL ' . $ans_header['ttl'] . ' Length ' . $ans_header['length']
227+
);
228+
}
208229

209230
$typeId = $ans_header['type'];
210231

@@ -767,4 +788,14 @@ public function setConnectionException(bool $value): void
767788
{
768789
$this->connectionException = $value;
769790
}
791+
792+
public function setResponseException(bool $value): void
793+
{
794+
$this->responseException = $value;
795+
}
796+
797+
public function getResponseException(): bool
798+
{
799+
return $this->responseException;
800+
}
770801
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/**
4+
* Copyright (C) 2025, David Cutting, Purplepixie Systems
5+
*
6+
* This is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* The software is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this software. If not, see www.gnu.org/licenses
18+
*
19+
* For more information see www.purplepixie.org/phpdns
20+
*/
21+
22+
namespace PurplePixie\PhpDns\Exceptions;
23+
24+
class InvalidResponse extends \Exception {
25+
26+
public function __construct(string $message) {
27+
parent::__construct('Invalid response: ' . $message);
28+
}
29+
}

0 commit comments

Comments
 (0)