Skip to content

Commit

Permalink
Fix IPC crash on wkwebview if receiving invalid types (#1097)
Browse files Browse the repository at this point in the history
  • Loading branch information
wusyong committed Nov 28, 2023
1 parent a4a39b9 commit a8c0d38
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
6 changes: 6 additions & 0 deletions .changes/ipc-fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"wry": patch
---

Fix IPC crash on wkwebview if receiving invalid types.

16 changes: 9 additions & 7 deletions src/wkwebview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,17 @@ impl InnerWebView {
if !function.is_null() {
let function = &mut *(*function as *mut Box<dyn Fn(String)>);
let body: id = msg_send![msg, body];
let utf8: *const c_char = msg_send![body, UTF8String];
if let Ok(js) = CStr::from_ptr(utf8).to_str() {
(function)(js.to_string());
} else {
log::warn!("WebView received invalid UTF8 string from IPC.");
let is_string: bool = msg_send![body, isKindOfClass: class!(NSString)];
if is_string {
let utf8: *const c_char = msg_send![body, UTF8String];
if let Ok(js) = CStr::from_ptr(utf8).to_str() {
(function)(js.to_string());
return;
}
}
} else {
log::warn!("WebView instance is dropped! This handler shouldn't be called.");
}

log::warn!("WebView received invalid IPC call.");
}
}

Expand Down

0 comments on commit a8c0d38

Please sign in to comment.