From a56693d9e830fa4defc1b748144d571b9dc8271d Mon Sep 17 00:00:00 2001 From: Richard Feldman Date: Tue, 11 Nov 2025 16:24:37 -0500 Subject: [PATCH] Fix panic when opening an invalid URL (#42483) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Now instead of a panic we see this: Screenshot 2025-11-11 at 3 47 25 PM Release Notes: - Trying to open invalid URLs in a browser now shows an error instead of panicking --- crates/gpui/src/platform/mac/platform.rs | 9 ++++++--- crates/zed/src/zed.rs | 19 ++++++++++++++++++- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/crates/gpui/src/platform/mac/platform.rs b/crates/gpui/src/platform/mac/platform.rs index 101520cdbbc220a49b1fa56584729ad93d507fe7..21dab9d3b617d505b80cb2d48b579916a9eb1238 100644 --- a/crates/gpui/src/platform/mac/platform.rs +++ b/crates/gpui/src/platform/mac/platform.rs @@ -651,9 +651,12 @@ impl Platform for MacPlatform { fn open_url(&self, url: &str) { unsafe { - let url = NSURL::alloc(nil) - .initWithString_(ns_string(url)) - .autorelease(); + let ns_url = NSURL::alloc(nil).initWithString_(ns_string(url)); + if ns_url.is_null() { + log::error!("Failed to create NSURL from string: {}", url); + return; + } + let url = ns_url.autorelease(); let workspace: id = msg_send![class!(NSWorkspace), sharedWorkspace]; msg_send![workspace, openURL: url] } diff --git a/crates/zed/src/zed.rs b/crates/zed/src/zed.rs index 7cdaea920f4b90de4393dd08e0c855ecd1cb2f88..cbd6fba694136c87c64c4d6ca2bfee6d1e1072dd 100644 --- a/crates/zed/src/zed.rs +++ b/crates/zed/src/zed.rs @@ -726,7 +726,24 @@ fn register_actions( ..Default::default() }) }) - .register_action(|_, action: &OpenBrowser, _window, cx| cx.open_url(&action.url)) + .register_action(|workspace, action: &OpenBrowser, _window, cx| { + // Parse and validate the URL to ensure it's properly formatted + match url::Url::parse(&action.url) { + Ok(parsed_url) => { + // Use the parsed URL's string representation which is properly escaped + cx.open_url(parsed_url.as_str()); + } + Err(e) => { + workspace.show_error( + &anyhow::anyhow!( + "Opening this URL in a browser failed because the URL is invalid: {}\n\nError was: {e}", + action.url + ), + cx, + ); + } + } + }) .register_action(|workspace, _: &workspace::Open, window, cx| { telemetry::event!("Project Opened"); let paths = workspace.prompt_for_open_path(