diff --git a/src/id/id.rs b/src/id/id.rs index 4b7b97126..829d48140 100644 --- a/src/id/id.rs +++ b/src/id/id.rs @@ -17,6 +17,7 @@ extern crate getopts; extern crate libc; +use std::ffi::c_str_to_bytes; use std::ptr::read; use libc::{ uid_t, @@ -136,8 +137,8 @@ pub fn uumain(args: Vec) -> isize { }; let gr = unsafe { getgrgid(id) }; - if nflag && gr.is_not_null() { - let gr_name = unsafe { String::from_raw_buf(read(gr).gr_name as *const u8) }; + if nflag && !gr.is_null() { + let gr_name = unsafe { String::from_utf8_lossy(c_str_to_bytes(&read(gr).gr_name)).to_string() }; println!("{}", gr_name); } else { println!("{}", id); @@ -155,9 +156,9 @@ pub fn uumain(args: Vec) -> isize { }; let pw = unsafe { getpwuid(id) }; - if nflag && pw.is_not_null() { + if nflag && !pw.is_null() { let pw_name = unsafe { - String::from_raw_buf(read(pw).pw_name as *const u8) + String::from_utf8_lossy(c_str_to_bytes(&read(pw).pw_name)).to_string() }; println!("{}", pw_name); } else { @@ -195,26 +196,26 @@ fn pretty(possible_pw: Option) { if possible_pw.is_some() { let pw = possible_pw.unwrap(); - let pw_name = unsafe { String::from_raw_buf(pw.pw_name as *const u8) }; + let pw_name = unsafe { String::from_utf8_lossy(c_str_to_bytes(&pw.pw_name)).to_string() }; print!("uid\t{}\ngroups\t", pw_name); group(possible_pw, true); } else { - let login = unsafe { String::from_raw_buf(getlogin() as *const u8) }; + let login = unsafe { String::from_utf8_lossy(c_str_to_bytes(&(getlogin() as *const i8))).to_string() }; let rid = unsafe { getuid() }; let pw = unsafe { getpwuid(rid) }; let is_same_user = unsafe { - String::from_raw_buf(read(pw).pw_name as *const u8) == login + String::from_utf8_lossy(c_str_to_bytes(&read(pw).pw_name)).to_string() == login }; if pw.is_null() || is_same_user { println!("login\t{}", login); } - if pw.is_not_null() { + if !pw.is_null() { println!( "uid\t{}", - unsafe { String::from_raw_buf(read(pw).pw_name as *const u8) }) + unsafe { String::from_utf8_lossy(c_str_to_bytes(&read(pw).pw_name)).to_string() }) } else { println!("uid\t{}\n", rid); } @@ -222,10 +223,10 @@ fn pretty(possible_pw: Option) { let eid = unsafe { getegid() }; if eid == rid { let pw = unsafe { getpwuid(eid) }; - if pw.is_not_null() { + if !pw.is_null() { println!( "euid\t{}", - unsafe { String::from_raw_buf(read(pw).pw_name as *const u8) }); + unsafe { String::from_utf8_lossy(c_str_to_bytes(&read(pw).pw_name)).to_string() }); } else { println!("euid\t{}", eid); } @@ -235,10 +236,10 @@ fn pretty(possible_pw: Option) { if rid != eid { let gr = unsafe { getgrgid(rid) }; - if gr.is_not_null() { + if !gr.is_null() { println!( "rgid\t{}", - unsafe { String::from_raw_buf(read(gr).gr_name as *const u8) }); + unsafe { String::from_utf8_lossy(c_str_to_bytes(&read(gr).gr_name)).to_string() }); } else { println!("rgid\t{}", rid); } @@ -257,12 +258,12 @@ fn pline(possible_pw: Option) { possible_pw.unwrap() }; - let pw_name = unsafe { String::from_raw_buf(pw.pw_name as *const u8) }; - let pw_passwd = unsafe { String::from_raw_buf(pw.pw_passwd as *const u8) }; - let pw_class = unsafe { String::from_raw_buf(pw.pw_class as *const u8) }; - let pw_gecos = unsafe { String::from_raw_buf(pw.pw_gecos as *const u8) }; - let pw_dir = unsafe { String::from_raw_buf(pw.pw_dir as *const u8) }; - let pw_shell = unsafe { String::from_raw_buf(pw.pw_shell as *const u8) }; + let pw_name = unsafe { String::from_utf8_lossy(c_str_to_bytes(&pw.pw_name )).to_string()}; + let pw_passwd = unsafe { String::from_utf8_lossy(c_str_to_bytes(&pw.pw_passwd)).to_string()}; + let pw_class = unsafe { String::from_utf8_lossy(c_str_to_bytes(&pw.pw_class )).to_string()}; + let pw_gecos = unsafe { String::from_utf8_lossy(c_str_to_bytes(&pw.pw_gecos )).to_string()}; + let pw_dir = unsafe { String::from_utf8_lossy(c_str_to_bytes(&pw.pw_dir )).to_string()}; + let pw_shell = unsafe { String::from_utf8_lossy(c_str_to_bytes(&pw.pw_shell )).to_string()}; println!( "{}:{}:{}:{}:{}:{}:{}:{}:{}:{}", @@ -286,11 +287,11 @@ fn pline(possible_pw: Option) { possible_pw.unwrap() }; - let pw_name = unsafe { String::from_raw_buf(pw.pw_name as *const u8)}; - let pw_passwd = unsafe { String::from_raw_buf(pw.pw_passwd as *const u8)}; - let pw_gecos = unsafe { String::from_raw_buf(pw.pw_gecos as *const u8)}; - let pw_dir = unsafe { String::from_raw_buf(pw.pw_dir as *const u8)}; - let pw_shell = unsafe { String::from_raw_buf(pw.pw_shell as *const u8)}; + let pw_name = unsafe { String::from_utf8_lossy(c_str_to_bytes(&pw.pw_name )).to_string()}; + let pw_passwd = unsafe { String::from_utf8_lossy(c_str_to_bytes(&pw.pw_passwd)).to_string()}; + let pw_gecos = unsafe { String::from_utf8_lossy(c_str_to_bytes(&pw.pw_gecos )).to_string()}; + let pw_dir = unsafe { String::from_utf8_lossy(c_str_to_bytes(&pw.pw_dir )).to_string()}; + let pw_shell = unsafe { String::from_utf8_lossy(c_str_to_bytes(&pw.pw_shell )).to_string()}; println!( "{}:{}:{}:{}:{}:{}:{}", @@ -350,27 +351,27 @@ fn id_print(possible_pw: Option, print!( "uid={}({})", uid, - unsafe { String::from_raw_buf(possible_pw.unwrap().pw_name as *const u8) }); + unsafe { String::from_utf8_lossy(c_str_to_bytes(&possible_pw.unwrap().pw_name)).to_string() }); } else { print!("uid={}", unsafe { getuid() }); } print!(" gid={}", gid); let gr = unsafe { getgrgid(gid) }; - if gr.is_not_null() { + if !gr.is_null() { print!( "({})", - unsafe { String::from_raw_buf(read(gr).gr_name as *const u8) }); + unsafe { String::from_utf8_lossy(c_str_to_bytes(&read(gr).gr_name)).to_string() }); } let euid = unsafe { geteuid() }; if p_euid && (euid != uid) { print!(" euid={}", euid); let pw = unsafe { getpwuid(euid) }; - if pw.is_not_null() { + if !pw.is_null() { print!( "({})", - unsafe { String::from_raw_buf(read(pw).pw_name as *const u8) }); + unsafe { String::from_utf8_lossy(c_str_to_bytes(&read(pw).pw_name)).to_string() }); } } @@ -379,8 +380,8 @@ fn id_print(possible_pw: Option, print!(" egid={}", egid); unsafe { let grp = getgrgid(egid); - if grp.is_not_null() { - print!("({})", String::from_raw_buf(read(grp).gr_name as *const u8)); + if !grp.is_null() { + print!("({})", String::from_utf8_lossy(c_str_to_bytes(&read(grp).gr_name)).to_string()); } } } @@ -393,9 +394,9 @@ fn id_print(possible_pw: Option, if !first { print!(",") } print!("{}", gr); let group = unsafe { getgrgid(gr) }; - if group.is_not_null() { + if !group.is_null() { let name = unsafe { - String::from_raw_buf(read(group).gr_name as *const u8) + String::from_utf8_lossy(c_str_to_bytes(&read(group).gr_name)).to_string() }; print!("({})", name); } diff --git a/src/nice/nice.rs b/src/nice/nice.rs index 126f2b35b..6a7e4a7ca 100644 --- a/src/nice/nice.rs +++ b/src/nice/nice.rs @@ -12,6 +12,7 @@ extern crate getopts; extern crate libc; +use std::ffi::CString; use std::io::IoError; use std::os; use std::ptr; @@ -101,12 +102,10 @@ pub fn uumain(args: Vec) -> isize { show_warning!("{}", IoError::last_error()); } - unsafe { - let executable = matches.free[0].to_c_str().into_inner(); - let mut args: Vec<*const i8> = matches.free.iter().map(|x| x.to_c_str().into_inner()).collect(); - args.push(ptr::null()); - execvp(executable as *const c_char, args.as_ptr() as *mut *const c_char); - } + let cstrs : Vec = matches.free.iter().map(|x| CString::from_slice(x.as_bytes())).collect(); + let mut args : Vec<*const c_char> = cstrs.iter().map(|s| s.as_ptr()).collect(); + args.push(0 as *const c_char); + unsafe { execvp(args[0], args.as_mut_ptr()); } show_error!("{}", IoError::last_error()); if os::errno() as c_int == libc::ENOENT { 127 } else { 126 } diff --git a/src/nohup/nohup.rs b/src/nohup/nohup.rs index 78d5c51a1..1a72e3b68 100644 --- a/src/nohup/nohup.rs +++ b/src/nohup/nohup.rs @@ -13,9 +13,11 @@ extern crate getopts; extern crate libc; use getopts::{optflag, getopts, usage}; +use std::ffi::CString; use std::io::stdio::{stdin_raw, stdout_raw, stderr_raw}; use std::io::{File, Open, Read, Append, Write}; use std::os::unix::prelude::*; +use libc::c_char; use libc::funcs::posix88::unistd::{dup2, execvp}; use libc::consts::os::posix88::SIGHUP; use libc::funcs::posix01::signal::signal; @@ -66,13 +68,10 @@ pub fn uumain(args: Vec) -> isize { if unsafe { _vprocmgr_detach_from_console(0) } != std::ptr::null() { crash!(2, "Cannot detach from console")}; - unsafe { - // we ignore the memory leak here because it doesn't matter anymore - let executable = opts.free[0].as_slice().to_c_str().into_inner(); - let mut args: Vec<*const i8> = opts.free.iter().map(|x| x.to_c_str().into_inner()).collect(); - args.push(std::ptr::null()); - execvp(executable as *const libc::c_char, args.as_ptr() as *mut *const libc::c_char) as int - } + let cstrs : Vec = opts.free.iter().map(|x| CString::from_slice(x.as_bytes())).collect(); + let mut args : Vec<*const c_char> = cstrs.iter().map(|s| s.as_ptr()).collect(); + args.push(std::ptr::null()); + unsafe { execvp(args[0], args.as_mut_ptr()) as isize } } fn replace_fds() {