Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions Zeroconf/Dns/RecordReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public string ReadDomainName()
// top 2 bits set denotes domain name compression and to reference elsewhere
if ((length & 0xc0) == 0xc0)
{
// Standard suffix compression (11xxxxxx)
// work out the existing domain name, copy this pointer
var newRecordReader = new RecordReader(m_Data, (length & 0x3f) << 8 | ReadByte());
if (bytes.Count > 0)
Expand All @@ -87,6 +88,37 @@ public string ReadDomainName()
}
return newRecordReader.ReadDomainName();
}
// Check for 0x80 pattern (10xxxxxx) - prefix compression
else if ((length & 0xc0) == 0x80)
{
// Prefix compression (10xxxxxx)
// Read the pointer to the prefix
var offset = (length & 0x3f) << 8 | ReadByte();
var newRecordReader = new RecordReader(m_Data, offset);
var prefix = newRecordReader.ReadDomainName();

// Continue reading suffix at current position
var suffix = ReadDomainName();
Copy link

Copilot AI Oct 23, 2025

Choose a reason for hiding this comment

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

Recursive call to ReadDomainName() could lead to infinite recursion or stack overflow if the DNS packet contains circular references with 0x80 pointers. Consider adding depth tracking or visited offset checking to prevent infinite loops, similar to how the 0xc0 compression should be protected.

Copilot uses AI. Check for mistakes.

// Build the result, handling empty suffix (just ".")
string result;
if (suffix == ".")
{
// No suffix, just return prefix
result = prefix;
}
else
{
// Combine prefix and suffix, removing the trailing dot from prefix
result = prefix.TrimEnd('.') + "." + suffix;
Copy link

Copilot AI Oct 23, 2025

Choose a reason for hiding this comment

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

String concatenation with dots could produce malformed domain names if either prefix or suffix contains internal dots or empty segments. Consider validating that both prefix and suffix are well-formed domain names before combining, or use a more robust joining method that handles edge cases like empty labels between dots.

Copilot uses AI. Check for mistakes.
}

if (bytes.Count > 0)
{
return Encoding.UTF8.GetString(bytes.ToArray(), 0, bytes.Count) + result;
}
Comment on lines +116 to +119
Copy link

Copilot AI Oct 23, 2025

Choose a reason for hiding this comment

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

[nitpick] This logic for prepending accumulated bytes is duplicated from the 0xc0 compression block above (lines 85-88). Consider extracting this pattern into a helper method to reduce code duplication and improve maintainability.

Copilot uses AI. Check for mistakes.
return result;
}

// if not using compression, copy a char at a time to the domain name
while (length > 0)
Expand Down
Loading