From 16afc422df4948e14ff26f297bc0c6ee42680c9c Mon Sep 17 00:00:00 2001 From: ACTCD <101378590+ACTCD@users.noreply.github.com> Date: Wed, 11 Sep 2024 19:16:00 +0800 Subject: [PATCH 1/2] fix: avoid potential thread crashes of parse --- xcode/Ext-Safari/Functions.swift | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/xcode/Ext-Safari/Functions.swift b/xcode/Ext-Safari/Functions.swift index 9d8de01f..5765d11c 100644 --- a/xcode/Ext-Safari/Functions.swift +++ b/xcode/Ext-Safari/Functions.swift @@ -128,6 +128,7 @@ func parse(_ content: String) -> [String: Any]? { let range = NSRange(location: 0, length: content.utf16.count) // return nil/fail if metablock missing guard let match = regex.firstMatch(in: content, options: [], range: range) else { + logger?.debug("\(#function, privacy: .public) - Non matched content: \(content, privacy: .public)") return nil } @@ -136,15 +137,23 @@ func parse(_ content: String) -> [String: Any]? { // rather than being too strict, text content can precede the opening userscript tag, however it will be ignored // adjust start index of file content while assigning group numbers to account for any text content preceding opening tag let contentStartIndex = content.index(content.startIndex, offsetBy: match.range.lowerBound) - var g1, g2, g3:Int - if (content[contentStartIndex.. Date: Wed, 11 Sep 2024 20:27:54 +0800 Subject: [PATCH 2/2] fix: avoid potential thread crashes of url Internal error in Foundation URL.host(percentEncoded:) --- xcode/Shared/UrlPolyfill.swift | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/xcode/Shared/UrlPolyfill.swift b/xcode/Shared/UrlPolyfill.swift index 915ae8a7..f4be2267 100644 --- a/xcode/Shared/UrlPolyfill.swift +++ b/xcode/Shared/UrlPolyfill.swift @@ -49,15 +49,23 @@ func jsLikeURL(_ urlString: String, baseString: String? = nil) -> [String: Strin guard let url = _url else { return nil } - guard let scheme = url.scheme else { return nil } + /* + The issue still exists as of macOS 14.4, iOS 17.0 + https://stackoverflow.com/questions/74445926/url-host-deprecated-but-replacement-crashes + https://forums.swift.org/t/does-url-query-percentencoded-calls-url-host-percentencoded-under-the-hood/70452 + https://forums.developer.apple.com/forums/thread/722451 + */ + guard let hostname = url.host else { + return nil + } var port = (url.port == nil) ? "" : String(url.port!) if (scheme == "http" && port == "80") { port = "" } if (scheme == "https" && port == "443") { port = "" } if #available(macOS 13.0, iOS 16.0, *) { - let hostname = url.host(percentEncoded: true) ?? "" +// let hostname = url.host(percentEncoded: true) ?? "" let host = (port == "") ? hostname : "\(hostname):\(port)" let query = url.query(percentEncoded: true) ?? "" let fragment = url.fragment(percentEncoded: true) ?? "" @@ -75,7 +83,6 @@ func jsLikeURL(_ urlString: String, baseString: String? = nil) -> [String: Strin "username": url.user(percentEncoded: true) ?? "" ] } else { - let hostname = url.host ?? "" let host = (port == "") ? hostname : "\(hostname):\(port)" let query = url.query ?? "" let fragment = url.fragment ?? ""