Skip to content

Commit

Permalink
Write error message with a single call to write to avoid interleaving
Browse files Browse the repository at this point in the history
  • Loading branch information
casey committed Dec 21, 2024
1 parent d17ec60 commit fbf3320
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/signals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,22 @@ fn die(message: &str) -> ! {
// Standard error is open for the duration of the program.
const STDERR: BorrowedFd = unsafe { BorrowedFd::borrow_raw(libc::STDERR_FILENO) };

nix::unistd::write(STDERR, b"error: ").ok();
nix::unistd::write(STDERR, message.as_bytes()).ok();
nix::unistd::write(STDERR, b"\n").ok();
let mut i = 0;
let mut buffer = [0; 512];

let mut append = |s: &str| {
let remaining = buffer.len() - i;
let n = s.len().min(remaining);
let end = i + n;
buffer[i..end].copy_from_slice(&s.as_bytes()[0..n]);
i = end;
};

append("error: ");
append(message);
append("\n");

nix::unistd::write(STDERR, &buffer[0..i]).ok();

process::abort();
}
Expand Down

0 comments on commit fbf3320

Please sign in to comment.