diff options
author | Thomas Habets <thomas@habets.se> | 2022-03-09 11:40:40 +0000 |
---|---|---|
committer | Thomas Habets <thomas@habets.se> | 2022-03-09 11:40:40 +0000 |
commit | 7f96e9e8f588c8d3e8015ebef256f3e16f044f40 (patch) | |
tree | f7fc30365d3a3d86dfb557e21bf118937394eeda | |
parent | 7726288bef8dc45bfd18aaad0575822f6958a115 (diff) | |
download | arping-7f96e9e8f588c8d3e8015ebef256f3e16f044f40.tar.gz |
Exit with failure if failing to write to stdout/stderr
Inspired by https://blog.sunfishcode.online/bugs-in-hello-world/
-rw-r--r-- | src/arping.c | 35 |
1 files changed, 33 insertions, 2 deletions
diff --git a/src/arping.c b/src/arping.c index be6e151..11739d7 100644 --- a/src/arping.c +++ b/src/arping.c @@ -2518,11 +2518,42 @@ arping_main(int argc, char **argv) printf("\n"); } + if (fflush(stdout)) { + fprintf(stderr, "arping: Failed to write to stdout: %s\n", + strerror(errno)); + return EXIT_FAILURE; + } + + if (ferror(stdout)) { + fprintf(stderr, "arping: Write failure to stdout: %s\n", + strerror(errno)); + return EXIT_FAILURE; + } + + if (fflush(stderr)) { + // Likely this will go nowhere. But we can't write to + // stdout in case it's being machine parsed. + // + // At least it should show up in strace. + fprintf(stderr, "arping: Failed to write to stderr: %s\n", + strerror(errno)); + return EXIT_FAILURE; + } + + if (ferror(stderr)) { + // Same here. Likely will go nowhere. + fprintf(stderr, "arping: Write failure to stderr: %s\n", + strerror(errno)); + return EXIT_FAILURE; + } + + // We could close stdout/stdin too, but that'll interfere with + // unit testing. + if (finddup) { return dupfound; - } else { - return !numrecvd; } + return !numrecvd; } /* ---- Emacs Variables ---- * Local Variables: |