Skip to content

Commit 36dc24d

Browse files
VeykrilZed Zippy
authored andcommitted
gpui: Do not unwrap in window_procedure (#42216)
Technically these should not be possible to hit, but sentry says otherwise. Turning these into errors should give us more information than the abort due to unwinding across ffi boundaries. Fixes ZED-321 Release Notes: - N/A *or* Added/Fixed/Improved ...
1 parent 02cc776 commit 36dc24d

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

crates/gpui/src/platform/windows/platform.rs

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,17 @@ impl WindowsPlatform {
128128
Some(HWND_MESSAGE),
129129
None,
130130
None,
131-
Some(&context as *const _ as *const _),
131+
Some(&raw const context as *const _),
132132
)
133133
};
134-
let inner = context.inner.take().unwrap()?;
135-
let dispatcher = context.dispatcher.take().unwrap();
134+
let inner = context
135+
.inner
136+
.take()
137+
.context("CreateWindowExW did not run correctly")??;
138+
let dispatcher = context
139+
.dispatcher
140+
.take()
141+
.context("CreateWindowExW did not run correctly")?;
136142
let handle = result?;
137143

138144
let disable_direct_composition = std::env::var(DISABLE_DIRECT_COMPOSITION)
@@ -696,14 +702,24 @@ impl Platform for WindowsPlatform {
696702
impl WindowsPlatformInner {
697703
fn new(context: &mut PlatformWindowCreateContext) -> Result<Rc<Self>> {
698704
let state = RefCell::new(WindowsPlatformState::new(
699-
context.directx_devices.take().unwrap(),
705+
context
706+
.directx_devices
707+
.take()
708+
.context("missing directx devices")?,
700709
));
701710
Ok(Rc::new(Self {
702711
state,
703712
raw_window_handles: context.raw_window_handles.clone(),
704-
dispatcher: context.dispatcher.as_ref().unwrap().clone(),
713+
dispatcher: context
714+
.dispatcher
715+
.as_ref()
716+
.context("missing dispatcher")?
717+
.clone(),
705718
validation_number: context.validation_number,
706-
main_receiver: context.main_receiver.take().unwrap(),
719+
main_receiver: context
720+
.main_receiver
721+
.take()
722+
.context("missing main receiver")?,
707723
}))
708724
}
709725

@@ -1152,13 +1168,16 @@ unsafe extern "system" fn window_procedure(
11521168
lparam: LPARAM,
11531169
) -> LRESULT {
11541170
if msg == WM_NCCREATE {
1155-
let params = lparam.0 as *const CREATESTRUCTW;
1156-
let params = unsafe { &*params };
1171+
let params = unsafe { &*(lparam.0 as *const CREATESTRUCTW) };
11571172
let creation_context = params.lpCreateParams as *mut PlatformWindowCreateContext;
11581173
let creation_context = unsafe { &mut *creation_context };
11591174

1175+
let Some(main_sender) = creation_context.main_sender.take() else {
1176+
creation_context.inner = Some(Err(anyhow!("missing main sender")));
1177+
return LRESULT(0);
1178+
};
11601179
creation_context.dispatcher = Some(Arc::new(WindowsDispatcher::new(
1161-
creation_context.main_sender.take().unwrap(),
1180+
main_sender,
11621181
hwnd,
11631182
creation_context.validation_number,
11641183
)));

crates/gpui/src/platform/windows/window.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,8 +1166,7 @@ unsafe extern "system" fn window_procedure(
11661166
lparam: LPARAM,
11671167
) -> LRESULT {
11681168
if msg == WM_NCCREATE {
1169-
let window_params = lparam.0 as *const CREATESTRUCTW;
1170-
let window_params = unsafe { &*window_params };
1169+
let window_params = unsafe { &*(lparam.0 as *const CREATESTRUCTW) };
11711170
let window_creation_context = window_params.lpCreateParams as *mut WindowCreateContext;
11721171
let window_creation_context = unsafe { &mut *window_creation_context };
11731172
return match WindowsWindowInner::new(window_creation_context, hwnd, window_params) {

0 commit comments

Comments
 (0)