Skip to content

Commit

Permalink
Check fclose(stdout) at the end of main()
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jmarshall committed Aug 21, 2023
1 parent ed3b05f commit 361dce9
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions bamtk.c
Original file line number Diff line number Diff line change
Expand Up @@ -303,5 +303,14 @@ int main(int argc, char *argv[])
fprintf(stderr, "[main] unrecognized command '%s'\n", argv[1]);
return 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) {
print_error_errno(argv[1], "closing standard output failed");
return 1;
}

return ret;
}

0 comments on commit 361dce9

Please sign in to comment.