From 888321446d30e178833e1950f45a41ff293d10a5 Mon Sep 17 00:00:00 2001 From: Wesley Moore Date: Wed, 13 Mar 2024 20:07:07 +1000 Subject: [PATCH] Avoid casting away const when processing C command args --- src/bcachefs.rs | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/src/bcachefs.rs b/src/bcachefs.rs index ef38a66b..079a4155 100644 --- a/src/bcachefs.rs +++ b/src/bcachefs.rs @@ -2,7 +2,7 @@ mod wrappers; mod commands; mod key; -use std::ffi::CString; +use std::ffi::{c_char, CString}; use commands::cmd_completions::cmd_completions; use commands::cmd_list::cmd_list; @@ -21,13 +21,7 @@ impl std::fmt::Display for ErrnoError { impl std::error::Error for ErrnoError {} -fn to_mut(p: *const T) -> *mut T { - p as *mut T -} - -fn handle_c_command(args: Vec, symlink_cmd: Option<&str>) -> i32 { - let mut argv: Vec<_> = args.clone(); - +fn handle_c_command(mut argv: Vec, symlink_cmd: Option<&str>) -> i32 { let cmd = match symlink_cmd { Some(s) => s.to_string(), None => argv.remove(1), @@ -36,10 +30,13 @@ fn handle_c_command(args: Vec, symlink_cmd: Option<&str>) -> i32 { let argc: i32 = argv.len().try_into().unwrap(); let argv: Vec<_> = argv - .iter() - .map(|s| CString::new(s.as_str()).unwrap()) + .into_iter() + .map(|s| CString::new(s).unwrap()) .collect(); - let mut argv: Vec<_> = argv.iter().map(|s| to_mut(s.as_ptr())).collect(); + let mut argv = argv + .into_iter() + .map(|s| Box::into_raw(s.into_boxed_c_str()) as *mut c_char) + .collect::>(); let argv = argv.as_mut_ptr(); // The C functions will mutate argv. It shouldn't be used after this block.