From 9f546d7fddadbff6ba921940802caa21700d5b2a Mon Sep 17 00:00:00 2001 From: Will Greenberg Date: Tue, 21 Jan 2025 15:00:13 -0800 Subject: [PATCH] serial: fix UTF-8 panic on macOS --- serial/src/main.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/serial/src/main.rs b/serial/src/main.rs index 02f32b3..05b53c7 100644 --- a/serial/src/main.rs +++ b/serial/src/main.rs @@ -78,7 +78,10 @@ fn send_command(handle: &mut DeviceHandle, command: &str) { .read_bulk(0x82, &mut response, timeout) .expect("Failed to read response"); - let responsestr = str::from_utf8(&response).expect("Failed to parse response"); + // For some reason, on macOS the response buffer gets filled with garbage data that's + // rarely valid UTF-8. Luckily we only care about the first couple bytes, so just drop + // the garbage with `from_utf8_lossy` and look for our expected success string. + let responsestr = String::from_utf8_lossy(&response); if !responsestr.contains("\r\nOK\r\n") { println!("Received unexpected response{0}", responsestr); std::process::exit(1);