Skip to content

Commit 2cf7fa6

Browse files
Merge pull request #59 from andrewdavidmackenzie/create_c_string_for_path
Fix failures on rustc >= 1.46.0
2 parents b0e862f + 66dcfdf commit 2cf7fa6

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/libproc/proc_pid.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ use self::libc::{c_char, readlink};
3030
#[cfg(target_os = "macos")]
3131
use self::libc::{c_int, c_void, c_char};
3232

33+
#[cfg(target_os = "macos")]
34+
use std::ffi::CString;
35+
3336
// Since we cannot access C macros for constants from Rust - I have had to redefine this, based on Apple's source code
3437
// See http://opensource.apple.com/source/Libc/Libc-594.9.4/darwin/libproc.c
3538
// buffersize must be more than PROC_PIDPATHINFO_SIZE
@@ -229,8 +232,10 @@ pub fn listpids(proc_types: ProcType) -> Result<Vec<u32>, String> {
229232
// -1 if an error was encountered;
230233
#[cfg(target_os = "macos")]
231234
pub fn listpidspath(proc_types: ProcType, path: &str) -> Result<Vec<u32>, String> {
235+
let c_path = CString::new(path).map_err(|_| "CString::new failed".to_string())?;
236+
232237
let buffer_size = unsafe {
233-
proc_listpidspath(proc_types as u32, 0, path.as_ptr() as * const c_char, 0, ptr::null_mut(), 0)
238+
proc_listpidspath(proc_types as u32, 0, c_path.as_ptr() as * const c_char, 0, ptr::null_mut(), 0)
234239
};
235240
if buffer_size <= 0 {
236241
return Err(helpers::get_errno_with_message(buffer_size));
@@ -241,7 +246,7 @@ pub fn listpidspath(proc_types: ProcType, path: &str) -> Result<Vec<u32>, String
241246
let buffer_ptr = pids.as_mut_ptr() as *mut c_void;
242247

243248
let ret = unsafe {
244-
proc_listpidspath(proc_types as u32, 0, path.as_ptr() as * const c_char, 0, buffer_ptr, buffer_size as u32)
249+
proc_listpidspath(proc_types as u32, 0, c_path.as_ptr() as * const c_char, 0, buffer_ptr, buffer_size as u32)
245250
};
246251

247252
if ret <= 0 {

0 commit comments

Comments
 (0)