Skip to content

Commit 9a27a77

Browse files
authored
Merge pull request #49 from ccbrown/fix-char-p-to-string
fix double free resulting from char_p_to_string
2 parents 3e380f1 + 3a6b1da commit 9a27a77

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

onnxruntime/src/lib.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ fn g_ort() -> sys::OrtApi {
163163
}
164164

165165
fn char_p_to_string(raw: *const i8) -> Result<String> {
166-
let c_string = unsafe { std::ffi::CString::from_raw(raw as *mut i8) };
166+
let c_string = unsafe { std::ffi::CStr::from_ptr(raw as *mut i8).to_owned() };
167167

168168
match c_string.into_string() {
169169
Ok(string) => Ok(string),
@@ -480,3 +480,15 @@ impl Into<sys::OrtMemType> for MemType {
480480
}
481481
}
482482
}
483+
484+
#[cfg(test)]
485+
mod test {
486+
use super::*;
487+
488+
#[test]
489+
fn test_char_p_to_string() {
490+
let s = std::ffi::CString::new("foo").unwrap();
491+
let ptr = s.as_c_str().as_ptr();
492+
assert_eq!("foo", char_p_to_string(ptr).unwrap());
493+
}
494+
}

0 commit comments

Comments
 (0)