From 2d780cc147c5b1ea56ffa84ce184cc442aaa36e3 Mon Sep 17 00:00:00 2001 From: amr-crabnebula Date: Fri, 1 Dec 2023 17:39:06 +0200 Subject: [PATCH] fix(packager): avoid crashing on invalid utf8 from shell commands --- crates/packager/src/shell.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/crates/packager/src/shell.rs b/crates/packager/src/shell.rs index 35b7b5c8..3fc3668f 100644 --- a/crates/packager/src/shell.rs +++ b/crates/packager/src/shell.rs @@ -36,15 +36,15 @@ impl CommandExt for Command { let stdout_lines = Arc::new(Mutex::new(Vec::new())); let stdout_lines_ = stdout_lines.clone(); std::thread::spawn(move || { - let mut buf = String::new(); + let mut buf = Vec::new(); let mut lines = stdout_lines_.lock().unwrap(); loop { buf.clear(); - if let Ok(0) = stdout.read_line(&mut buf) { + if let Ok(0) = stdout.read_until(b'\n', &mut buf) { break; } - tracing::debug!("{}", &buf[0..buf.len() - 1]); - lines.extend(buf.as_bytes()); + tracing::debug!("{}", String::from_utf8_lossy(&buf[..buf.len() - 1])); + lines.extend(&buf); } }); @@ -52,15 +52,15 @@ impl CommandExt for Command { let stderr_lines = Arc::new(Mutex::new(Vec::new())); let stderr_lines_ = stderr_lines.clone(); std::thread::spawn(move || { - let mut buf = String::new(); + let mut buf = Vec::new(); let mut lines = stderr_lines_.lock().unwrap(); loop { buf.clear(); - if let Ok(0) = stderr.read_line(&mut buf) { + if let Ok(0) = stderr.read_until(b'\n', &mut buf) { break; } - tracing::debug!("{}", &buf[0..buf.len() - 1]); - lines.extend(buf.as_bytes()); + tracing::debug!("{}", String::from_utf8_lossy(&buf[..buf.len() - 1])); + lines.extend(&buf); } });