feat: avoid leaking and use a Vec instead (thanks @filiptibell)

This commit is contained in:
Erica Marigold 2023-09-30 11:16:43 +05:30
parent ac87772e61
commit 47d39cb8a9
No known key found for this signature in database
GPG key ID: 7843994FD1386E35
2 changed files with 5 additions and 5 deletions

View file

@ -200,10 +200,10 @@ async fn process_spawn(
async fn spawn_command( async fn spawn_command(
program: String, program: String,
args: Option<Vec<String>>, args: Option<Vec<String>>,
options: ProcessSpawnOptions, mut options: ProcessSpawnOptions,
) -> LuaResult<(ExitStatus, Vec<u8>, Vec<u8>)> { ) -> LuaResult<(ExitStatus, Vec<u8>, Vec<u8>)> {
let inherit_stdio = options.inherit_stdio; let inherit_stdio = options.inherit_stdio;
let stdin = options.stdin; let stdin = options.stdin.take();
let mut child = options let mut child = options
.into_command(program, args) .into_command(program, args)
@ -218,7 +218,7 @@ async fn spawn_command(
// If the stdin option was provided, we write that to the child // If the stdin option was provided, we write that to the child
if let Some(stdin) = stdin { if let Some(stdin) = stdin {
let mut child_stdin = child.stdin.take().unwrap(); let mut child_stdin = child.stdin.take().unwrap();
child_stdin.write_all(stdin).await.into_lua_err()?; child_stdin.write_all(&stdin).await.into_lua_err()?;
} }
if inherit_stdio { if inherit_stdio {

View file

@ -14,7 +14,7 @@ pub struct ProcessSpawnOptions {
pub(crate) envs: HashMap<String, String>, pub(crate) envs: HashMap<String, String>,
pub(crate) shell: Option<String>, pub(crate) shell: Option<String>,
pub(crate) inherit_stdio: bool, pub(crate) inherit_stdio: bool,
pub(crate) stdin: Option<&'static [u8]>, pub(crate) stdin: Option<Vec<u8>>,
} }
impl<'lua> FromLua<'lua> for ProcessSpawnOptions { impl<'lua> FromLua<'lua> for ProcessSpawnOptions {
@ -139,7 +139,7 @@ impl<'lua> FromLua<'lua> for ProcessSpawnOptions {
*/ */
match value.get("stdin")? { match value.get("stdin")? {
LuaValue::Nil => {} LuaValue::Nil => {}
LuaValue::String(s) => this.stdin = Some(&*(s.as_bytes().to_vec().leak())), LuaValue::String(s) => this.stdin = Some(s.as_bytes().to_vec()),
value => { value => {
return Err(LuaError::RuntimeError(format!( return Err(LuaError::RuntimeError(format!(
"Invalid type for option 'stdin' - expected 'string', got '{}'", "Invalid type for option 'stdin' - expected 'string', got '{}'",