From 788a8b7091bbdde77817030e0836d7a7a786fd99 Mon Sep 17 00:00:00 2001 From: Mason Reed Date: Sun, 4 May 2025 19:47:55 -0400 Subject: [Rust] Simplify usage surrounding c strings `cstring.as_ref().as_ptr() as *const c_char` -> `cstring.as_ptr()` `cstring.as_ref().as_ptr() as *mut _` -> `cstring.as_ptr()` `cstring.as_ptr() as *const c_char` -> `cstring.as_ptr()` With a few fixes for cstrings that might be dropped prematurely. --- rust/src/websocket/client.rs | 14 ++++---------- rust/src/websocket/provider.rs | 7 +++---- 2 files changed, 7 insertions(+), 14 deletions(-) (limited to 'rust/src/websocket') diff --git a/rust/src/websocket/client.rs b/rust/src/websocket/client.rs index 43e1e1f7..e9e753b1 100644 --- a/rust/src/websocket/client.rs +++ b/rust/src/websocket/client.rs @@ -86,14 +86,8 @@ impl CoreWebsocketClient { .into_iter() .map(|(k, v)| (k.to_cstr(), v.to_cstr())) .unzip(); - let header_keys: Vec<*const c_char> = header_keys - .iter() - .map(|k| k.as_ref().as_ptr() as *const c_char) - .collect(); - let header_values: Vec<*const c_char> = header_values - .iter() - .map(|v| v.as_ref().as_ptr() as *const c_char) - .collect(); + let header_keys: Vec<*const c_char> = header_keys.iter().map(|k| k.as_ptr()).collect(); + let header_values: Vec<*const c_char> = header_values.iter().map(|v| v.as_ptr()).collect(); // SAFETY: This context will only be live for the duration of BNConnectWebsocketClient // SAFETY: Any subsequent call to BNConnectWebsocketClient will write over the context. let mut output_callbacks = BNWebsocketClientOutputCallbacks { @@ -106,7 +100,7 @@ impl CoreWebsocketClient { unsafe { BNConnectWebsocketClient( self.handle.as_ptr(), - url.as_ptr() as *const c_char, + url.as_ptr(), header_keys.len().try_into().unwrap(), header_keys.as_ptr(), header_values.as_ptr(), @@ -131,7 +125,7 @@ impl CoreWebsocketClient { pub fn notify_error(&self, msg: &str) { let error = msg.to_cstr(); unsafe { - BNNotifyWebsocketClientError(self.handle.as_ptr(), error.as_ptr() as *const c_char) + BNNotifyWebsocketClientError(self.handle.as_ptr(), error.as_ptr()) } } diff --git a/rust/src/websocket/provider.rs b/rust/src/websocket/provider.rs index 48c198d2..9cc1de84 100644 --- a/rust/src/websocket/provider.rs +++ b/rust/src/websocket/provider.rs @@ -3,7 +3,7 @@ use crate::string::{AsCStr, BnString}; use crate::websocket::client; use crate::websocket::client::{CoreWebsocketClient, WebsocketClient}; use binaryninjacore_sys::*; -use std::ffi::{c_char, c_void}; +use std::ffi::c_void; use std::mem::MaybeUninit; use std::ptr::NonNull; @@ -17,7 +17,7 @@ where let leaked_provider = Box::leak(Box::new(provider_uninit)); let result = unsafe { BNRegisterWebsocketProvider( - name.as_ptr() as *const c_char, + name.as_ptr(), &mut BNWebsocketProviderCallbacks { context: leaked_provider as *mut _ as *mut c_void, createClient: Some(cb_create_client::), @@ -82,8 +82,7 @@ impl CoreWebsocketProvider { pub fn by_name(name: S) -> Option { let name = name.to_cstr(); - let result = - unsafe { BNGetWebsocketProviderByName(name.as_ref().as_ptr() as *const c_char) }; + let result = unsafe { BNGetWebsocketProviderByName(name.as_ptr()) }; NonNull::new(result).map(|h| unsafe { Self::from_raw(h) }) } -- cgit v1.3.1