Skip to content

Conversation

@purplepixie
Copy link
Owner

Resolve issue #30 - added error handling for empty array and optional exception, also suppress warnings from unpack()

@purplepixie purplepixie requested a review from Copilot October 28, 2025 17:55
@purplepixie purplepixie self-assigned this Oct 28, 2025
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR addresses issue #30 by improving error handling for DNS response parsing failures. The changes introduce a new exception type for invalid responses and add safeguards against unpack() failures when parsing DNS answer headers.

Key Changes:

  • Added InvalidResponse exception class for DNS response parsing errors
  • Implemented error handling for empty/null array results from unpack() with optional exception throwing
  • Suppressed warnings from unpack() using the @ operator

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
src/PurplePixie/PhpDns/Exceptions/InvalidResponse.php New exception class for handling invalid DNS response errors
src/PurplePixie/PhpDns/DNSQuery.php Added response exception flag, error handling for unpack failures, and getter/setter methods for the new flag

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

' TTL ' . $ans_header['ttl'] . ' Length ' . $ans_header['length']
);
else $this->debug("Error unpacking answer header, no array returned.");
if ($ans_header === null || $ans_header === false || !is_array($ans_header)) // the unpack has failed - we assume an invalid return
Copy link

Copilot AI Oct 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The condition $ans_header === null is redundant. The unpack() function in PHP only returns false on failure, never null. The check should be simplified to if ($ans_header === false).

Suggested change
if ($ans_header === null || $ans_header === false || !is_array($ans_header)) // the unpack has failed - we assume an invalid return
if ($ans_header === false) // the unpack has failed - we assume an invalid return

Copilot uses AI. Check for mistakes.
Comment on lines +222 to +228
else
{
$this->debug(
'Record Type ' . $ans_header['type'] . ' Class ' . $ans_header['class'] .
' TTL ' . $ans_header['ttl'] . ' Length ' . $ans_header['length']
);
}
Copy link

Copilot AI Oct 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The else block is unnecessary since the if block returns an array. The code in the else block (lines 224-227) should be moved after the if block and the else removed to reduce nesting and improve readability.

Copilot uses AI. Check for mistakes.
$ans_header_bin = $this->readResponse(10); // 10 byte header

$ans_header = unpack('ntype/nclass/Nttl/nlength', $ans_header_bin);
$ans_header = @unpack('ntype/nclass/Nttl/nlength', $ans_header_bin);
Copy link

Copilot AI Oct 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using the @ operator to suppress errors is generally discouraged as it hides potential issues and makes debugging harder. Consider removing the @ operator and handling the false return value explicitly, which you're already doing in the subsequent if statement.

Suggested change
$ans_header = @unpack('ntype/nclass/Nttl/nlength', $ans_header_bin);
$ans_header = unpack('ntype/nclass/Nttl/nlength', $ans_header_bin);

Copilot uses AI. Check for mistakes.
@purplepixie purplepixie merged commit f9f82d3 into master Oct 28, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants