Use mode "r" for popen() in psql's evaluate_backtick().
authorTom Lane <[email protected]>
Wed, 28 Oct 2020 18:35:53 +0000 (14:35 -0400)
committerTom Lane <[email protected]>
Wed, 28 Oct 2020 18:35:53 +0000 (14:35 -0400)
In almost all other places, we use plain "r" or "w" mode in popen()
calls (the exceptions being for COPY data).  This one has been
overlooked (possibly because it's buried in a ".l" flex file?),
but it's using PG_BINARY_R.

Kensuke Okamura complained in bug #16688 that we fail to strip \r
when stripping the trailing newline from a backtick result string.
That's true enough, but we'd also fail to convert embedded \r\n
cleanly, which also seems undesirable.  Fixing the popen() mode
seems like the best way to deal with this.

It's been like this for a long time, so back-patch to all supported
branches.

Discussion: https://postgr.es/m/16688-c649c7b69cd7e6f8@postgresql.org

src/bin/psql/psqlscan.l

index a8f2bbe094a8e6e684901a21605ace6d76b54341..03b785cd84d104b97e3db7e2c7f7648eb5bb46da 100644 (file)
@@ -1717,7 +1717,7 @@ evaluate_backtick(void)
 
    initPQExpBuffer(&cmd_output);
 
-   fd = popen(cmd, PG_BINARY_R);
+   fd = popen(cmd, "r");
    if (!fd)
    {
        psql_error("%s: %s\n", cmd, strerror(errno));
@@ -1758,7 +1758,7 @@ evaluate_backtick(void)
    /* If no error, transfer result to output_buf */
    if (!error)
    {
-       /* strip any trailing newline */
+       /* strip any trailing newline (but only one) */
        if (cmd_output.len > 0 &&
            cmd_output.data[cmd_output.len - 1] == '\n')
            cmd_output.len--;