From b72959e1752f3d26cee0939ff9fb8789dd7d6af4 Mon Sep 17 00:00:00 2001 From: John Marshall Date: Mon, 27 Nov 2023 20:50:56 +1300 Subject: [PATCH 1/2] Check fclose(stdout) at the end of main() after running subcommands Since PR samtools/htslib#1665, hts_open("-", "w") / hts_close() no longer actually closes stdout. Close it at the end of main() so there is an opportunity to detect I/O errors in previously-uncommitted writes. Ignore EBADF as other code may have already closed stdout, e.g., either particular subcommands or when (dynamically) linked against an older version of HTSlib. --- main.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/main.c b/main.c index a0213589f..f5618c56b 100644 --- a/main.c +++ b/main.c @@ -28,6 +28,7 @@ THE SOFTWARE. */ #include #include #include +#include #include #include "version.h" #include "bcftools.h" @@ -300,7 +301,17 @@ int main(int argc, char *argv[]) { if (cmds[i].func && strcmp(argv[1],cmds[i].alias)==0) { - return cmds[i].func(argc-1,argv+1); + int ret = cmds[i].func(argc-1,argv+1); + + // For subcommands that may have produced substantial output on stdout, + // make a final check for delayed I/O errors. Ignore EBADF as other code + // may have already closed stdout. + if (fclose(stdout) != 0 && errno != EBADF) { + fprintf(stderr, "[E::%s] closing standard output failed\n", __func__); + return 1; + } + + return ret; } i++; } From b56f39a7f1d7b6db92585cfbb3fff8c6883cb22e Mon Sep 17 00:00:00 2001 From: John Marshall Date: Mon, 27 Nov 2023 20:56:16 +1300 Subject: [PATCH 2/2] Include directly where needed [minor] Other source files that use strcase*() functions #include themselves, so do so for this source file too. ( is often a byproduct of but POSIX doesn't require that.) --- csq.c | 1 + gff.c | 1 + plugins/tag2tag.c | 1 + 3 files changed, 3 insertions(+) diff --git a/csq.c b/csq.c index f619e061a..5a11179a3 100644 --- a/csq.c +++ b/csq.c @@ -149,6 +149,7 @@ #include #include #include +#include #include "bcftools.h" #include "filter.h" #include "regidx.h" diff --git a/gff.c b/gff.c index 5d9f062be..6ec5a5875 100644 --- a/gff.c +++ b/gff.c @@ -29,6 +29,7 @@ #include #include #include +#include #include #include #include diff --git a/plugins/tag2tag.c b/plugins/tag2tag.c index 92ee2e478..387b9eec8 100644 --- a/plugins/tag2tag.c +++ b/plugins/tag2tag.c @@ -24,6 +24,7 @@ DEALINGS IN THE SOFTWARE. */ #include #include +#include #include #include #include