diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2022-08-29 13:55:38 -0400 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2022-08-29 13:55:41 -0400 |
commit | 7fed801135bae14d63b11ee4a10f6083767046d8 (patch) | |
tree | 73fa489ffb169fe62ae066ef5cfe6a39a8fa6163 /src/bin/psql | |
parent | 20796536c10fe7869e7af2c69615f14a80555c5d (diff) | |
download | postgresql-7fed801135bae14d63b11ee4a10f6083767046d8.tar.gz postgresql-7fed801135bae14d63b11ee4a10f6083767046d8.zip |
Clean up inconsistent use of fflush().
More than twenty years ago (79fcde48b), we hacked the postmaster
to avoid a core-dump on systems that didn't support fflush(NULL).
We've mostly, though not completely, hewed to that rule ever since.
But such systems are surely gone in the wild, so in the spirit of
cleaning out no-longer-needed portability hacks let's get rid of
multiple per-file fflush() calls in favor of using fflush(NULL).
Also, we were fairly inconsistent about whether to fflush() before
popen() and system() calls. While we've received no bug reports
about that, it seems likely that at least some of these call sites
are at risk of odd behavior, such as error messages appearing in
an unexpected order. Rather than expend a lot of brain cells
figuring out which places are at hazard, let's just establish a
uniform coding rule that we should fflush(NULL) before these calls.
A no-op fflush() is surely of trivial cost compared to launching
a sub-process via a shell; while if it's not a no-op then we likely
need it.
Discussion: https://postgr.es/m/2923412.1661722825@sss.pgh.pa.us
Diffstat (limited to 'src/bin/psql')
-rw-r--r-- | src/bin/psql/command.c | 4 | ||||
-rw-r--r-- | src/bin/psql/common.c | 1 | ||||
-rw-r--r-- | src/bin/psql/copy.c | 8 | ||||
-rw-r--r-- | src/bin/psql/prompt.c | 4 | ||||
-rw-r--r-- | src/bin/psql/psqlscanslash.l | 1 |
5 files changed, 12 insertions, 6 deletions
diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c index a81bd3307b4..61188d96f2a 100644 --- a/src/bin/psql/command.c +++ b/src/bin/psql/command.c @@ -2661,6 +2661,7 @@ exec_command_write(PsqlScanState scan_state, bool active_branch, if (fname[0] == '|') { is_pipe = true; + fflush(NULL); disable_sigpipe_trap(); fd = popen(&fname[1], "w"); } @@ -3834,6 +3835,7 @@ editFile(const char *fname, int lineno) sys = psprintf("\"%s\" \"%s\"", editorName, fname); #endif + fflush(NULL); result = system(sys); if (result == -1) pg_log_error("could not start editor \"%s\"", editorName); @@ -4956,6 +4958,7 @@ do_shell(const char *command) { int result; + fflush(NULL); if (!command) { char *sys; @@ -5065,6 +5068,7 @@ do_watch(PQExpBuffer query_buf, double sleep) #endif if (pagerprog && myopt.topt.pager) { + fflush(NULL); disable_sigpipe_trap(); pagerpipe = popen(pagerprog, "w"); diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index 9f95869eca6..e611e3266d0 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -59,6 +59,7 @@ openQueryOutputFile(const char *fname, FILE **fout, bool *is_pipe) } else if (*fname == '|') { + fflush(NULL); *fout = popen(fname + 1, "w"); *is_pipe = true; } diff --git a/src/bin/psql/copy.c b/src/bin/psql/copy.c index c181682a132..0f66ebc2edf 100644 --- a/src/bin/psql/copy.c +++ b/src/bin/psql/copy.c @@ -288,8 +288,7 @@ do_copy(const char *args) { if (options->program) { - fflush(stdout); - fflush(stderr); + fflush(NULL); errno = 0; copystream = popen(options->file, PG_BINARY_R); } @@ -307,10 +306,9 @@ do_copy(const char *args) { if (options->program) { - fflush(stdout); - fflush(stderr); - errno = 0; + fflush(NULL); disable_sigpipe_trap(); + errno = 0; copystream = popen(options->file, PG_BINARY_W); } else diff --git a/src/bin/psql/prompt.c b/src/bin/psql/prompt.c index 509e6422b7e..3955d72206c 100644 --- a/src/bin/psql/prompt.c +++ b/src/bin/psql/prompt.c @@ -266,8 +266,10 @@ get_prompt(promptStatus_t status, ConditionalStack cstack) { int cmdend = strcspn(p + 1, "`"); char *file = pnstrdup(p + 1, cmdend); - FILE *fd = popen(file, "r"); + FILE *fd; + fflush(NULL); + fd = popen(file, "r"); if (fd) { if (fgets(buf, sizeof(buf), fd) == NULL) diff --git a/src/bin/psql/psqlscanslash.l b/src/bin/psql/psqlscanslash.l index 878da9f48e9..a467b721449 100644 --- a/src/bin/psql/psqlscanslash.l +++ b/src/bin/psql/psqlscanslash.l @@ -777,6 +777,7 @@ evaluate_backtick(PsqlScanState state) initPQExpBuffer(&cmd_output); + fflush(NULL); fd = popen(cmd, "r"); if (!fd) { |