{"id":24607,"date":"2019-03-14T16:15:20","date_gmt":"2019-03-14T20:15:20","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=24607"},"modified":"2019-03-14T16:15:20","modified_gmt":"2019-03-14T20:15:20","slug":"netservice-nuthouse","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2019\/03\/14\/netservice-nuthouse\/","title":{"rendered":"NetService NutHouse"},"content":{"rendered":"<p><a href=\"https:\/\/lapcatsoftware.com\/articles\/netservice-nuthouse.html\">Jeff Johnson<\/a>:<\/p>\n<blockquote cite=\"https:\/\/lapcatsoftware.com\/articles\/netservice-nuthouse.html\"><p>I couldn&rsquo;t reproduce the crash myself, so I did a bit of searching on the web, and I discovered the explanation on <a href=\"https:\/\/stackoverflow.com\/questions\/40193911\/nsnetservice-dictionaryfromtxtrecord-fails-an-assertion-on-invalid-input\">Stack Overflow<\/a>. The function <code>NetService.dictionary(fromTXTRecord:)<\/code> is declared to return <code>[String:Data]<\/code>, but when the TXT Record does not have the proper <code>key=value<\/code> format, CFNetwork inserts <code>kCFNull<\/code> in the dictionary where <code>Data<\/code> is expected. This causes Swift to crash.<\/p><p>In Objective-C, the method <code>+[NSNetService dictionaryFromTXTRecordData:]<\/code> does not crash. However, it still behaves badly, because it is declared to return <code>NSDictionary&lt;NSString*,NSData*&gt;*<\/code>, but the dictionary can contain <code>NSNull<\/code> instead of <code>NSData<\/code>, so your app could still crash if the code trusts the compiler and calls <code>NSData<\/code> methods on <code>NSNull<\/code>.<\/p><p>Needless to say, this bug is awful. It affects both macOS and iOS. Moreover, the bug has existed for more than two years, which is even more awful. Fortunately, developers can work around the bug. In Objective-C a workaround is easy, because we can just check the returned dictionary for <code>NSNull<\/code> values at runtime. In Swift, however, the crash occurs before we can check the dictionary, so we need to try something else.<\/p><\/blockquote>\n<p>He worked around it using the Core Foundation version of the method, which has a different signature and so bridges differently. But it seems to also work to <a href=\"https:\/\/mjtsai.com\/blog\/2017\/08\/29\/swift-4-bridging-peephole-for-as-casts\/\">disable the bridging by casting<\/a> with <code>as NSDictionary<\/code>.<\/p>\n\n<p>Previously: <a href=\"https:\/\/mjtsai.com\/blog\/2019\/02\/22\/swift-subclass-of-nstextstorage-is-slow-because-of-swift-bridging\/\">Swift Subclass of NSTextStorage Is Slow Because of Swift Bridging<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<p>Jeff Johnson: I couldn&rsquo;t reproduce the crash myself, so I did a bit of searching on the web, and I discovered the explanation on Stack Overflow. The function NetService.dictionary(fromTXTRecord:) is declared to return [String:Data], but when the TXT Record does not have the proper key=value format, CFNetwork inserts kCFNull in the dictionary where Data is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"2019-03-14T20:15:22Z","apple_news_api_id":"54d137b9-5280-4067-a9a5-2d09e0eb72d7","apple_news_api_modified_at":"2019-03-14T20:15:25Z","apple_news_api_revision":"AAAAAAAAAAD\/\/\/\/\/\/\/\/\/\/w==","apple_news_api_share_url":"https:\/\/apple.news\/AVNE3uVKAQGeppS0J4Oty1w","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[131,69,31,1610,30,1609,476,71,901],"class_list":["post-24607","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-bug","tag-cocoa","tag-ios","tag-ios-12","tag-mac","tag-macos-10-14","tag-networking","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/24607","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=24607"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/24607\/revisions"}],"predecessor-version":[{"id":24608,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/24607\/revisions\/24608"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=24607"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=24607"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=24607"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}