Skip to content

Commit cb94305

Browse files
committed
psql: Reset query buffer of \e, \ef and \ev on error
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
1 parent 9bfd44b commit cb94305

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/bin/psql/command.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,6 +1127,14 @@ exec_command_edit(PsqlScanState scan_state, bool active_branch,
11271127
else
11281128
status = PSQL_CMD_ERROR;
11291129
}
1130+
1131+
/*
1132+
* On error while editing or if specifying an incorrect line
1133+
* number, reset the query buffer.
1134+
*/
1135+
if (status == PSQL_CMD_ERROR)
1136+
resetPQExpBuffer(query_buf);
1137+
11301138
free(fname);
11311139
free(ln);
11321140
}
@@ -1239,6 +1247,13 @@ exec_command_ef_ev(PsqlScanState scan_state, bool active_branch,
12391247
status = PSQL_CMD_NEWEDIT;
12401248
}
12411249

1250+
/*
1251+
* On error while doing object lookup or while editing, or if
1252+
* specifying an incorrect line number, reset the query buffer.
1253+
*/
1254+
if (status == PSQL_CMD_ERROR)
1255+
resetPQExpBuffer(query_buf);
1256+
12421257
free(obj_desc);
12431258
}
12441259
else

0 commit comments

Comments
 (0)