From 68326174f754c42ff39b112c8cc3e27a61d578e9 Mon Sep 17 00:00:00 2001 From: ryan Date: Sat, 12 Jul 2025 14:33:48 -0400 Subject: [PATCH] add nullptr to last arg in execv() --- crates/nit/src/system/syscall.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/crates/nit/src/system/syscall.rs b/crates/nit/src/system/syscall.rs index 55b2dc0..8b0c343 100644 --- a/crates/nit/src/system/syscall.rs +++ b/crates/nit/src/system/syscall.rs @@ -200,12 +200,18 @@ pub fn execv(command: impl AsRef, args: &[&str]) -> Result<()> { .context(format_args!("bad command: {}", command.display()))?; let mut args_cstr = vec![]; for arg in args { - let cur_arg_cstr = CString::new(arg.as_bytes()).context(format_args!("bad arg: {arg}")); + let cur_arg_cstr = CString::new(arg.as_bytes()).context(format_args!("bad arg: {arg}"))?; 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()) }); + if unsafe { libc::execv(command_cstr.as_ptr().cast(), args_cstr.as_ptr().cast()) } == -1 { - return ctx_os_error(format_args!("error calling exec()")); + return ctx_os_error(format_args!( + "error calling exec({command}, {args:?})", + command = command.display() + )); } Ok(())