psql: Reset query buffer of \e, \ef and \ev on error
authorMichael Paquier <michael@paquier.xyz>
Wed, 20 Sep 2023 00:26:15 +0000 (09:26 +0900)
committerMichael Paquier <michael@paquier.xyz>
Wed, 20 Sep 2023 00:26:15 +0000 (09:26 +0900)
If any of these commands fail during editing or pre-processing, the
command stored in the query buffer would remain around without being
executed immediately as PSQL_CMD_ERROR is returned as status.  The next
command provided by the user would run it, likely causing failures as
this could include silently some of the contents generated automatically
for views or functions.

The problems would be different depending on the psql meta-command used:
- For \ev and \ef, some errors can happen in a predictable way while
doing an object lookup or while creating an object command.  A failure
while editing is equally problematic, but the class of failures
happening in the code path of do_edit() are unlikely.  The query reset
is kept in exec_command_ef_ev() as a query may be unchanged.
- For \e, error can happen while editing.

In both cases, the query buffer is reset on error for an incorrect file
number provided, whose value check is done before filling up the query
buffer.

This is a slight change of behavior compared to the past for some of the
predictable error patterns for \ev and \ef, so for now I have made the
choice to not backpatch this commit (argument particularly available for
v11 that's going to be EOL'd soon).  Perhaps this could be revisited
later depending on the feedback of this new behavior.

Author: Ryoga Yoshida, Michael Paquier
Reviewed-by: Aleksander Alekseev, Kyotaro Horiguchi
Discussion: https://postgr.es/m/01419622d84ef093fd4fe585520bf03c@oss.nttdata.com

src/bin/psql/command.c

index bcd8eb353870a68c611245f2acff59921655d877..4dfcf04fe03132344d6b3be07b78197e9258b20b 100644 (file)
@@ -1127,6 +1127,14 @@ exec_command_edit(PsqlScanState scan_state, bool active_branch,
                                else
                                        status = PSQL_CMD_ERROR;
                        }
+
+                       /*
+                        * On error while editing or if specifying an incorrect line
+                        * number, reset the query buffer.
+                        */
+                       if (status == PSQL_CMD_ERROR)
+                               resetPQExpBuffer(query_buf);
+
                        free(fname);
                        free(ln);
                }
@@ -1239,6 +1247,13 @@ exec_command_ef_ev(PsqlScanState scan_state, bool active_branch,
                                status = PSQL_CMD_NEWEDIT;
                }
 
+               /*
+                * On error while doing object lookup or while editing, or if
+                * specifying an incorrect line number, reset the query buffer.
+                */
+               if (status == PSQL_CMD_ERROR)
+                       resetPQExpBuffer(query_buf);
+
                free(obj_desc);
        }
        else