don't construct nullptr cstring

This commit is contained in:
Ryan Heywood 2025-07-12 14:42:50 -04:00
parent b3a16d49bc
commit 8bd07cccaa
Signed by: ryan
GPG Key ID: 8E401478A3FBEF72
1 changed files with 10 additions and 3 deletions

View File

@ -204,10 +204,17 @@ pub fn execv(command: impl AsRef<Path>, args: &[&str]) -> Result<()> {
args_cstr.push(cur_arg_cstr);
}
// NOTE: The last arg must be a null pointer. We construct a CString from the raw pointer.
args_cstr.push(unsafe { CString::from_raw(std::ptr::null_mut()) });
// NOTE: The last arg must be a null pointer, but we can't construct a CString from nullptr, so
// we construct an array of pointers and use that
let args_ptrs: Vec<_> = args
.iter()
.map(|s| s.as_ptr())
.chain(std::iter::once(std::ptr::null()))
.collect();
if unsafe { libc::execv(command_cstr.as_ptr().cast(), args_cstr.as_ptr().cast()) } == -1 {
dbg!(&command_cstr, &args_ptrs);
if unsafe { libc::execv(command_cstr.as_ptr().cast(), args_ptrs.as_ptr().cast()) } == -1 {
return ctx_os_error(format_args!(
"error calling exec({command}, {args:?})",
command = command.display()