Skip to content

Commit

Permalink
Mark cstr_to_string as unsafe
Browse files Browse the repository at this point in the history
In Rust, having an unsafe interior conventionally means that you are
guaranteeing safety at the boundary. However, that's not the case for
cstr_to_string which is taking a random pointer as input. Thankfully,
this doesn't functionally change anything anyway since it was already
always called from an unsafe context.
  • Loading branch information
vlovich committed Jan 22, 2025
1 parent 580928a commit 2717e6f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
4 changes: 2 additions & 2 deletions crates/sherpa-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ pub struct OfflineRecognizerResult {

impl OfflineRecognizerResult {
fn new(result: &sherpa_rs_sys::SherpaOnnxOfflineRecognizerResult) -> Self {
let lang = cstr_to_string(result.lang);
let text = cstr_to_string(result.text);
let lang = unsafe { cstr_to_string(result.lang) };
let text = unsafe { cstr_to_string(result.text) };
let count = result.count.try_into().unwrap();
let timestamps = if result.timestamps.is_null() {
Vec::new()
Expand Down
12 changes: 5 additions & 7 deletions crates/sherpa-rs/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ pub fn cstring_from_str(s: &str) -> CString {
return CString::new(s).expect("CString::new failed");
}

pub fn cstr_to_string(ptr: *const c_char) -> String {
unsafe {
if ptr.is_null() {
String::new()
} else {
std::ffi::CStr::from_ptr(ptr).to_string_lossy().into_owned()
}
pub unsafe fn cstr_to_string(ptr: *const c_char) -> String {
if ptr.is_null() {
String::new()
} else {
std::ffi::CStr::from_ptr(ptr).to_string_lossy().into_owned()
}
}

0 comments on commit 2717e6f

Please sign in to comment.