diff --git a/src/templ.rs b/src/templ.rs index 6048dd898..1263178fe 100644 --- a/src/templ.rs +++ b/src/templ.rs @@ -79,7 +79,11 @@ macro_rules! string_array_arg { ($name: ident) => { let $name = $name .iter() - .map(|x| x.as_ptr().cast::<::std::ffi::c_char>()) + .map(|x| ::std::ffi::CString::new(*x).expect("invalid C string")) + .collect::<::std::vec::Vec<_>>(); + let $name = $name + .iter() + .map(|x| x.as_ptr()) .collect::<::std::vec::Vec<_>>(); }; } @@ -87,9 +91,14 @@ macro_rules! string_array_arg { #[allow(unused_macros)] macro_rules! string_array_arg_mut { ($name: ident) => { + let $name = $name + .iter() + .map(|x| ::std::ffi::CString::new(*x).expect("invalid C string")) + .collect::<::std::vec::Vec<_>>(); + // Casting to mut below trusts on undefined CString behavior. let mut $name = $name .iter() - .map(|x| x.as_ptr().cast::<::std::ffi::c_char>().cast_mut()) + .map(|x| x.as_ptr().cast_mut()) .collect::<::std::vec::Vec<_>>(); }; }