Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: avoid two potential thread crashes #714

Merged
merged 2 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions xcode/Ext-Safari/Functions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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..<content.endIndex].starts(with: "//")) {
let contentEndIndex = content.index(contentStartIndex, offsetBy: 15)
let metaHeadContent = content[contentStartIndex..<contentEndIndex]
var g1, g2, g3: Int
if (metaHeadContent.starts(with: "//")) {
g1 = 1; g2 = 2; g3 = 3
} else {
g1 = 4; g2 = 5; g3 = 6
}

// unlikely to happen but did see some crashes, add checking and logging
guard let metablockRange = Range(match.range(at: g1), in: content) else {
logger?.error("\(#function, privacy: .public) - Nil range (\(g1, privacy: .public)): \(metaHeadContent, privacy: .public)")
logger?.debug("\(#function, privacy: .public) - Nil range content: \(content, privacy: .public)")
return nil
}
// can force unwrap metablock since nil check was done above
let metablock = content[Range(match.range(at: g1), in: content)!]
let metablock = content[metablockRange]
// create var to store separated metadata keys/values
var metadata = [:] as [String: [String]]
// iterate through the possible metadata keys in file
Expand Down
13 changes: 10 additions & 3 deletions xcode/Shared/UrlPolyfill.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) ?? ""
Expand All @@ -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 ?? ""
Expand Down
Loading