Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSNetService dictionaryFromTXTRecord fails an assertion on invalid input

The input to dictionary(fromTXTRecord:) comes from the network, potentially from outside the app, or even the device. However, Apple's docs say:

... Fails an assertion if txtData cannot be represented as an NSDictionary object.

Failing an assertion leaves the programmer (me) with no way of handling the error, which seems illogic for a method that processes external data.

If I run this in Terminal on a Mac:

dns-sd -R 'My Service Name' _myservice._tcp local 4567 asdf asdf

my app, running in an iPhone, crashes.

dictionary(fromTXTRecord:) expects the TXT record data (asdf asdf) to be in key=val form. If, like above, a word doesn't contain any = the method won't be able to parse it and fail the assertion.

I see no way of solving this problem other than not using that method at all and implementing my own parsing, which feels wrong.

Am I missing something?

like image 665
ateijelo Avatar asked Oct 22 '16 15:10

ateijelo


1 Answers

Here's a solution in Swift 4.2, assuming the TXT record has only strings:

/// Decode the TXT record as a string dictionary, or [:] if the data is malformed
public func dictionary(fromTXTRecord txtData: Data) -> [String: String] {

    var result = [String: String]()
    var data = txtData

    while !data.isEmpty {
        // The first byte of each record is its length, so prefix that much data
        let recordLength = Int(data.removeFirst())
        guard data.count >= recordLength else { return [:] }
        let recordData = data[..<(data.startIndex + recordLength)]
        data = data.dropFirst(recordLength)

        guard let record = String(bytes: recordData, encoding: .utf8) else { return [:] }
        // The format of the entry is "key=value"
        // (According to the reference implementation, = is optional if there is no value,
        // and any equals signs after the first are part of the value.)
        // `ommittingEmptySubsequences` is necessary otherwise an empty string will crash the next line
        let keyValue = record.split(separator: "=", maxSplits: 1, omittingEmptySubsequences: false)
        let key = String(keyValue[0])
        // If there's no value, make the value the empty string
        switch keyValue.count {
        case 1:
            result[key] = ""
        case 2:
            result[key] = String(keyValue[1])
        default:
            fatalError()
        }
    }

    return result
}
like image 55
spudwaffle Avatar answered Sep 30 '22 06:09

spudwaffle