260915.1 · 24198d904a074f2943b2a3be925e53134c994734
fix: Better sanitizing
This refactors the sanitization code somewhat, to provide a couple benefits:
- Ensure we sanitize output in error messages (#2119), while not
escaping newlines from error messages.
- Avoids some unnecessary allocations and string copies
- Makes `print_error` a little nicer to use. As a macro, it avoids
needing a separate call to `format!`.
The main downside is added complexity.
Fixes: #2119
reading declarations
Changes to src/exec/command.rs. All files
@@ -3,7 +3,6 @@ use std::io::Write; use argmax::Command; -use crate::error::print_error; use crate::exit_codes::ExitCode; struct Outputs {@@ -101,14 +100,14 @@ pub fn execute_commands<I: Iterator<Item = io::Result<Command>>>( pub fn handle_cmd_error(cmd: Option<&Command>, err: io::Error) -> ExitCode { match (cmd, err) { (Some(cmd), err) if err.kind() == io::ErrorKind::NotFound => {- print_error(format!(- "Command not found: {}",- cmd.get_program().to_string_lossy()- ));+ print_error!(+ "Command not found: {:?}",+ cmd.get_program().to_string_lossy().as_ref()+ ); ExitCode::GeneralError } (_, err) => {- print_error(format!("Problem while executing command: {err}"));+ print_error!("Problem while executing command: {}", err); ExitCode::GeneralError } }