Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Commit

Permalink
Fix #7484 - Fix migration crash when parsing an invalid URL (#7486)
Browse files Browse the repository at this point in the history
Fix potential issues in migration where users URLs can somehow not be parsed by both Chromium and Foundation :S
This results in their being 0 history to migrate, and gives use UInt(-1) which cannot overflow, thus giving a crash.
  • Loading branch information
Brandon-T authored and iccub committed May 19, 2023
1 parent 17aa8f3 commit 8891c52
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions Sources/Brave/Migration/Migration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,25 @@ public class Migration {
let historySnapshot = oldTab.urlHistorySnapshot as? [String] ?? []

for url in historySnapshot {
guard let url = NSURL(idnString: url) as? URL ?? URL(string: url) else { continue }
guard let url = NSURL(idnString: url) as? URL ?? URL(string: url) else {
Logger.module.error("Failed to parse URL: \(url) during Migration!")
continue
}
if let internalUrl = InternalURL(url), !internalUrl.isAuthorized, let authorizedURL = InternalURL.authorize(url: url) {
historyURLs.append(authorizedURL)
} else {
historyURLs.append(url)
}
}

if historyURLs.count == 0 {
Logger.module.error("User has zero history to migrate!")
return
}

// currentPage is -webView.backForwardList.forwardList.count
let currentPage = (historyURLs.count - 1) + Int(oldTab.urlHistoryCurrentIndex)
// If for some reason current page can be negative, we clamp it to [0, inf].
let currentPage = max((historyURLs.count - 1) + Int(oldTab.urlHistoryCurrentIndex), 0)

// Create WebKit interactionState
let interactionState = SynthesizedSessionRestore.serialize(withTitle: tabTitle,
Expand Down

0 comments on commit 8891c52

Please sign in to comment.