[llvm] e845f89 - Add an error message to the default SIGPIPE handler

Tim Northover via llvm-commits llvm-commits at lists.llvm.org
Thu May 12 00:00:10 PDT 2022


Author: Tim Northover
Date: 2022-05-12T08:00:06+01:00
New Revision: e845f899e6079c51770162b8995a685287d315a5

URL: https://github.com/llvm/llvm-project/commit/e845f899e6079c51770162b8995a685287d315a5
DIFF: https://github.com/llvm/llvm-project/commit/e845f899e6079c51770162b8995a685287d315a5.diff

LOG: 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.

Added: 
    llvm/test/tools/llvm-nm/unix03-sigpipe-exit.test

Modified: 
    llvm/lib/Support/Unix/Signals.inc

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Support/Unix/Signals.inc b/llvm/lib/Support/Unix/Signals.inc
index 575e2aab1eab2..9426f84b6619d 100644
--- a/llvm/lib/Support/Unix/Signals.inc
+++ b/llvm/lib/Support/Unix/Signals.inc
@@ -432,6 +432,10 @@ void llvm::sys::SetOneShotPipeSignalFunction(void (*Handler)()) {
 }
 
 void llvm::sys::DefaultOneShotPipeSignalHandler() {
+  // UNIX03 conformance requires a non-zero exit code and an error message
+  // to stderr when writing to a closed stdout fails.
+  errs() << "error: write on a pipe with no reader\n";
+
   // Send a special return code that drivers can check for, from sysexits.h.
   exit(EX_IOERR);
 }

diff  --git a/llvm/test/tools/llvm-nm/unix03-sigpipe-exit.test b/llvm/test/tools/llvm-nm/unix03-sigpipe-exit.test
new file mode 100644
index 0000000000000..50b5c08f8e4d0
--- /dev/null
+++ b/llvm/test/tools/llvm-nm/unix03-sigpipe-exit.test
@@ -0,0 +1,17 @@
+# Test that when nm tries to write to a closed stdout it will finish with
+# a non-zero exit code and an error message on stderr.
+# This is required for UNIX03 conformance.
+
+# UNSUPPORTED: system-windows
+
+# RUN: not %python %s llvm-nm llvm-nm 2>&1 | FileCheck %s
+# CHECK: error: write on a pipe with no reader
+
+import subprocess
+import sys
+
+with subprocess.Popen([sys.argv[1], sys.argv[2]], stdout=subprocess.PIPE) as process:
+  # Read single byte and immediately close pipe to trigger SIGPIPE.
+  process.stdout.read(1)
+  process.stdout.close()
+sys.exit(process.returncode)


        


More information about the llvm-commits mailing list