From 31b96a44de7465ff47c5cc4e5e20e9fe33771104 Mon Sep 17 00:00:00 2001 From: fufesou Date: Tue, 16 May 2023 16:11:45 +0800 Subject: [PATCH 1/5] remove assert Signed-off-by: fufesou --- src/plugin/callback_msg.rs | 2 +- src/plugin/mod.rs | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/plugin/callback_msg.rs b/src/plugin/callback_msg.rs index 9a82adfec..826aa159f 100644 --- a/src/plugin/callback_msg.rs +++ b/src/plugin/callback_msg.rs @@ -311,7 +311,7 @@ fn request_plugin_sign(id: String, msg_to_rustdesk: MsgToRustDesk) -> PluginRetu &[], ) { Ok(ret) => { - assert!(!ret.msg.is_null()); + debug_assert!(!ret.msg.is_null(), "msg is null"); let msg = cstr_to_string(ret.msg).unwrap_or_default(); free_c_ptr(ret.msg as _); if ret.code == super::errno::ERR_SUCCESS { diff --git a/src/plugin/mod.rs b/src/plugin/mod.rs index f19f67dd1..d0b326dc3 100644 --- a/src/plugin/mod.rs +++ b/src/plugin/mod.rs @@ -80,7 +80,10 @@ impl PluginReturn { if self.is_success() { (self.code, "".to_owned()) } else { - debug_assert!(!self.msg.is_null(), "msg is null"); + if self.msg.is_null() { + log::warn!("The message pointer is null"); + return (self.code, "".to_owned()) + } let msg = cstr_to_string(self.msg).unwrap_or_default(); free_c_ptr(self.msg as _); self.msg = null(); @@ -146,7 +149,6 @@ fn get_uninstall_file_path() -> ResultType { #[inline] fn cstr_to_string(cstr: *const c_char) -> ResultType { - assert!(!cstr.is_null(), "cstr must be a valid pointer"); if cstr.is_null() { bail!("failed to convert string, the pointer is null"); } From 607f2d5d9da823c2bca0f1cbd53cf7f5d3115076 Mon Sep 17 00:00:00 2001 From: fufesou Date: Tue, 16 May 2023 17:17:20 +0800 Subject: [PATCH 2/5] better handle of null pointer Signed-off-by: fufesou --- src/plugin/callback_msg.rs | 9 +++++++++ src/plugin/mod.rs | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/plugin/callback_msg.rs b/src/plugin/callback_msg.rs index 826aa159f..dfb0be27c 100644 --- a/src/plugin/callback_msg.rs +++ b/src/plugin/callback_msg.rs @@ -312,6 +312,15 @@ fn request_plugin_sign(id: String, msg_to_rustdesk: MsgToRustDesk) -> PluginRetu ) { Ok(ret) => { debug_assert!(!ret.msg.is_null(), "msg is null"); + if ret.msg.is_null() { + // unreachable + log::error!( + "The returned message pointer of plugin status is null, plugin id: '{}', code: {}", + id, + ret.code, + ); + return PluginReturn::success(); + } let msg = cstr_to_string(ret.msg).unwrap_or_default(); free_c_ptr(ret.msg as _); if ret.code == super::errno::ERR_SUCCESS { diff --git a/src/plugin/mod.rs b/src/plugin/mod.rs index d0b326dc3..be6e49ea8 100644 --- a/src/plugin/mod.rs +++ b/src/plugin/mod.rs @@ -81,7 +81,7 @@ impl PluginReturn { (self.code, "".to_owned()) } else { if self.msg.is_null() { - log::warn!("The message pointer is null"); + log::warn!("The message pointer from the plugin is null, but the error code is {}", self.code); return (self.code, "".to_owned()) } let msg = cstr_to_string(self.msg).unwrap_or_default(); From e611125b865f631bffd827a30c1dc0d8801bf31d Mon Sep 17 00:00:00 2001 From: fufesou Date: Tue, 16 May 2023 17:22:07 +0800 Subject: [PATCH 3/5] better log on error Signed-off-by: fufesou --- src/plugin/mod.rs | 10 +++++++--- src/plugin/plugins.rs | 10 +++++----- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/plugin/mod.rs b/src/plugin/mod.rs index be6e49ea8..bc97e5cfb 100644 --- a/src/plugin/mod.rs +++ b/src/plugin/mod.rs @@ -76,13 +76,17 @@ impl PluginReturn { } } - pub fn get_code_msg(&mut self) -> (i32, String) { + pub fn get_code_msg(&mut self, id: &str) -> (i32, String) { if self.is_success() { (self.code, "".to_owned()) } else { if self.msg.is_null() { - log::warn!("The message pointer from the plugin is null, but the error code is {}", self.code); - return (self.code, "".to_owned()) + log::warn!( + "The message pointer from the plugin '{}' is null, but the error code is {}", + id, + self.code + ); + return (self.code, "".to_owned()); } let msg = cstr_to_string(self.msg).unwrap_or_default(); free_c_ptr(self.msg as _); diff --git a/src/plugin/plugins.rs b/src/plugin/plugins.rs index 83308b643..b969e2a82 100644 --- a/src/plugin/plugins.rs +++ b/src/plugin/plugins.rs @@ -213,7 +213,7 @@ macro_rules! make_plugin { fn init(&self, data: &InitData, path: &str) -> ResultType<()> { let mut init_ret = (self.init)(data as _); if !init_ret.is_success() { - let (code, msg) = init_ret.get_code_msg(); + let (code, msg) = init_ret.get_code_msg(path); bail!( "Failed to init plugin {}, code: {}, msg: {}", path, @@ -227,7 +227,7 @@ macro_rules! make_plugin { fn clear(&self, id: &str) { let mut clear_ret = (self.clear)(); if !clear_ret.is_success() { - let (code, msg) = clear_ret.get_code_msg(); + let (code, msg) = clear_ret.get_code_msg(id); log::error!( "Failed to clear plugin {}, code: {}, msg: {}", id, @@ -428,7 +428,7 @@ pub fn plugin_call(id: &str, method: &[u8], peer: &str, event: &[u8]) -> ResultT if ret.is_success() { Ok(()) } else { - let (code, msg) = ret.get_code_msg(); + let (code, msg) = ret.get_code_msg(id); bail!( "Failed to handle plugin event, id: {}, method: {}, code: {}, msg: {}", id, @@ -496,7 +496,7 @@ fn _handle_listen_event(event: String, peer: String) { evt_bytes.len(), ); if !ret.is_success() { - let (code, msg) = ret.get_code_msg(); + let (code, msg) = ret.get_code_msg(&id); log::error!( "Failed to handle plugin listen event, id: {}, event: {}, code: {}, msg: {}", id, @@ -540,7 +540,7 @@ pub fn handle_client_event(id: &str, peer: &str, event: &[u8]) -> Message { free_c_ptr(out as _); msg } else { - let (code, msg) = ret.get_code_msg(); + let (code, msg) = ret.get_code_msg(id); if code > ERR_RUSTDESK_HANDLE_BASE && code < ERR_PLUGIN_HANDLE_BASE { log::debug!( "Plugin {} failed to handle client event, code: {}, msg: {}", From 029c90897b7dfa1f5e39f25e0c7606105354ce63 Mon Sep 17 00:00:00 2001 From: fufesou Date: Tue, 16 May 2023 17:26:58 +0800 Subject: [PATCH 4/5] fix build Signed-off-by: fufesou --- src/plugin/callback_msg.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/plugin/callback_msg.rs b/src/plugin/callback_msg.rs index dfb0be27c..85288a8f5 100644 --- a/src/plugin/callback_msg.rs +++ b/src/plugin/callback_msg.rs @@ -279,7 +279,6 @@ fn request_plugin_sign(id: String, msg_to_rustdesk: MsgToRustDesk) -> PluginRetu "parse signature data '{}'", signature_data ); - // to-do: Request server to sign the data. thread::spawn(move || { let sign_url = format!("{}/lic/web/api/plugin-sign", get_api_server()); let client = reqwest::blocking::Client::new(); @@ -319,7 +318,6 @@ fn request_plugin_sign(id: String, msg_to_rustdesk: MsgToRustDesk) -> PluginRetu id, ret.code, ); - return PluginReturn::success(); } let msg = cstr_to_string(ret.msg).unwrap_or_default(); free_c_ptr(ret.msg as _); From 3f8c9a9799edbe59c9033413a891eab85da37eae Mon Sep 17 00:00:00 2001 From: fufesou Date: Tue, 16 May 2023 18:38:06 +0800 Subject: [PATCH 5/5] fix early return Signed-off-by: fufesou --- src/plugin/callback_msg.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/plugin/callback_msg.rs b/src/plugin/callback_msg.rs index 85288a8f5..2a3b43247 100644 --- a/src/plugin/callback_msg.rs +++ b/src/plugin/callback_msg.rs @@ -318,6 +318,7 @@ fn request_plugin_sign(id: String, msg_to_rustdesk: MsgToRustDesk) -> PluginRetu id, ret.code, ); + return; } let msg = cstr_to_string(ret.msg).unwrap_or_default(); free_c_ptr(ret.msg as _);