From 8bd07cccaa251a40256577d9ab85b16086fd9b19 Mon Sep 17 00:00:00 2001 From: ryan Date: Sat, 12 Jul 2025 14:42:50 -0400 Subject: [PATCH] don't construct nullptr cstring --- crates/nit/src/system/syscall.rs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/crates/nit/src/system/syscall.rs b/crates/nit/src/system/syscall.rs index 8b0c343..469b061 100644 --- a/crates/nit/src/system/syscall.rs +++ b/crates/nit/src/system/syscall.rs @@ -204,10 +204,17 @@ pub fn execv(command: impl AsRef, 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()