Skip to content

Commit

Permalink
serial: fix UTF-8 panic on macOS
Browse files Browse the repository at this point in the history
  • Loading branch information
wgreenberg authored and cooperq committed Jan 27, 2025
1 parent 6bd3692 commit 565c0f1
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion serial/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ fn send_command<T: UsbContext>(handle: &mut DeviceHandle<T>, 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);
Expand Down

0 comments on commit 565c0f1

Please sign in to comment.