Skip to content

Commit 51b557a

Browse files
committed
Add an error message to the default SIGPIPE handler
UNIX03 conformance requires utilities to flush stdout before exiting and raise an error if writing fails. Flushing already happens on a call to exit and thus automatically on a return from main. Write failure is then detected by LLVM's default SIGPIPE handler. The handler already exits with a non-zero code, but conformance additionally requires an error message.
1 parent b03451b commit 51b557a

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

llvm/lib/Support/Unix/Signals.inc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,10 @@ void llvm::sys::SetOneShotPipeSignalFunction(void (*Handler)()) {
432432
}
433433

434434
void llvm::sys::DefaultOneShotPipeSignalHandler() {
435+
// UNIX03 conformance requires a non-zero exit code and an error message
436+
// to stderr when writing to a closed stdout fails.
437+
errs() << "error: write on a pipe with no reader\n";
438+
435439
// Send a special return code that drivers can check for, from sysexits.h.
436440
exit(EX_IOERR);
437441
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Test that when nm tries to write to a closed stdout it will finish with
2+
# a non-zero exit code and an error message on stderr.
3+
# This is required for UNIX03 conformance.
4+
5+
# UNSUPPORTED: system-windows
6+
7+
# RUN: not %python %s llvm-nm llvm-nm 2>&1 | FileCheck %s
8+
# CHECK: error: write on a pipe with no reader
9+
10+
import subprocess
11+
import sys
12+
13+
with subprocess.Popen([sys.argv[1], sys.argv[2]], stdout=subprocess.PIPE) as process:
14+
# Read single byte and immediately close pipe to trigger SIGPIPE.
15+
process.stdout.read(1)
16+
process.stdout.close()
17+
sys.exit(process.returncode)

0 commit comments

Comments
 (0)