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/update.rs | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) (limited to 'rust/src/update.rs') diff --git a/rust/src/update.rs b/rust/src/update.rs index aa8c7daf..b3c45d84 100644 --- a/rust/src/update.rs +++ b/rust/src/update.rs @@ -1,10 +1,10 @@ #![allow(dead_code)] -use std::ffi::{c_char, c_void}; +use std::ffi::c_void; use std::time::{Duration, SystemTime, UNIX_EPOCH}; use crate::progress::{NoProgressCallback, ProgressCallback}; use crate::rc::{Array, CoreArrayProvider, CoreArrayProviderInner}; -use crate::string::{raw_to_string, BnString}; +use crate::string::{raw_to_string, AsCStr, BnString}; use binaryninjacore_sys::*; pub type UpdateResult = BNUpdateResult; @@ -96,8 +96,9 @@ impl UpdateChannel { pub fn versions(&self) -> Result, BnString> { let mut count = 0; let mut errors = std::ptr::null_mut(); + let name = self.name.clone().to_cstr(); let result = unsafe { - BNGetUpdateChannelVersions(self.name.as_ptr() as *const c_char, &mut count, &mut errors) + BNGetUpdateChannelVersions(name.as_ptr(), &mut count, &mut errors) }; if !errors.is_null() { Err(unsafe { BnString::from_raw(errors) }) @@ -122,9 +123,10 @@ impl UpdateChannel { /// Whether updates are available pub fn updates_available(&self) -> Result { let mut errors = std::ptr::null_mut(); + let name = self.name.clone().to_cstr(); let result = unsafe { BNAreUpdatesAvailable( - self.name.as_ptr() as *const c_char, + name.as_ptr(), std::ptr::null_mut(), std::ptr::null_mut(), &mut errors, @@ -147,9 +149,10 @@ impl UpdateChannel { ) -> Result { let mut errors = std::ptr::null_mut(); + let name = self.name.clone().to_cstr(); let result = unsafe { BNUpdateToLatestVersion( - self.name.as_ptr() as *const c_char, + name.as_ptr(), &mut errors, Some(P::cb_progress_callback), &mut progress as *mut P as *mut c_void, @@ -174,10 +177,12 @@ impl UpdateChannel { ) -> Result { let mut errors = std::ptr::null_mut(); + let name = self.name.clone().to_cstr(); + let version = version.version.clone().to_cstr(); let result = unsafe { BNUpdateToVersion( - self.name.as_ptr() as *const c_char, - version.version.as_ptr() as *const c_char, + name.as_ptr(), + version.as_ptr(), &mut errors, Some(P::cb_progress_callback), &mut progress as *mut P as *mut c_void, -- cgit v1.3.1