Skip to content

Commit aee4b2e

Browse files
authored
Merge pull request #714 from quoid/fix/avoid-potential-thread-crashes
fix: avoid two potential thread crashes
2 parents 4937f21 + b50a23e commit aee4b2e

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

xcode/Ext-Safari/Functions.swift

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ func parse(_ content: String) -> [String: Any]? {
128128
let range = NSRange(location: 0, length: content.utf16.count)
129129
// return nil/fail if metablock missing
130130
guard let match = regex.firstMatch(in: content, options: [], range: range) else {
131+
logger?.debug("\(#function, privacy: .public) - Non matched content: \(content, privacy: .public)")
131132
return nil
132133
}
133134

@@ -136,15 +137,23 @@ func parse(_ content: String) -> [String: Any]? {
136137
// rather than being too strict, text content can precede the opening userscript tag, however it will be ignored
137138
// adjust start index of file content while assigning group numbers to account for any text content preceding opening tag
138139
let contentStartIndex = content.index(content.startIndex, offsetBy: match.range.lowerBound)
139-
var g1, g2, g3:Int
140-
if (content[contentStartIndex..<content.endIndex].starts(with: "//")) {
140+
let contentEndIndex = content.index(contentStartIndex, offsetBy: 15)
141+
let metaHeadContent = content[contentStartIndex..<contentEndIndex]
142+
var g1, g2, g3: Int
143+
if (metaHeadContent.starts(with: "//")) {
141144
g1 = 1; g2 = 2; g3 = 3
142145
} else {
143146
g1 = 4; g2 = 5; g3 = 6
144147
}
145148

149+
// unlikely to happen but did see some crashes, add checking and logging
150+
guard let metablockRange = Range(match.range(at: g1), in: content) else {
151+
logger?.error("\(#function, privacy: .public) - Nil range (\(g1, privacy: .public)): \(metaHeadContent, privacy: .public)")
152+
logger?.debug("\(#function, privacy: .public) - Nil range content: \(content, privacy: .public)")
153+
return nil
154+
}
146155
// can force unwrap metablock since nil check was done above
147-
let metablock = content[Range(match.range(at: g1), in: content)!]
156+
let metablock = content[metablockRange]
148157
// create var to store separated metadata keys/values
149158
var metadata = [:] as [String: [String]]
150159
// iterate through the possible metadata keys in file

xcode/Shared/UrlPolyfill.swift

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,23 @@ func jsLikeURL(_ urlString: String, baseString: String? = nil) -> [String: Strin
4949
guard let url = _url else {
5050
return nil
5151
}
52-
5352
guard let scheme = url.scheme else {
5453
return nil
5554
}
55+
/*
56+
The issue still exists as of macOS 14.4, iOS 17.0
57+
https://stackoverflow.com/questions/74445926/url-host-deprecated-but-replacement-crashes
58+
https://forums.swift.org/t/does-url-query-percentencoded-calls-url-host-percentencoded-under-the-hood/70452
59+
https://forums.developer.apple.com/forums/thread/722451
60+
*/
61+
guard let hostname = url.host else {
62+
return nil
63+
}
5664
var port = (url.port == nil) ? "" : String(url.port!)
5765
if (scheme == "http" && port == "80") { port = "" }
5866
if (scheme == "https" && port == "443") { port = "" }
5967
if #available(macOS 13.0, iOS 16.0, *) {
60-
let hostname = url.host(percentEncoded: true) ?? ""
68+
// let hostname = url.host(percentEncoded: true) ?? ""
6169
let host = (port == "") ? hostname : "\(hostname):\(port)"
6270
let query = url.query(percentEncoded: true) ?? ""
6371
let fragment = url.fragment(percentEncoded: true) ?? ""
@@ -75,7 +83,6 @@ func jsLikeURL(_ urlString: String, baseString: String? = nil) -> [String: Strin
7583
"username": url.user(percentEncoded: true) ?? ""
7684
]
7785
} else {
78-
let hostname = url.host ?? ""
7986
let host = (port == "") ? hostname : "\(hostname):\(port)"
8087
let query = url.query ?? ""
8188
let fragment = url.fragment ?? ""

0 commit comments

Comments
 (0)