From fa7864edebc0a23d803adee509dcb99292cfd371 Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Sat, 2 Mar 2024 20:26:06 +0900 Subject: [PATCH] signing: ensure child processes are wait()ed on I/O error This will also provide a better error indication. If write() failed, the child process would presumably have exited with non-zero status and error message to stderr. --- lib/src/gpg_signing.rs | 3 ++- lib/src/ssh_signing.rs | 15 +++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/src/gpg_signing.rs b/lib/src/gpg_signing.rs index ea4c85404..7d1385597 100644 --- a/lib/src/gpg_signing.rs +++ b/lib/src/gpg_signing.rs @@ -68,9 +68,10 @@ fn parse_gpg_verify_output( fn run_sign_command(command: &mut Command, input: &[u8]) -> Result, GpgError> { let process = command.stderr(Stdio::piped()).spawn()?; - process.stdin.as_ref().unwrap().write_all(input)?; + let write_result = process.stdin.as_ref().unwrap().write_all(input); let output = process.wait_with_output()?; if output.status.success() { + write_result?; Ok(output.stdout) } else { Err(GpgError::Command { diff --git a/lib/src/ssh_signing.rs b/lib/src/ssh_signing.rs index 235d56536..4e3378851 100644 --- a/lib/src/ssh_signing.rs +++ b/lib/src/ssh_signing.rs @@ -60,18 +60,17 @@ fn parse_utf8_string(data: Vec) -> SshResult { fn run_command(command: &mut Command, stdin: &[u8]) -> SshResult> { let process = command.spawn()?; - process.stdin.as_ref().unwrap().write_all(stdin)?; - + let write_result = process.stdin.as_ref().unwrap().write_all(stdin); let output = process.wait_with_output()?; - - if !output.status.success() { - return Err(SshError::Command { + if output.status.success() { + write_result?; + Ok(output.stdout) + } else { + Err(SshError::Command { exit_status: output.status, stderr: String::from_utf8_lossy(&output.stderr).trim_end().into(), - }); + }) } - - Ok(output.stdout) } // This attempts to convert given key data into a file and return the filepath.