From 84fb21806a5bdc528accd475e31101532d8d7339 Mon Sep 17 00:00:00 2001 From: Ivan Semkin Date: Fri, 26 Jul 2024 18:58:43 +0300 Subject: [PATCH] DNSSD: Fix parsing of concatenated TXT records --- .../AsyncDNSResolver/dnssd/DNSResolver_dnssd.swift | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/Sources/AsyncDNSResolver/dnssd/DNSResolver_dnssd.swift b/Sources/AsyncDNSResolver/dnssd/DNSResolver_dnssd.swift index 29c306e..7dc8bde 100644 --- a/Sources/AsyncDNSResolver/dnssd/DNSResolver_dnssd.swift +++ b/Sources/AsyncDNSResolver/dnssd/DNSResolver_dnssd.swift @@ -420,7 +420,14 @@ extension DNSSD { guard let ptr = data?.assumingMemoryBound(to: UInt8.self) else { return nil } - let txt = String(cString: ptr.advanced(by: 1)) + + let bufferPtr = UnsafeBufferPointer(start: ptr, count: Int(length)) + var buffer = Array(bufferPtr)[...] + + guard let txt = self.readName(&buffer, separator: "") else { + throw AsyncDNSResolver.Error(code: .badResponse) + } + return TXTRecord(txt: txt) } @@ -462,7 +469,7 @@ extension DNSSD { } extension DNSSDQueryReplyHandler { - func readName(_ buffer: inout ArraySlice) -> String? { + func readName(_ buffer: inout ArraySlice, separator: String = ".") -> String? { var parts: [String] = [] while let length = buffer.readInteger(as: UInt8.self), length > 0, @@ -470,7 +477,7 @@ extension DNSSDQueryReplyHandler { parts.append(part) } - return parts.isEmpty ? nil : parts.joined(separator: ".") + return parts.isEmpty ? nil : parts.joined(separator: separator) } @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)