Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
Fix panic when opening an invalid URL (#42483)
Richard Feldmana56693d9e830
diff --git a/crates/gpui/src/platform/mac/platform.rs b/crates/gpui/src/platform/mac/platform.rsindex 101520cdbbc..21dab9d3b61 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.rsindex 7cdaea920f4..cbd6fba6941 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(